o
    va                     @   s\   d Z ddlZddlZddlmZ ddlmZ ddlmZ G dd deZ	G dd	 d	e
ZdS )
zIRQ plugin.    N)LINUX)getTimeSinceLastUpdate)GlancesPluginc                       sT   e Zd ZdZd fdd	Zdd Zejejdd Z	 fd	d
Z
dddZ  ZS )Pluginz-Glances IRQ plugin.

    stats is a list
    Nc                    s(   t t| j||g d d| _t | _dS )zInit the plugin.)argsconfigZstats_init_valueTN)superr   __init__Zdisplay_curse
GlancesIRQirq)selfr   r   	__class__ =/usr/lib/python3/dist-packages/glances/plugins/glances_irq.pyr	   $   s   zPlugin.__init__c                 C   s
   | j  S )zReturn the key of the list.)r   get_keyr   r   r   r   r   0   s   
zPlugin.get_keyc                 C   s\   |   }ts	| jS | jdkr| j }n| jdkr	 t|tddddd }|| _| jS )zUpdate the IRQ stats.ZlocalZsnmpirq_rateT)keyreverseN   )	Zget_init_valuer   statsZinput_methodr   getsortedoperator
itemgetter)r   r   r   r   r   update4   s   

zPlugin.updatec                    s   t t|   dS )zUpdate stats views.N)r   r   update_viewsr   r   r   r   r   Q   s   zPlugin.update_viewsc                 C   s   g }t r| jr|  r|S |d }djd|d}|| |d dd}|| | | jD ]/}||   dj|d d	| |d}|| | dt|d
 }|| | q1|S )z2Return the dict to display in the curse interface.   z
{:{width}}ZIRQ)widthZTITLEz{:>9}zRate/sirq_lineNr   )r   r   Zis_disabledformatappendZcurse_add_lineZcurse_new_linestr)r   r   Z	max_widthretZname_max_widthmsgir   r   r   	msg_curseV   s"   

zPlugin.msg_curse)NN)__name__
__module____qualname____doc__r	   r   r   Z_check_decoratorZ_log_result_decoratorr   r   r'   __classcell__r   r   r   r   r      s    r   c                   @   sT   e Zd ZdZ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 )r
   z This class manages the IRQ file.z/proc/interruptsc                 C   s   i | _ |   dS )zPInit the class.

        The stat are stored in a internal list of dict
        N)lastsresetr   r   r   r   r	   z   s   zGlancesIRQ.__init__c                 C   s   g | _ d| _dS )zReset the stats.r   N)r   
cpu_numberr   r   r   r   r.      s   
zGlancesIRQ.resetc                 C   s   |   S )zReturn the current IRQ stats.)_GlancesIRQ__updater   r   r   r   r      s   zGlancesIRQ.getc                 C   s   dS )zReturn the key of the dict.r    r   r   r   r   r   r      s   zGlancesIRQ.get_keyc                 C   s   t | | _| jS )zBuild the header (contain the number of CPU).

        CPU0       CPU1       CPU2       CPU3
        0:         21          0          0          0   IO-APIC   2-edge      timer
        )lensplitr/   )r   liner   r   r   Z__header   s   zGlancesIRQ.__headerc                 C   s6   |  }|d dd}| r|d|d 7 }|S )a  Return the IRQ name, alias or number (choose the best for human).

        IRQ line samples:
        1:      44487        341         44         72   IO-APIC   1-edge      i8042
        LOC:   33549868   22394684   32474570   21855077   Local timer interrupts
        r   : z_{})r2   replaceisdigitr!   )r   r3   splitted_liner    r   r   r   Z__humanname   s
   zGlancesIRQ.__humannamec                 C   sD   |  }zttt|d| jd  }W |S  ty!   d}Y |S w )zReturn the IRQ sum number.

        IRQ line samples:
        1:     44487        341         44         72   IO-APIC   1-edge      i8042
        LOC:   33549868   22394684   32474570   21855077   Local timer interrupts
        FIQ:   usb_fiq
           r   )r2   summapintr/   
ValueError)r   r3   r9   r$   r   r   r   Z__sum   s   zGlancesIRQ.__sumc              	   C   s   |    tj| js| jS z`t| jO}td}| |	  |
 D ]4}| |}| |}t| j|r?|| j| nd| }|||  |d}| j| || j|< q$W d   W | jS 1 sfw   Y  W | jS  ttfy{   Y | jS w )z/Load the IRQ file and update the internal dict.r   r   )r    r   r   time_since_updateN)r.   ospathexistsIRQ_FILEr   openr   _GlancesIRQ__headerreadline	readlines_GlancesIRQ__humanname_GlancesIRQ__sumr=   r-   r   r   r"   OSErrorIOError)r   Zirq_procr?   r3   r    Zcurrent_irqsr   Zirq_currentr   r   r   Z__update   s>   


zGlancesIRQ.__updateN)r(   r)   r*   r+   rC   r	   r.   r   r   rE   rH   rI   r0   r   r   r   r   r
   u   s    	r
   )r+   r@   r   Zglances.globalsr   Zglances.timerr   Zglances.plugins.glances_pluginr   r   objectr
   r   r   r   r   <module>   s   W