o
    Ìv›a»0  ã                   @   s¤   d Z ddlZddlZddlZddlmZ ddlZddlmZm	Z	m
Z
mZ ddlmZmZmZmZmZ ddlmZ dd„ Zd	d
„ Zdd„ Zdd„ ZG dd„ deƒZdS )zManage the configuration file.é    N)Úopen)ÚConfigParserÚNoOptionErrorÚNoSectionErrorÚsystem_exec)ÚBSDÚLINUXÚMACOSÚSUNOSÚWINDOWS)Úloggerc                  C   s^   t r	tj d¡} ntrtj d¡} ntj d¡ptj d¡} | du r&d} | S tj | d¡} | S )z±Return the per-user config dir (full path).

    - Linux, *BSD, SunOS: ~/.config/glances
    - macOS: ~/Library/Application Support/glances
    - Windows: %APPDATA%\glances
    ÚAPPDATAz~/Library/Application SupportZXDG_CONFIG_HOMEz	~/.configNÚ Úglances)r   ÚosÚenvironÚgetr	   ÚpathÚ
expanduserÚjoin©r   © r   ú0/usr/lib/python3/dist-packages/glances/config.pyÚuser_config_dir!   s   þr   c                  C   sf   t rtj tj d¡ptj d¡dd¡} | S tr tj d¡} | S tj tj d¡p.tj d¡d¡} | S )z¹Return the per-user cache dir (full path).

    - Linux, *BSD, SunOS: ~/.cache/glances
    - macOS: ~/Library/Caches/glances
    - Windows: {%LOCALAPPDATA%,%APPDATA%}\glances\cache
    ZLOCALAPPDATAr   r   Úcachez~/Library/Caches/glancesZXDG_CACHE_HOMEz~/.cache)r   r   r   r   r   r   r	   r   r   r   r   r   Úuser_cache_dir6   s   ÿúýÿr   c                  C   sJ   t strd} ntstrd} ntj d¡} | du rd} | S tj | d¡} | S )z Return the system-wide config dir (full path).

    - Linux, SunOS: /etc/glances
    - *BSD, macOS: /usr/local/etc/glances
    - Windows: %APPDATA%\glances
    z/etcz/usr/local/etcr   Nr   r   ©	r   r
   r   r	   r   r   r   r   r   r   r   r   r   Úsystem_config_dirI   s   þr   c                  C   sD   t stststrd} ntj d¡} | du rd} | S tj | d¡} | S )z¨Return the system-wide config dir (full path).

    - Linux, SunOS, *BSD, macOS: /usr/share/doc (as defined in the setup.py files)
    - Windows: %APPDATA%\glances
    z/usr/share/docr   Nr   r   r   r   r   r   r   Údefault_config_dir^   s   þr   c                   @   s¢   e Zd ZdZd%dd„Zdd„ Zdd„ Zd	d
„ Zedd„ ƒZ	dd„ Z
dd„ Zdd„ Zdd„ Zdg d¢fdd„Zdd„ Z	d%dd„Zd&dd„Zd'd d!„Zd(d#d$„ZdS ))ÚConfigzžThis class is used to access/read config file, if it exists.

    :param config_dir: the path to search for config file
    :type config_dir: str or None
    Nc                 C   sV   || _ d| _d | _t d¡| _ztd d| _W n ty$   tƒ | _Y nw |  	¡  d S )Nzglances.confz	(\`.+?\`))Úinterpolation)
Ú
config_dirÚconfig_filenameÚ_loaded_config_fileÚreÚcompileÚ
re_patternr   ÚparserÚ	TypeErrorÚread)Úselfr!   r   r   r   Ú__init__x   s   ÿzConfig.__init__c                 C   sb   g }| j r| | j ¡ | tj tƒ | j¡¡ | tj tƒ | j¡¡ | tj tƒ | j¡¡ |S )aº  Get a list of config file paths.

        The list is built taking into account of the OS, priority and location.

        * custom path: /path/to/glances
        * Linux, SunOS: ~/.config/glances, /etc/glances
        * *BSD: ~/.config/glances, /usr/local/etc/glances
        * macOS: ~/Library/Application Support/glances, /usr/local/etc/glances
        * Windows: %APPDATA%\glances

        The config file will be searched in the following order of priority:
            * /path/to/file (via -C flag)
            * user's home directory (per-user settings)
            * system-wide directory (system-wide settings)
            * default pip directory (as defined in the setup.py file)
        )	r!   Úappendr   r   r   r   r"   r   r   )r*   Úpathsr   r   r   Úconfig_file_paths‡   s   zConfig.config_file_pathsc                 C   sÚ   |   ¡ D ]b}t d |¡¡ tj |¡rfz,t|dd}| j 	|¡ | j 
|¡ W d  ƒ n1 s2w   Y  t d |¡¡ W n  ty` } zt d ||¡¡ t d¡ W Y d}~nd}~ww || _ nq|  ¡  dS )z=Read the config file, if it exists. Using defaults otherwise.zSearch glances.conf file in {}zutf-8)ÚencodingNzRead configuration file '{}'z(Can not read configuration file '{}': {}é   )r.   r   ÚdebugÚformatr   r   Úexistsr   r'   Z	read_filer)   ÚinfoÚUnicodeDecodeErrorÚerrorÚsysÚexitr#   Úsections_set_default)r*   Zconfig_fileÚfÚerrr   r   r   r)   £   s$   þ€þõzConfig.readc              	   C   s¾  | j  d¡s| j  d¡ |  ddd¡ | j  d¡s| j  d¡ |  ddd¡ | j  d¡s2| j  d¡ |  dd¡ |  dd¡ |  dd	¡ | j  d¡sP| j  d¡ |  dd
¡ |  dd¡ |  dd¡ dt ¡  d }|  ddt||d  ƒt||d  ƒt|ƒg¡ dt ¡  }|  ddt||d  ƒt||d  ƒt|ƒg¡ | j  d¡s¬| j  d¡ |  dd
¡ |  dd¡ | j  d¡sÄ| j  d¡ | jdg d¢d | j  d¡sÙ| j  d¡ |  d¡ | j  d¡sê| j  d¡ |  d¡ | j  d¡sû| j  d¡ |  dd¡ |  dd¡ | j  d¡s| j  d¡ |  d¡ | j  d¡s&| j  d¡ | jddg d¢d | jdd g d!¢d | jdd"g d#¢d | j  d$¡sQ| j  d$¡ |  d$d¡ |  d$d¡ d S )%NÚglobalZstrftime_formatr   Zcheck_updateZfalseZ	quicklookZcpuZmemZswapÚuserÚsystemZstealg      ð?g      Y@Ziowaitgš™™™™™É?gš™™™™™¹?g     jè@Zctx_switchesZpercpuÚload)z0.7z1.0z5.0)ÚcwcZmemswapZnetworkZrxZtxZfsZsensorsZtemperature_core)Ú60Ú70Ú80Ztemperature_hdd)Z45Z52rA   Zbattery)rC   Ú90Z95Zprocesslist)r'   Úhas_sectionZadd_sectionÚset_defaultÚset_default_cwcÚmultiprocessingÚ	cpu_countÚstr)r*   Ziowait_bottleneckZctx_switches_bottleneckr   r   r   r9   ·   sv   þÿþÿ


