a
    »ÏOc¨  ã                   @   s,   d Z ddlZdd„ Zeƒ ZG dd„ dƒZdS )z
utility functions / classes
é    Nc                    s   ‡ fdd„ˆ   ¡ D ƒS )zÌ
    Takes a configparser.ConfigParser instance and returns a dict
    of sections and their keys and values

    **Arguments**

    - config (`configparser.ConfigParsers`)

    **Returns**

    dict
    c                    s   i | ]}|t ˆ  |¡ƒ“qS © )ÚdictÚitems)Ú.0Ús©Úconfigr   úA/var/www/html/gsl.tools/lib/python3.9/site-packages/confu/util.pyÚ
<dictcomp>   ó    z&config_parser_dict.<locals>.<dictcomp>)Úsectionsr   r   r   r	   Úconfig_parser_dict   s    r   c                   @   s>   e Zd ZdZdd„ Zefdd„Zddd„Zd	d
„ Zdd„ Z	dS )ÚSettingsManagerzO
    Scoped settings management with environment variable override support
    c                 C   s
   || _ dS )z6
        Arguments:

        - scope (`dict`)
        N©Úscope)Úselfr   r   r   r	   Ú__init__    s    zSettingsManager.__init__c                 C   s.   |t u r|tjvrdS tj ||¡| j|< dS )zÕ
        Sets a scope variable from a environment variable of the same name.

        This is useful to leave the option unset and use default if it
        already exists in the scope (which may change).
        N)Ú_DEFAULT_ARGÚosÚenvironÚgetr   )r   ÚnameÚdefaultr   r   r	   Úset_from_env*   s    zSettingsManager.set_from_envNc                 C   sv   t |tƒr|  ||¡S |dur(t|ƒ}n|du r@td|› d�ƒ‚|tjv rftj |¡}||ƒ| j|< n|  	||¡ dS )aš  
        Sets an option, first checking for env vars,
        then checking for value already set,
        then going to the default value if passed.
        Environment variables are always strings, but
        we try to coerce them to the correct type first by checking
        the type of the default value provided. If the default
        value is None, then we check the optional envvar_type arg.
        Nz0If no default value is provided for the setting z& the envvar_type argument must be set.)
Ú
isinstanceÚboolÚset_boolÚtypeÚ
ValueErrorr   r   r   r   Úset_default)r   r   ÚvalueZenvvar_typeÚenv_varr   r   r	   Ú
set_option6   s    


ÿ
zSettingsManager.set_optionc                 C   sl   |t jv r\t j |¡ ¡ }|dv r.d| j|< n.|dv rBd| j|< nt|› dt j| › d�ƒ‚|  ||¡ dS )z}Sets and option, first checking for env vars, then checking for value already set, then going to the default value if passed.)Ú1ÚtrueÚyÚyesT)Ú0ÚfalseÚnÚnoFz is a boolean, cannot match 'ú'N)r   r   r   Úlowerr   r   r   )r   r   r    Zenvvalr   r   r	   r   X   s    
ÿzSettingsManager.set_boolc                 C   s   || j vr|| j |< dS )z>Sets the default value for the option if it's not already set.Nr   )r   r   r    r   r   r	   r   f   s    
zSettingsManager.set_default)N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r"   r   r   r   r   r   r	   r      s   

"r   )r0   r   r   Úobjectr   r   r   r   r   r	   Ú<module>   s   