a
    »ÏOc�  ã                   @   sl   d Z zddlZW n ey"   Y n0 ddd„Zddd„Zd	d
„ Zddd„Zdd„ Zdd„ ZG dd„ dƒZ	dS )z_
functions that allow you to generate CLI parameters from a confu schema for
argparse or click
é    Nú--c                 C   s   d  | | ¡ dd¡¡S )zÖ
    Returns a cli option name from attribute path

    **Arguments**

    - path (`list`): attribute path
    - delimiter (`str`): delimiter for nested attributes

    **Returns**

    cli option name (`str`)
    z--{}Ú_ú-)ÚformatÚjoinÚreplace©ÚpathÚ	delimiter© r   ú@/var/www/html/gsl.tools/lib/python3.9/site-packages/confu/cli.pyÚoption_name   s    r   Ú__c                 C   s   |  | ¡› S )zç
    Returns a cli option destination name from attribute path

    **Arguments**

    - path (`list`): attribute path
    - delimiter (`str`): delimiter for nested attributes

    **Returns**

    cli destination name (`str`)
    )r   r   r   r   r   Údestination_name   s    r   c                 C   s6   |r|s| S |}|D ]}||vr(|   S || }q|S ©Nr   )Úvaluer	   ÚdefaultsÚ	containerÚkeyr   r   r   Údefault-   s    
r   Tc                    s    ‡ ‡‡‡fdd„}|  |¡ dS )aÜ  
    Add cli options to an argparse ArgumentParser instance

    **Arguments**

    - parser (`argparse.ArgumentParser`)
    - schema (`Schema`)

    **Keyword Arguments**

    - defaults (`dict`): if specified will override defaults from here
    - attributes (`list<str>`): can hold a list of attribute names.
    if specified only matching attributes will be aded
    - default_from_schema (`bool`): determines if defaults for
    argparser should come from the schema
    c                    sÈ   ˆ j s
d S ˆrtˆƒˆvrd S ‡ ‡fdd„ˆ jtˆƒdœ}ˆrRtˆ jˆˆƒ|d< ntd ˆˆƒ|d< tˆdd�}tˆ dd ƒ}|rˆ|||ƒ}|d d ur²ˆ jr²d |d	 |d ¡|d	< ˆj|fi |¤Ž d S )
Nc                    s   ˆ   | ˆ¡S r   ©Úvalidate)Úx©Ú	attributer	   r   r   Ú<lambda>U   ó    z5argparse_options.<locals>.optionize.<locals>.<lambda>)ÚtypeÚhelpÚdestr   Ú.)r
   Zfinalize_argparseú{} ({})r   )	Úclir   r   r   r   ÚgetattrÚcli_show_defaultr   Úadd_argument)r   r	   ÚkwargsÚnameÚfinalize©Ú
attributesÚdefault_from_schemar   Úparserr   r   Ú	optionizeM   s$    ý
z#argparse_options.<locals>.optionizeN)Úwalk)r,   Úschemar   r*   r+   r-   r   r)   r   Úargparse_options8   s    r0   c                 C   s   | j D ]}t|| |ƒ q|S )a  
    Takes the output of a parser and applies it to a Config object.

    **Arguments**

    - args (`argparse.Namespace`):  the result of parser.parse_args()
    - config (`Config`): the config object

    **Returns**

    - config (`Config`): now updated with args
    )Ú__dict__Ú	apply_arg)ÚargsÚconfigÚkr   r   r   Úapply_argparsem   s    
r6   c                 C   sð   ddl m}m} |j}|  d¡}t|| ƒ}t|ƒdkrÊ|j}|j}	|D ]J}
t	|	 
|
¡|ƒrh|	 
|
¡j}	qFt	|	 
|
¡|ƒrF|	 
|
¡}|du rF dS qF|dd… D ]}
|
|vr²i ||
< ||
 }qž|||d < n"|j 
| ¡}|du râdS ||j| < dS )aA  
    Function for applying arguments to a config. Applies to nested
    configs as well.

    **Arguments**

    - original_key (`str`): the name of the argument in the argparse Namespace
    - args (`argparse.Namespace`): the entire Namespace, ie the result of the parser
    - config (`Config`): the config object

    r   )Ú	AttributeÚSchemar   é   Néÿÿÿÿ)Zconfu.schemar7   r8   Z_schemaÚsplitr#   ÚlenÚdataZ_attrÚ
isinstanceÚget)Zoriginal_keyr3   r4   r7   r8   r/   r	   Zarg_datar=   Zcurrent_schemar   r   r   r   r   r2   �   s.    



r2   c                   @   s"   e Zd ZdZddd„Zdd„ ZdS )Úclick_optionsa[  
    Add cli options to a click decorated function

    Use like a decorator

    **Arguments**

    - schema (`Schema`)

    **Keyword Arguments**

    - defaults (`dict`): if specified will override defaults from here
    - attributes (`list<str>`): can hold a list of attribute names.
    if specified only matching attributes will be aded
    Nc                 C   s   || _ || _|| _d S r   )r/   r   r*   )Úselfr/   r   r*   r   r   r   Ú__init__Ì   s    zclick_options.__init__c                    s:   dˆi‰| j ‰| j‰ ‡ ‡‡‡fdd„}| j |¡ ˆd S )NÚfnc                    sÈ   ˆ j s
d S ˆrtˆƒˆvrd S ‡ ‡fdd„}ˆ jj|_tj |¡ˆ jtˆ jˆˆƒdœ}t	ˆƒ}t
ˆ dd ƒ}|rx|||ƒ}|d d ur¢ˆ jr¢d |d |d ¡|d< tj|tˆƒfi |¤Ž ˆ¡ˆd< d S )	Nc                    s   ˆ   | ˆ¡S r   r   )r   r   r   r   Úvalidate_and_convertÝ   s    zGclick_options.__call__.<locals>.optionize.<locals>.validate_and_convert)r   r   r   Zfinalize_clickr   r!   r   rC   )r"   r   Ú	__class__Ú__name__ÚclickÚtypesZFuncParamTyper   r   r   r#   r$   r   ÚoptionÚ__call__)r   r	   rD   r&   r'   r(   ©r*   r   r   rC   r   r   r-   Ö   s.    

ý
ÿÿþz)click_options.__call__.<locals>.optionize)r   r*   r/   r.   )rA   rC   r-   r   rK   r   rJ   Ñ   s    zclick_options.__call__)NN)rF   Ú
__module__Ú__qualname__Ú__doc__rB   rJ   r   r   r   r   r@   º   s   
r@   )r   )r   )NNT)
rN   rG   ÚImportErrorr   r   r   r0   r6   r2   r@   r   r   r   r   Ú<module>   s   

 ÿ
59