o
    va                     @   sd   d Z ddlZddl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mZ G dd deZdS )	zManage the AMPs list.    N)listkeys	iteritems)logger)	amps_path)glances_processesc                   @   sl   e Zd ZdZi 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S )AmpsLista  This class describes the optional application monitoring process list.

    The AMP list is a list of processes with a specific monitoring action.

    The list (Python list) is composed of items (Python dict).
    An item is defined (dict keys):
    *...
    c                 C   s   || _ || _|   dS )zInit the AMPs list.N)argsconfigload_configs)selfr   r	    r   3/usr/lib/python3/dist-packages/glances/amps_list.py__init__.   s   zAmpsList.__init__c                 C   sP  | j du rdS d| j  v rtd d}| j  D ]}|dr|dd }tjt||dd  d }tj	|sCtjtd	}zt
tj|dd
 }W n5 tyl } ztd|| W Y d}~qd}~w ty } ztd|| W Y d}~qd}~ww |j|| jd| j|< | j| | j  qtd|   dS )z!Load the AMP configuration files.NFZmonitorzA deprecated [monitor] section exists in the Glances configuration file. You should use the new Applications Monitoring Process module instead (http://glances.readthedocs.io/en/develop/aoa/amps.html).Zglances_Zamp_   z.pyzglances_default.pyz+Missing Python Lib ({}), cannot load {} AMPzCannot load {} AMP ({}))namer   zAMPs list: {}T)r	   Zsectionsr   Zwarning
startswithospathjoinr   exists
__import__basenameImportErrorformat	ExceptionZAmpr   _AmpsList__amps_dictZload_configdebuggetList)r   headersZamp_conf_nameZ
amp_scriptZamper   r   r   r
   6   s2   


zAmpsList.load_configsc                 C   
   t | jS N)strr   r   r   r   r   __str__]      
zAmpsList.__str__c                 C      | j S r#   r   r%   r   r   r   __repr__`   s   zAmpsList.__repr__c                 C   s
   | j | S r#   r)   )r   itemr   r   r   __getitem__c   r'   zAmpsList.__getitem__c                 C   r"   r#   )lenr   r%   r   r   r   __len__f   r'   zAmpsList.__len__c                 C   s   t  }t|  D ]^\}}| sq
| du r,|d tj|j	g gd}|
  q
| ||}t|dkrRtdt||| tj|j	|gd}|
  q
|d | durh| dkrh|d q
| jS )z%Update the command result attributed.Nr   )targetr   z#AMPS: {} processes {} detected ({})zNo running process)r   Zgetlistr   getZenableregexZ	set_count	threadingZThreadupdate_wrapperstart_build_amps_listr-   r   r   r   Z	count_minZ
set_resultr   )r   processlistkvthreadZ	amps_listr   r   r   updatei   s,   



zAmpsList.updatec              
   C   s   g }zF|D ]@}d}t | |d durd}n|d du rq|d D ]}t | |dur3d} nq#|rE||d |d |d d	 qW |S  ttfye } ztd
| W Y d}~|S d}~ww )zReturn the AMPS process list according to the amp_value

        Search application monitored processes by a regular expression
        Fr   NTZcmdlinepidcpu_percentmemory_percent)r;   r<   r=   zCan not build AMPS list ({}))	researchr1   append	TypeErrorKeyErrorr   r   r   )r   Z	amp_valuer6   retpZadd_itcr!   r   r   r   r5      s4   
zAmpsList._build_amps_listc                 C   r"   )zReturn the AMPs list.)r   r   r%   r   r   r   r         
zAmpsList.getListc                 C   r(   )zReturn the AMPs dict.r)   r%   r   r   r   r0      s   zAmpsList.getc                 C   s
   || _ dS )zSet the AMPs dict.Nr)   )r   Znew_dictr   r   r   set   rF   zAmpsList.setN)__name__
__module____qualname____doc__r   r   r
   r&   r*   r,   r.   r:   r5   r   r0   rG   r   r   r   r   r       s    
'&r   )rK   r   r>   r2   Zglances.compatr   r   Zglances.loggerr   Zglances.globalsr   Zglances.processesr   objectr   r   r   r   r   <module>   s   