zConfig.sections_set_defaultc                 C   s   | j S )z%Return the loaded configuration file.)r#   ©r*   r   r   r   Úloaded_config_file	  s   zConfig.loaded_config_filec                 C   sH   i }| j  ¡ D ]}i ||< | j  |¡D ]}| j  ||¡|| |< qq|S )z"Return the configuration as a dict)r'   ÚsectionsÚoptionsr   )r*   Z
dictionaryÚsectionÚoptionr   r   r   Úas_dict  s   ÿzConfig.as_dictc                 C   s
   | j  ¡ S )zReturn a list of all sections.)r'   rM   rK   r   r   r   rM     s   
zConfig.sectionsc                 C   ó   | j  |¡S )z#Return the items list of a section.)r'   Úitems©r*   rO   r   r   r   rS     ó   zConfig.itemsc                 C   rR   )z-Return info about the existence of a section.)r'   rE   rT   r   r   r   rE     rU   zConfig.has_section)Z50rB   rD   c                 C   s\   |du rd}n|d }|   ||d |d ¡ |   ||d |d ¡ |   ||d |d	 ¡ dS )
z5Set default values for careful, warning and critical.Nr   Ú_Zcarefulr   Zwarningr0   Zcriticalé   )rF   )r*   rO   Zoption_headerr@   Úheaderr   r   r   rG   #  s   zConfig.set_default_cwcc                 C   s&   | j  ||¡s| j  |||¡ dS dS )z4If the option did not exist, create a default value.N)r'   Z
has_optionÚset©r*   rO   rP   Údefaultr   r   r   rF   /  s   ÿzConfig.set_defaultc              	   C   s„   |}z	| j  ||¡}W n ttfy   Y nw |dur@z| j |¡}|D ]}| |t|dd… ƒ¡}q$W |S  ty?   Y |S w |S )a  Get the value of an option, if it exists.

        If it did not exist, then return the default value.

        It allows user to define dynamic configuration key (see issue#1204)
        Dynamic value should starts and end with the ` char
        Example: prefix=`hostname`
        Nr0   éÿÿÿÿ)	r'   r   r   r   r&   ÚfindallÚreplacer   r(   )r*   rO   rP   r[   ÚretÚmatchÚmr   r   r   Ú	get_value5  s$   
ÿÿþþzConfig.get_valuer   c              	   C   ó0   z| j  ||¡W S  ttfy   t|ƒ Y S w )z-Get the int value of an option, if it exists.)r'   Zgetintr   r   ÚintrZ   r   r   r   Úget_int_valueO  ó
   ÿzConfig.get_int_valueç        c              	   C   rc   )z/Get the float value of an option, if it exists.)r'   Zgetfloatr   r   ÚfloatrZ   r   r   r   Úget_float_valueV  rf   zConfig.get_float_valueTc              	   C   rc   )z.Get the bool value of an option, if it exists.)r'   Z
getbooleanr   r   ÚboolrZ   r   r   r   Úget_bool_value]  rf   zConfig.get_bool_value)N)r   )rg   )T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r+   r.   r)   r9   ÚpropertyrL   rQ   rM   rS   rE   rG   rF   rb   re   ri   rk   r   r   r   r   r   p   s*    
R
	
þ
ÿ

r   )ro   r   r7   rH   Úior   r$   Zglances.compatr   r   r   r   Zglances.globalsr   r   r	   r
   r   Zglances.loggerr   r   r   r   r   Úobjectr   r   r   r   r   Ú<module>   s   