o
    Ìv›a7  ã                   @   sH   d Z ddlmZmZmZmZ ddlmZ ddlm	Z	 G dd„ de
ƒZdS )ay  
I am your father...

...for all Glances Application Monitoring Processes (AMP).

AMP (Application Monitoring Process)
A Glances AMP is a Python script called (every *refresh* seconds) if:
- the AMP is *enabled* in the Glances configuration file
- a process is running (match the *regex* define in the configuration file)
The script should define a Amp (GlancesAmp) class with, at least, an update method.
The update method should call the set_result method to set the AMP return string.
The return string is a string with one or more line (
 between lines).
If the *one_line* var is true then the AMP will be displayed in one line.
é    )ÚuÚbÚnÚ	nativestr)ÚTimer)Úloggerc                   @   s¨   e Zd ZdZdZdZdZdZdZd%dd„Z	dd„ Z
dd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd&dd „Zd!d"„ Zd#d$„ ZdS )'Ú
GlancesAmpzMain class for Glances AMP.ú?Nc                 C   sV   t  d | j| j¡¡ |du r| jjtdƒd… | _n|| _|| _	i | _
tdƒ| _dS )zInit AMP classe.zAMP - Init {} version {}NZglances_r   )r   ÚdebugÚformatÚNAMEÚVERSIONÚ	__class__Ú
__module__ÚlenÚamp_nameÚargsÚconfigsr   Útimer)ÚselfÚnamer   © r   ú:/usr/lib/python3/dist-packages/glances/amps/glances_amp.pyÚ__init__1   s   zGlancesAmp.__init__c              	   C   sH  d| j  }t|dƒre| |¡ret d | j¡¡ | |¡D ]F\}}z| ||¡| j	|< W n( t
yT   | ||¡ d¡| j	|< t| j	| ƒdkrR| j	| d | j	|< Y nw t d | j|| j	| ¡¡ qnt d | j| j ¡¡ d	S |  ¡ r’d
D ]}|| j	vrt d | j|| j ¡¡ d| j	d< qxn	t d | j¡¡ d| j	d< |  ¡ S )z0Load AMP parameters from the configuration file.Zamp_Úhas_sectionzAMP - {}: Load configurationú,é   r   z!AMP - {}: Load parameter: {} = {}z;AMP - {}: Can not find section {} in the configuration fileF)ÚrefreshzTAMP - {}: Can not find configuration key {} in section {} (the AMP will be disabled)ZfalseÚenablezAMP - {} is disabledÚcount)r   Úhasattrr   r   r
   r   r   ÚitemsZget_float_valuer   Ú
ValueErrorZ	get_valueÚsplitr   r   Zwarning)r   ZconfigZamp_sectionZparamÚ_Úkr   r   r   Úload_configE   s8   

ÿ€ýù	

€ý
zGlancesAmp.load_configc                 C   s   || j v r
| j | S dS )z7Generic method to get the item in the AMP configurationN©r   )r   Úkeyr   r   r   Úgett   s   

zGlancesAmp.getc                 C   ó$   |   d¡}|du rdS | ¡  d¡S )zVReturn True|False if the AMP is enabled in the configuration file (enable=true|false).r   NFÚtrue©r)   ÚlowerÚ
startswith©r   Úretr   r   r   r   {   ó   
zGlancesAmp.enablec                 C   ó
   |   d¡S )zEReturn regular expression used to identified the current application.Úregex©r)   ©r   r   r   r   r3   ƒ   ó   
zGlancesAmp.regexc                 C   r2   )zNReturn refresh time in seconds for the current application monitoring process.r   r4   r5   r   r   r   r   ‡   r6   zGlancesAmp.refreshc                 C   r*   )zVReturn True|False if the AMP shoukd be displayed in oneline (one_lineline=true|false).Úone_lineNFr+   r,   r/   r   r   r   r7   ‹   r1   zGlancesAmp.one_linec                 C   s
   | j  ¡ S )z%Return time in seconds until refresh.)r   r)   r5   r   r   r   Útime_until_refresh“   r6   zGlancesAmp.time_until_refreshc                 C   s0   | j  ¡ r| j  |  ¡ ¡ | j  ¡  |  ¡ S dS )zxReturn True is the AMP should be updated:
        - AMP is enable
        - only update every 'refresh' seconds
        F)r   ZfinishedÚsetr   Úresetr   r5   r   r   r   Úshould_update—   s
   

zGlancesAmp.should_updatec                 C   s   || j d< dS )z.Set the number of processes matching the regexr   Nr'   )r   r   r   r   r   Ú	set_count¢   s   zGlancesAmp.set_countc                 C   r2   )z.Get the number of processes matching the regexr   r4   r5   r   r   r   r   ¦   r6   zGlancesAmp.countc                 C   r2   )z#Get the minimum number of processesZcountminr4   r5   r   r   r   Ú	count_minª   r6   zGlancesAmp.count_minc                 C   r2   )z#Get the maximum number of processesZcountmaxr4   r5   r   r   r   Ú	count_max®   r6   zGlancesAmp.count_maxÚ c                 C   s4   |   ¡ rt|ƒ d|¡| jd< dS t|ƒ| jd< dS )zyStore the result (string) into the result key of the AMP
        if one_line is true then replace 
 by separator
        Ú
ÚresultN)r7   r   Úreplacer   )r   rA   Z	separatorr   r   r   Ú
set_result²   s   zGlancesAmp.set_resultc                 C   s   |   d¡}|durt|ƒ}|S )z+ Return the result of the AMP (as a string)rA   N)r)   r   r/   r   r   r   rA   »   s   
zGlancesAmp.resultc                 C   s(   |   t|ƒ¡ |  ¡ r|  |¡S |  ¡ S )zWrapper for the children update)r<   r   r;   ÚupdaterA   )r   Zprocess_listr   r   r   Úupdate_wrapperÂ   s   
zGlancesAmp.update_wrapper)NN)r?   )Ú__name__r   Ú__qualname__Ú__doc__r   r   ZDESCRIPTIONZAUTHORZEMAILr   r&   r)   r   r3   r   r7   r8   r;   r<   r   r=   r>   rC   rA   rE   r   r   r   r   r   (   s.    
/
	r   N)rH   Zglances.compatr   r   r   r   Zglances.timerr   Zglances.loggerr   Úobjectr   r   r   r   r   Ú<module>   s
   