o
    if|"                     @   sZ  d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ ejZ	e	dkr0d dl
mZmZ eZn
d dlmZmZ eZe	dkrCd	d
 Zndd
 ZdZh dZh dZdd ZG dd deZG dd deZe ZG dd deZG dd deZG dd deZG dd deZG dd deZG dd  d eZ e  Z!G d!d" d"eZ"G d#d$ d$eZ#dS )%    N)shlex)open)OrderedDict)   r   r   )ConfigParserNoOptionError)SafeConfigParserr   )r      r   c                 C   
   |  |S N)	read_fileparserfile r   A/var/www/html/corbot_env/lib/python3.10/site-packages/decouple.py<lambda>      
 r   c                 C   r
   r   )readfpr   r   r   r   r      r   zUTF-8>   1tyonyestrue>   0fnnoofffalsec                 C   s:   t | tr| S |  } | tv rdS | tv rdS td|  )NTFzInvalid truth value: )
isinstanceboollowerTRUE_VALUESFALSE_VALUES
ValueErrorvaluer   r   r   	strtobool!   s   
r)   c                   @   s   e Zd ZdS )UndefinedValueErrorN)__name__
__module____qualname__r   r   r   r   r*   .   s    r*   c                   @   s   e Zd ZdZdS )	Undefinedz,
    Class to represent undefined type.
    N)r+   r,   r-   __doc__r   r   r   r   r.   2   s    r.   c                   @   sB   e Zd ZdZdd Zdd Zedd Zeefdd	Z	d
d Z
dS )Configz2
    Handle .env file format used by Foreman.
    c                 C   s
   || _ d S r   )
repository)selfr1   r   r   r   __init__B      
zConfig.__init__c                 C   s$   t |}|dkrt|S tt|S )zP
        Helper to convert config values to boolean as ConfigParser do.
         )strr"   r)   )r2   r(   r   r   r   _cast_booleanE   s   zConfig._cast_booleanc                 C   s   | S r   r   r'   r   r   r   _cast_do_nothingL   s   zConfig._cast_do_nothingc                 C   sv   |t jv rt j| }n|| jv r| j| }nt|tr"td||}t|tr0| j}||S |tu r7| j	}||S )zD
        Return the value for option or default if defined.
        z={} not found. Declare it as envvar or define a default value.)
osenvironr1   r!   r.   r*   formatr8   r"   r7   )r2   optiondefaultcastr(   r   r   r   getP   s   



z
Config.getc                 O   s   | j |i |S )z-
        Convenient shortcut to get.
        )r?   r2   argskwargsr   r   r   __call__g   s   zConfig.__call__N)r+   r,   r-   r/   r3   r7   staticmethodr8   	undefinedr?   rC   r   r   r   r   r0   =   s    
r0   c                   @   s*   e Zd ZdefddZdd Zdd ZdS )	RepositoryEmptyr5   c                 C      d S r   r   )r2   sourceencodingr   r   r   r3   o      zRepositoryEmpty.__init__c                 C   s   dS )NFr   r2   keyr   r   r   __contains__r   rJ   zRepositoryEmpty.__contains__c                 C   rG   r   r   rK   r   r   r   __getitem__u   rJ   zRepositoryEmpty.__getitem__N)r+   r,   r-   DEFAULT_ENCODINGr3   rM   rN   r   r   r   r   rF   n   s    rF   c                   @   s0   e Zd ZdZdZefddZdd Zdd Zd	S )
RepositoryIniz0
    Retrieves option keys from .ini files.
    settingsc                 C   sF   t  | _t||d}t| j| W d    d S 1 sw   Y  d S )NrI   )r   r   r   read_config)r2   rH   rI   file_r   r   r   r3      s   "zRepositoryIni.__init__c                 C   s   |t jv p| j| j|S r   )r9   r:   r   
has_optionSECTIONrK   r   r   r   rM      s   
zRepositoryIni.__contains__c                 C   s*   z	| j | j|W S  ty   t|w r   )r   r?   rV   r   KeyErrorrK   r   r   r   rN      s
   zRepositoryIni.__getitem__N)	r+   r,   r-   r/   rV   rO   r3   rM   rN   r   r   r   r   rP   y   s    rP   c                   @   s,   e Zd ZdZefddZdd Zdd ZdS )	RepositoryEnvzM
    Retrieves option keys from .env files with fall back to os.environ.
    c                 C   s   i | _ t||dW}|D ]K}| }|r|dsd|vrq|dd\}}| }| }t|dkrR|d dkr@|d dksL|d d	krR|d d	krR|dd }|| j |< qW d    d S 1 scw   Y  d S )
NrR   #=   r	   r   '")datar   strip
startswithsplitlen)r2   rH   rI   rT   linekvr   r   r   r3      s   <"zRepositoryEnv.__init__c                 C      |t jv p	|| jv S r   r9   r:   r_   rK   r   r   r   rM         zRepositoryEnv.__contains__c                 C   
   | j | S r   r_   rK   r   r   r   rN      r4   zRepositoryEnv.__getitem__N)r+   r,   r-   r/   rO   r3   rM   rN   r   r   r   r   rX      s
    rX   c                   @   s*   e Zd ZdZd
