o
    va                     @   s>   d Z ddlmZ ddlmZ ddlZG dd deZe ZdS )z;CPU percent stats shared between CPU and Quicklook plugins.    )logger)TimerNc                   @   sL   e Zd ZdZdddZdd Zddd	Zd
d Zdd Zdd Z	dd Z
dS )
CpuPercentzGet and store the CPU percent.   c                 C   sT   d d d d| _ d| _g | _|   || _td| _td| _|d | _td| _	d S )N)cpu_namecpu_hz_currentcpu_hzr      )
cpu_infocpu_percentpercpu_percent_CpuPercent__get_cpu_namecached_timer_cpur   	timer_cputimer_percpucached_timer_cpu_infotimer_cpu_info)selfr    r   5/usr/lib/python3/dist-packages/glances/cpu_percent.py__init__    s   


zCpuPercent.__init__c                 C   s   dS )z#Return the key of the per CPU list.
cpu_numberr   r   r   r   r   get_key7   s   zCpuPercent.get_keyFc                 C   s   |r|   S |  S )zaUpdate and/or return the CPU using the psutil library.
        If percpu, return the percpu stats)_CpuPercent__get_percpu_CpuPercent__get_cpu)r   percpur   r   r   get;   s   zCpuPercent.getc              
   C   s   | j  rWttdrWzt }W n ty, } ztd| W Y d}~| j
S d}~ww t|dr9|j	| j
d< nd| j
d< t|drJ|j| j
d< nd| j
d< | j j| jd | j
S )	z)Get additional informations about the CPUcpu_freqz!Can not grab CPU information ({})Ncurrentr   maxr   Zduration)r   finishedhasattrpsutilr   	Exceptionr   debugformatr   r
   r    resetr   )r   r   er   r   r   get_infoC   s    



zCpuPercent.get_infoc                 C   sH   zt dd d dd  | jd< W n	   d| jd< Y | jd S )Nz/proc/cpuinfor   :   r   ZCPU)open	readlinessplitstripr
   r   r   r   r   Z__get_cpu_nameY   s
   *
zCpuPercent.__get_cpu_namec                 C   s.   | j  rtjdd| _| j j| jd | jS )z6Update and/or return the CPU using the psutil library.        )intervalr!   )r   r"   r$   r   r(   r   r   r   r   r   Z	__get_cpub   s   
zCpuPercent.__get_cpuc                 C   s  | j  r~g | _ttjdddD ]l\}}|  |td|j d|j	|j
|jd}t|dr3|j|d< t|dr=|j|d< t|d	rG|j|d	< t|d
rQ|j|d
< t|dr[|j|d< t|dre|j|d< t|dro|j|d< | j| | j j| jd q| jS )z?Update and/or return the per CPU list using the psutil library.r3   T)r4   r   d   r.   )keyr   totalusersystemidleniceiowaitirqsoftirqstealguest
guest_nicer!   )r   r"   r   	enumerater$   Zcpu_times_percentr   roundr:   r8   r9   r#   r;   r<   r=   r>   r?   r@   rA   appendr(   r   )r   r   ZcputimesZcpur   r   r   Z__get_percpuk   s:   














zCpuPercent.__get_percpuN)r   )F)__name__
__module____qualname____doc__r   r   r   r*   r   r   r   r   r   r   r   r      s    

		r   )	rH   Zglances.loggerr   Zglances.timerr   r$   objectr   r   r   r   r   r   <module>   s   
s