o
    va-                     @   s   d Z ddlmZ ddlZddlmZ ddlmZmZ ddl	m
Z
 dZzddlmZ W n eyG   zddlmZ W n eyD   d	ZY nw Y nw G d
d deZdS )zManage the folder list.    )unicode_literalsN)Timer)range	nativestr)loggerT)scandirFc                   @   s   e Zd ZdZdZg 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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 )'
FolderLista  This class describes the optional monitored folder list.

    The folder list is a list of 'important' folder to monitor.

    The list (Python list) is composed of items (Python dict).
    An item is defined (dict keys):
    * path: Path to the folder
    * careful: optional careful threshold (in MB)
    * warning: optional warning threshold (in MB)
    * critical: optional critical threshold (in MB)
    
      c                 C   s\   || _ g | _d| _| j dur)| j dr)tr"td | d dS td dS g | _	dS )z?Init the folder list from the configuration file, if it exists.TNZfoldersz"Folder list configuration detectedzDScandir not found. Please use Python 3.5+ or install the scandir lib)
configtimer_folders
first_grabZhas_sectionscandir_tagr   debug_FolderList__set_folder_listerror_FolderList__folder_list)selfr    r   5/usr/lib/python3/dist-packages/glances/folder_list.py__init__@   s   

zFolderList.__init__c                 C   s@  t d| jd D ]}i }dt| d }t||d< | j||d |d< |d du r,qt|d |d< t| jj||d | jd|d< | j	t
|d  d	D ]F}| j||| ||< || durqtd
||d ||  | j||| d }|dur|||d < td||d ||d   qP| j	| qdS )zhInit the monitored folder list.

        The list is defined in the Glances configuration file.
           Zfolder__ZindicepathNZrefresh)default)carefulwarningcriticalz {} threshold for folder {} is {}Z_actionz{} action for folder {} is {})r   !_FolderList__folder_list_max_sizestrr   Z	get_valuer   int_FolderList__default_refreshr   appendr   r   r   formatr   )r   sectionlvaluekeyiactionr   r   r   Z__set_folder_listT   s0   
 zFolderList.__set_folder_listc                 C   
   t | jS N)r   r   r   r   r   r   __str__z      
zFolderList.__str__c                 C      | j S r+   r   r,   r   r   r   __repr__}   s   zFolderList.__repr__c                 C   s
   | j | S r+   r0   r   itemr   r   r   __getitem__   r.   zFolderList.__getitem__c                 C   r*   r+   )lenr   r,   r   r   r   __len__   r.   zFolderList.__len__c                 C   s8   |t | jk rz| j| | W S  ty   Y dS w dS )zkMeta function to return key value of item.

        Return None if not defined or item > len(list)
        N)r5   r   	Exception)r   r3   r'   r   r   r   __get__   s   zFolderList.__get__c              	   C   sr   d}t |D ]0}| r$|jdks|jdkr$|| tj||j7 }qz	|| j7 }W q t	y6   Y qw |S )zFReturn the size of the directory given by path

        path: <string>r   .z..)
r   is_dirname_FolderList__folder_sizeosr   joinstatst_sizeOSError)r   r   retfr   r   r   Z__folder_size   s   zFolderList.__folder_sizec                 C   s   t | jdkr
| jS tt |  D ]V}| js| j|  sqz| | || j| d< W n2 t	y` } z&t
d| || |jdkrOd| j| d< nd| j| d< W Y d}~nd}~ww | j|   qd| _| jS )	z%Update the command result attributed.r   sizez&Cannot get folder size ({}). Error: {}   !?NF)r5   r   r   getr   r   Zfinishedr<   r   rA   r   r   r#   errnoreset)r   r(   er   r   r   update   s"   
zFolderList.updatec                 C   r/   )z)Return the monitored list (list of dict).r0   r,   r   r   r   rH      s   zFolderList.getc                 C   s
   || _ dS )z&Set the monitored list (list of dict).Nr0   r   Znewlistr   r   r   set   s   
zFolderList.setc                 C   s   |   S r+   )rH   r,   r   r   r   getAll   s   zFolderList.getAllc                 C   s   |  | d S r+   )rN   rM   r   r   r   setAll   s   zFolderList.setAllc                 C      |  |dS )z*Return the path of the item number (item).r   r8   r2   r   r   r   r         zFolderList.pathc                 C   rQ   )z7Return the careful threshold of the item number (item).r   rR   r2   r   r   r   r      rS   zFolderList.carefulc                 C   rQ   )z7Return the warning threshold of the item number (item).r   rR   r2   r   r   r   r      rS   zFolderList.warningc                 C   rQ   )z8Return the critical threshold of the item number (item).r   rR   r2   r   r   r   r      rS   zFolderList.criticalN)__name__
__module____qualname____doc__r   r   r!   r   r   r-   r1   r4   r6   r8   r<   rL   rH   rN   rO   rP   r   r   r   r   r   r   r   r   r   +   s,    &r   )rW   Z
__future__r   r=   Zglances.timerr   Zglances.compatr   r   Zglances.loggerr   r   r   ImportErrorobjectr   r   r   r   r   <module>   s$   