o
    Ìv›aŸ  ã                   @   sn   d Z ddlmZ ddlmZ ddlmZ zddlZW n ey*   e 	d¡ dZ
Y nw dZ
G d	d
„ d
eƒZdS )zManage on alert actions.é    )Úlogger)ÚTimer)Úsecure_popenNz5Chevron library not found (action scripts won't work)FTc                   @   s4   e Zd ZdZddd„Zdd„ Zdd„ Zdd	d
„ZdS )ÚGlancesActionsz0This class manage action if an alert is reached.Nc                 C   s2   i | _ t|dƒrt|jd ƒ| _dS tdƒ| _dS )zInit GlancesActions class.Útimeé   é   N)ÚstatusÚhasattrr   r   Ústart_timer)ÚselfÚargs© r   ú1/usr/lib/python3/dist-packages/glances/actions.pyÚ__init__'   s   
zGlancesActions.__init__c                 C   s"   z| j | W S  ty   Y dS w )zGet the stat_name criticity.N)r	   ÚKeyError)r   Ú	stat_namer   r   r   Úget6   s
   ÿzGlancesActions.getc                 C   s   || j |< dS )zSet the stat_name to criticity.N)r	   )r   r   Ú	criticityr   r   r   Úset=   s   zGlancesActions.setc           
      C   sÞ   |   |¡|kr	|r| j ¡ sdS t d |rdnd||||¡¡ |D ]D}tr-t ||¡}n|}t 	d |||¡¡ zt
|ƒ}W n ty[ }	 zt d |||	¡¡ W Y d}	~	q"d}	~	ww t d |||¡¡ q"|  ||¡ d	S )
aq  Run the commands (in background).

        - stats_name: plugin_name (+ header)
        - criticity: criticity of the trigger
        - commands: a list of command line with optional {{mustache}}
        - If True, then repeat the action
        - mustache_dict: Plugin stats (can be use within {{mustache}})

        Return True if the commands have been ran.
        Fz&{} action {} for {} ({}) with stats {}ZRepeatZRunz Action triggered for {} ({}): {}zAction error for {} ({}): {}NzAction result for {} ({}): {}T)r   r   Zfinishedr   ÚdebugÚformatÚchevron_tagÚchevronZrenderÚinfor   ÚOSErrorÚerrorr   )
r   r   r   ZcommandsÚrepeatZmustache_dictÚcmdZcmd_fullÚretÚer   r   r   ÚrunA   s>   ÿ
þ
þ
þ€ÿ
þzGlancesActions.run)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r!   r   r   r   r   r   #   s    
r   )r%   Zglances.loggerr   Zglances.timerr   Zglances.securer   r   ÚImportErrorr   r   Úobjectr   r   r   r   r   Ú<module>   s   
þ