o
    va                  
   @   s   d Z ddlZddlmZmZ ddlmZ ddlmZ ddl	Z	zddl
mZ ddlmZ W n eyH Z zdZed	e W Y dZ[ndZ[ww d
ZerTdZed G dd deZdS )zWifi plugin.    N)	nativestrPY3)logger)GlancesPlugin)Cell)InterfaceErrorTz0Missing Python Lib ({}), Wifi plugin is disabledFz@Wifi lib is not compliant with Python 3, Wifi plugin is disabledc                       s\   e Zd ZdZd fdd	Zdd Zejejdd Z	d	d
 Z
 fddZdddZ  ZS )PluginzFGlances Wifi plugin.

    Get stats of the current Wifi hotspots.
    Nc                    s    t t| j||g d d| _dS )zInit the plugin.)argsconfigZstats_init_valueTN)superr   __init__Zdisplay_curse)selfr	   r
   	__class__ >/usr/lib/python3/dist-packages/glances/plugins/glances_wifi.pyr   4   s
   
zPlugin.__init__c                 C   s   dS )zVReturn the key of the list.

        :returns: string -- SSID is the dict key
        ssidr   )r   r   r   r   get_key=   s   zPlugin.get_keyc                 C   s&  |   }tr|S | jdkrztjdd}W n ty    | Y S w |D ]b}| |r+q#zt|}W n3 t	yL } zt
d| W Y d}~q#d}~w tye } zt
d| W Y d}~q#d}~ww |D ]}|  |j|j|j|j|jr{|jndd}|| qhq#n| jdkr	 || _| jS )	zUpdate Wifi stats using the input method.

        Stats is a list of dict (one dict per hotspot)

        :returns: list -- Stats is a list of dict (hotspot)
        ZlocalT)Zpernicz%WIFI plugin: Scan InterfaceError ({})Nz,WIFI plugin: Can not grab cellule stats ({}))keyr   signalquality	encryptedencryption_typeZsnmp)Zget_init_valueimport_error_tagZinput_methodpsutilZnet_io_countersUnicodeDecodeErrorZis_hider   allr   r   debugformat	Exceptionr   r   r   r   r   r   appendstats)r   r!   ZnetiocountersZnetZ
wifi_cellseZ	wifi_cellZhotspotr   r   r   updateD   sL   


	
zPlugin.updatec              	   C   s   d}z0|| j d| jdkrd}W |S || j d| jdkr!d}W |S || j d| jdkr0d}W |S W |S  ttfy@   d	}Y |S w )
zOverwrite the default get_alert method.

        Alert is on signal quality where lower is better...

        :returns: string -- Signal alert
        ZOKZcritical)Z	stat_nameZCRITICALwarningZWARNINGZcarefulZCAREFULZDEFAULT)Z	get_limitZplugin_name	TypeErrorKeyError)r   valueretr   r   r   	get_alert   s"   	zPlugin.get_alertc                    sr   t t|   | jD ],}| |d | j||    d d< | j||    d d | j||    d d< q
dS )zUpdate stats views.r   
decorationr   N)r   r   update_viewsr!   r)   Zviewsr   )r   ir   r   r   r+      s
   
$2zPlugin.update_viewsc                 C   sP  g }| j rts|  r|S |d }djd|d}|| |d dd}|| | t| j t| 	 dD ]k}|d	 d
ksN|d	 du sN|d du rOq:|| 
  |d	 }|d rg|d|d 7 }t||krxd|| d d  }djt||d}|| | dj|d |d}|| || j|| 	  ddd q:|S )z2Return the dict to display in the curse interface.   z
{:{width}}ZWIFI)widthZTITLEz{:>7}ZdBm)r   r    Nr   r   z {}r   _   r*   )itemr   option)r!   r   Zis_disabledr   r    Zcurse_add_linesortedoperator
itemgetterr   Zcurse_new_linelenr   Z	get_views)r   r	   Z	max_widthr(   Zifname_max_widthmsgr,   Zhotspotnamer   r   r   	msg_curse   s>   
$


zPlugin.msg_curse)NN)__name__
__module____qualname____doc__r   r   r   Z_check_decoratorZ_log_result_decoratorr#   r)   r+   r9   __classcell__r   r   r   r   r   .   s    	?r   )r=   r5   Zglances.compatr   r   Zglances.loggerr   Zglances.plugins.glances_pluginr   r   Z	wifi.scanr   Zwifi.exceptionsr   ImportErrorr"   r   r$   r   r   r   r   r   r   <module>   s&   