ddZdd Zdd Zd	S )RepositorySecretz
    Retrieves option keys from files,
    where title of file is a key, content of file is a value
    e.g. Docker swarm secrets
    /run/secrets/c              	   C   s`   i | _ t|}|D ]#}ttj||d}| | j |< W d    n1 s(w   Y  q
d S )Nr)r_   r9   listdirr   pathjoinread)r2   rH   lsr   r   r   r   r   r3      s   
zRepositorySecret.__init__c                 C   rg   r   rh   rK   r   r   r   rM      ri   zRepositorySecret.__contains__c                 C   rj   r   rk   rK   r   r   r   rN      r4   zRepositorySecret.__getitem__N)rm   )r+   r,   r-   r/   r3   rM   rN   r   r   r   r   rl      s
    
rl   c                   @   sR   e Zd ZdZedefdefgZeZ	dddZ
dd Zd	d
 Zdd Zdd ZdS )
AutoConfigz
    Autodetects the config file and type.

    Parameters
    ----------
    search_path : str, optional
        Initial search path. If empty, the default search path is the
        caller's path.

    zsettings.iniz.envNc                 C   s   || _ d | _d S r   )search_pathconfig)r2   ru   r   r   r   r3      s   
zAutoConfig.__init__c                 C   sn   | j D ]}tj||}tj|r|  S qtj|}|r5tj|tjtjtjkr5| 	|S dS )Nr5   )
	SUPPORTEDr9   rp   rq   isfiledirnamenormcaseabspathsep
_find_file)r2   rp   
configfilefilenameparentr   r   r   r}      s   
&
zAutoConfig._find_filec                 C   s\   z|  tj|}W n ty   d}Y nw | jtj|t}t	||| j
d| _d S )Nr5   rR   )r}   r9   rp   r{   	Exceptionrw   r?   basenamerF   r0   rI   rv   )r2   rp   r   
Repositoryr   r   r   _load   s   zAutoConfig._loadc                 C   s    t  }tj|jjjj}|S r   )sys	_getframer9   rp   ry   f_backf_codeco_filename)r2   framerp   r   r   r   _caller_path   s   zAutoConfig._caller_pathc                 O   s*   | j s| | jp|   | j |i |S r   )rv   r   ru   r   r@   r   r   r   rC      s   zAutoConfig.__call__r   )r+   r,   r-   r/   r   rP   rX   rw   rO   rI   r3   r}   r   r   rC   r   r   r   r   rt      s    


rt   c                   @   s,   e Zd ZdZedejefddZdd Z	dS )CsvzK
    Produces a csv parser that return a list of transformed elements.
    ,c                 C   s   || _ || _|| _|| _dS )ai  
        Parameters:
        cast -- callable that transforms the item just before it's added to the list.
        delimiter -- string of delimiters chars passed to shlex.
        strip -- string of non-relevant characters to be passed to str.strip after the split.
        post_process -- callable to post process all casted values. Default is `list`.
        N)r>   	delimiterr`   post_process)r2   r>   r   r`   r   r   r   r   r3     s   
zCsv.__init__c                    sN   |du r   S  fddt|dd} j|_d|_  fdd|D S )zThe actual transformationNc                    s     |  jS r   )r>   r`   )s)r2   r   r   r     s    zCsv.__call__.<locals>.<lambda>T)posixc                 3   s    | ]} |V  qd S r   r   ).0r   )	transformr   r   	<genexpr>  s    zCsv.__call__.<locals>.<genexpr>)r   r   r   
whitespacewhitespace_split)r2   r(   splitterr   )r2   r   r   rC     s   zCsv.__call__N)
r+   r,   r-   r/   	text_typestringr   listr3   rC   r   r   r   r   r     s    r   c                   @   s(   e Zd ZdZdedfddZdd ZdS )ChoiceszD
    Allows for cast and validation based on a list of choices.
    Nc                 C   sJ   |pg | _ || _|pg | _g | _| j| j  | jdd | jD  dS )z
        Parameters:
        flat -- a flat list of valid choices.
        cast -- callable that transforms value before validation.
        choices -- tuple of Django-like choices.
        c                 S   s   g | ]\}}|qS r   r   )r   r(   _r   r   r   
<listcomp>3  s    z$Choices.__init__.<locals>.<listcomp>N)flatr>   choices_valid_valuesextend)r2   r   r>   r   r   r   r   r3   &  s   

zChoices.__init__c                 C   s*   |  |}|| jvrtd|| j|S )Nz.Value not in list: {!r}; valid values are {!r})r>   r   r&   r;   )r2   r(   r   r   r   r   rC   5  s   


zChoices.__call__)r+   r,   r-   r/   r   r3   rC   r   r   r   r   r   !  s    r   )$r9   r   r   r   ior   collectionsr   version_info	PYVERSIONconfigparserr   r   r6   r   r   unicoderS   rO   r$   r%   r)   r   r*   objectr.   rE   r0   rF   rP   rX   rl   rt   rv   r   r   r   r   r   r   <module>   s>   
1> 