o
    WôjUC  ã                   @   sX  d dl Z d dlmZ d dlZd dlmZmZmZ d dlZd dl	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 d dlZi Zi ai ZdadZdZed$d	d
„ƒZd%dd„Zddddœdeeef fdd„ZeZG dd„ dƒZdd„ Z dd„ Z!dd„ Z"G dd„ dƒZ#G dd„ dƒZ$G dd„ dƒZ%d&deeef fd d!„Z&d'deeef fd"d#„Z'dS )(é    N)Úcontextmanager)ÚListÚDictÚUnion)ÚPath)ÚwrapsFé<   Ú c                 c   s–   � |   ¡ } g }i }t}t}| D ]}d|v r.| d¡d |kr)| | || d¡d < | |¡ q|  |¡ |D ]}|  |¡ q6| a|adV  |a|adS )zD
    Context manager to put parsed arguments into 
    a state.
    ú/r   éÿÿÿÿN)ÚcopyÚARGSÚPATTERNÚsplitÚappendÚupdateÚpop)Zparsed_argsÚpatternZremove_keysÚmatchedÚold_argsZold_patternÚkey© r   ú`/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/argbind/argbind.pyÚscope   s(   €
€
r   c              	   C   sl   | › d�g}|d ur|  d|› �¡ | ¡ D ]\}}|  d|› dt|ƒj› d|› �¡ q|  d¡ d |¡S )Nú(z  # scope = z  z : z = ú)Ú
)r   ÚitemsÚtypeÚ__name__Újoin)Ú	func_nameÚfunc_kwargsr   Ú	formattedr   Úvalr   r   r   Ú_format_func_debug6   s   $

r%   Údefault)Úwithout_prefixÚ
positionalÚgroupr)   c                    s”   |rt |d tƒs|d }t|ƒdkr|dd… ng ‰nd}|‰ˆr/ˆr/t dˆ› d�¡ g ‰t ˆ tƒr7ˆ g‰ ‡ ‡‡‡fdd„}|du rF|S ||ƒS )a›  Binds a functions arguments so that it looks up argument
    values in a dictionary scoped by ArgBind.

    Parameters
    ----------
    args : List[str] or [fn or Object] + List[str], optional
        List of patterns to bind the function under. If the first item
        in the list is a function or Object, then the function is bound
        here (e.g. decorate is called on the first argument). Otherwise,
        it is treated is a decorator.
    without_prefix : bool, optional
        Whether or not to bind without the function name as the prefix. 
        If True, the functions arguments will be available at "arg_name"
        rather than "func_name.arg_name", by default False
    positional : bool, optional
        Arguments that are not keyword arguments are not bound by default. If
        this is True, then the arguments will be bound as positional arguments
        in some order, by default False
    r   é   Nz_Combining positional arguments with scoping patterns is not allowed. Removing scoping patterns zi. 
See https://github.com/pseeth/argbind/tree/main/examples/hello_world#argbind-with-positional-argumentsc                    s’   | ‰ t  ˆ ¡}|rtˆ dƒ‰ ˆ j‰dˆv rˆ d¡d ‰ˆtv r'tˆ d ‰ n	ˆ ˆˆˆˆftˆ< tˆ ƒ‡ ‡‡‡fdd„ƒ}|rGt| d|ƒ | }|S )NÚ__init__Ú.r   c                     sh  t t ˆ¡j ¡ ƒ‰ i }‡ fdd„t| ƒD ƒ}ˆ D ]B\}}|j}|tjjus(ˆr[ˆs1ˆ› d|› �n|› }|t	v r[||vr[t	| }||v rH|| }|||< |}t
rWt
› d|› �}|t|< q| |¡ g }	t| ƒD ]\}
}ˆ |
 d }||vrz|	 |¡ qgi }ˆ D ]\}}||v r�|| ||< q|}dt	vr˜dt	d< t	d sžtr­t
r£t
}nd }ttˆ||ƒƒ ˆ|	i |¤ŽS )Nc                    s   i | ]\}}ˆ | d  |“qS )r   r   )Ú.0ÚiÚarg©Ú
parametersr   r   Ú
<dictcomp>z   ó    z=bind.<locals>.decorator.<locals>.cmd_func.<locals>.<dictcomp>r,   r
   r   ú
args.debugF)ÚlistÚinspectÚ	signaturer1   r   Ú	enumerater&   Ú	ParameterÚemptyr   r   Ú	USED_ARGSr   r   ÚDEBUGÚprintr%   )ÚargsÚkwargsZ
cmd_kwargsZ
pos_kwargsr   r$   Úarg_valÚarg_nameÚuse_keyZcmd_argsr.   r/   Zordered_kwargsÚkÚ_r   )Úfuncr(   Úprefixr'   r0   r   Úcmd_funcu   sJ   €

€€z)bind.<locals>.decorator.<locals>.cmd_func)r6   ÚisclassÚgetattrÚ__qualname__r   ÚPARSE_FUNCSr   Úsetattr)Zobject_or_funcÚis_classrG   ©r)   Úpatternsr(   r'   )rE   rF   r   Ú	decoratord   s    

+zbind.<locals>.decorator)Ú
isinstanceÚstrÚlenÚwarningsÚwarn)r'   r(   r)   r>   Zbound_fn_or_clsrP   r   rN   r   Úbind?   s    
ÿ
CrV   c                   @   s   e Zd Zdd„ dœdd„ZdS )Úbind_modulec                 C   ó   dS ©NTr   )Úfnr   r   r   Ú<lambda>±   ó    zbind_module.<lambda>)Ú	filter_fnc                O   s^   t |ƒD ](}t||ƒ}t|ttƒƒs,t|dƒr,||ƒr,t|g|¢R i |¤Ž}t| ||ƒ qdS )a  Binds every function/class in a specified module. The output
        class is a bound version of the original module, with the 
        attributes in the same place.

        Parameters
        ----------
        module : ModuleType
            Module or object whose attributes to bind.
        scopes : List[str] or [fn or Object] + List[str], optional
            List of patterns to bind the function under.
        filter_fn : Callable, optional
            A function that takes in the function that is to be bound, and 
            returns a boolean as to whether or not it should be bound.
            Defaults to always True, no matter what the function is.
        kwargs : keyword arguments, optional
            Keyword arguments to the bind function.

        rJ   N)ÚdirrI   rQ   r   ÚsysÚhasattrrV   rL   )ÚselfÚmoduler]   Úscopesr?   Úfn_namerZ   Zbound_fnr   r   r   r+   ±   s   
€ûzbind_module.__init__N)r   Ú
__module__rJ   r+   r   r   r   r   rW   °   s    rW   c                   C   s   t S )z
    Gets the args that have been used so far
    by the script (e.g. their function they target
    was actually called).
    )r;   r   r   r   r   Úget_used_argsË   s   rf   c           	      C   sÚ   t |ƒ}tj|jdd� t|dƒ�R}dd„ tj_tj| tjd�}d}g }| 	d¡D ])}| 	d	¡d
  
¡ }| d¡sM||krD|rDd|› �}| 	d	¡d
  
¡ }| |¡ q)| d |¡¡ W d  ƒ dS 1 sfw   Y  dS )z5
    Dumps the provided arguments to a
    file.
    T)Úexist_okÚwc                  W   rX   rY   r   )r>   r   r   r   r[   Û   r\   zdump_args.<locals>.<lambda>)ÚDumperNr   r,   r   ú-)r   ÚosÚmakedirsÚparentÚopenÚyamlri   Úignore_aliasesÚdumpr   ÚstripÚ
startswithr   Úwriter    )	r>   Zoutput_pathÚpathÚfÚxÚ	prev_lineÚoutputÚlineÚcur_liner   r   r   Ú	dump_argsÓ   s    

"ôr|   c                 C   s‚  t | ttfƒr%t| dƒ�}tj|tjd�}W d  ƒ n1 sw   Y  ntj| tjd�}d|v rK| d¡}i }|D ]	}| t	|ƒ¡ q:| |¡ |}t
j ¡ }d|v r\| | d¡¡ | ¡ D ]V\}}t |tƒr| d¡r~|dd… }	|	|v r~||	 ||< q`t |tƒr¶g }
|D ])}t |tƒr¬| d¡r¬|dd… }	|	|v r¦|
 ||	 ¡ qˆ|
 |¡ qˆ|
 |¡ qˆ|
||< q`d|vr¿t|d< |S )	zb
    Loads arguments from a given input path or file stream, if
    the file is already open.
    Úr)ÚLoaderNz$includez$varsú$r*   r4   )rQ   rR   r   rn   ro   Úloadr~   r   r   Ú	load_argsrk   Úenvironr   r   rs   r5   r   r<   )Zinput_path_or_streamrv   ÚdataZinclude_filesZinclude_argsZinclude_fileZ_varsr   r$   ÚlookupZnew_listÚsubvalr   r   r   r�   è   sJ   ÿ€




€
€r�   c                   @   ó   e Zd Zdd„ Zdd„ ZdS )Ústr_to_listc                 C   ó
   || _ d S ©N©Ú_type)ra   r‹   r   r   r   r+     ó   
zstr_to_list.__init__c                    s    |  d¡}‡ fdd„|D ƒ}|S )Nú c                    s   g | ]}ˆ   |¡‘qS r   rŠ   )r-   Úv©ra   r   r   Ú
<listcomp>  s    z(str_to_list.__call__.<locals>.<listcomp>)r   ©ra   ÚvaluesÚ_valuesr   r�   r   Ú__call__  s   
zstr_to_list.__call__N©r   re   rJ   r+   r”   r   r   r   r   r‡     ó    r‡   c                   @   r†   )Ústr_to_tuplec                 C   rˆ   r‰   ©Ú
_type_list)ra   r™   r   r   r   r+   !  rŒ   zstr_to_tuple.__init__c                    s(   |  d¡}‡ fdd„t|ƒD ƒ}t|ƒS )Nr�   c                    s   g | ]\}}ˆ j | |ƒ‘qS r   r˜   )r-   r.   rŽ   r�   r   r   r�   %  r3   z)str_to_tuple.__call__.<locals>.<listcomp>)r   r8   Útupler‘   r   r�   r   r”   #  s   
zstr_to_tuple.__call__Nr•   r   r   r   r   r—      r–   r—   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )Ústr_to_dictc                 C   s   d S r‰   r   r�   r   r   r   r+   )  s   zstr_to_dict.__init__c                 C   s(   zt  |¡}W |S  ty   | Y S w r‰   )ÚastÚliteral_evalÚ
ValueError)ra   ÚsÚvaluer   r   r   Ú_guess_type,  s   ýÿzstr_to_dict._guess_typec                 C   sH   |  d¡}i }|D ]}|  dd¡\}}|  |¡}|  |¡}|||< q	|S )Nr�   ú=r*   )r   r¡   )ra   r’   r“   Úelemr   r$   r   r   r   r”   4  s   



zstr_to_dict.__call__N)r   re   rJ   r+   r¡   r”   r   r   r   r   r›   (  s    r›   c              	      s  t jt jd�}|jdtddd� |jdtddd� |jdtdd	d
d� t| tƒr+| g} d| vr4|  d¡ tD �]H‰tˆ \}‰ ‰}}t	|ƒt	| ƒ@ sKq6t
 |¡}t |j¡}|j}dd„ |D ƒ}|jdˆ› �d�}‡ ‡‡fdd„}	|j ¡ D ]Æ\}
}|j}|j}|t
jju}|t
jju r’|r’t|ƒ}|s—|�r<|	|
|ƒ}i }d}|
|v r­tj||
 td�}|||d	 < t|ƒdkrÇ|dd… D ]}t j||< q¿|D ]r}ttttg}dd„ |D ƒ}|tu rç|j|d|| d� qÉ||v �r|| |¡ }|j|t |ƒ||| d� qÉ|t!u �r|j|t"ƒ ||| d� qÉt#|dƒ�r0|j$t%u �r/|j&}|j|t'|ƒ||| d� qÉ|j||||| d� qÉqv|j(}|du �rGd}ˆ �ruˆ�s[dˆ d	 › dˆ› d|
› �}n
dˆ d	 › d|
› �}|d d! )t*ˆ ƒ¡› d"|› d#�7 }tj|td�}||_+q6|S )$z–Builds the argument parser from all of the bound functions.

    Returns
    -------
    ArgumentParser
        Argument parser built by ArgBind.
    )Úformatter_classz--args.saveFz1Path to save all arguments used to run script to.)r   ÚrequiredÚhelpz--args.loadz3Path to load arguments from, stored as a .yml file.z--args.debugr   z4Print arguments as they are passed to each function.)r   r¥   r&   r¦   r&   c                 S   s   i | ]}|j |j“qS r   )rA   Údescription©r-   rw   r   r   r   r2   a  s    
ÿz build_parser.<locals>.<dictcomp>z!Generated arguments for function )Útitlec                    sv   g }| }|rdnd}ˆr|d| › � }n
|dˆ› d| › � }|  | dd¡¡ ˆ d ur9ˆ D ]}|  | d|¡¡ q-|S )Nú--r	   zPATTERN/r,   r   )r   Úreplace)r   Úis_kwargÚ	arg_namesrA   ÚprependÚp©rO   rF   r'   r   r   Ú_get_arg_namesi  s   
ÿz$build_parser.<locals>._get_arg_namesr	   )Úwidthr*   Nc                 S   s   g | ]}t | ‘qS r   )r   r¨   r   r   r   r�   ’  s    z build_parser.<locals>.<listcomp>Ú
store_true)Úactionr¦   )r   r&   r¦   Ú
__origin__rª   r
   r,   z Additional scope patterns: z, zX. Use these by prefacing any of the args below with one of these patterns. For example: z VALUE.),ÚargparseÚArgumentParserÚRawTextHelpFormatterÚadd_argumentrR   ÚintrQ   r   rK   Úsetr6   r7   Údocstring_parserÚparseÚ__doc__ÚparamsÚadd_argument_groupr1   r   r&   Ú
annotationr9   r:   r   ÚtextwrapÚfillÚ
HELP_WIDTHrS   ÚSUPPRESSÚfloatÚboolÚindexr‡   r   r›   r`   rµ   rš   Ú__args__r—   Zshort_descriptionr    r5   r§   )r)   r¯   rE   r(   Zfn_groupÚsigÚ	docstringZparameter_helprv   r±   r   r$   r@   Úarg_typer¬   r­   Zarg_helpÚ	help_textZpattern_arg_namerA   Úinner_typesÚ
list_typesr‹   r™   ÚdescZscope_patternr   r°   r   Úbuild_parser@  s®   ÿ
ÿ
ÿÿ



ÿÿ

ÿ
ÿ

ÿÿ€ÿ€ýÿrÑ   c                 C   sŒ  | du r	t |d�n| } dd„ tjD ƒ}| ddg¡ |  ¡ \}}t|ƒ}||d< | d¡}| d¡}| d¡}d	d„ |D ƒ}	d
d„ |D ƒ}
|	D ]}| d¡\}}||vrZ|| ||< qG|rŽt|ƒ}|D ]}||vro|| ||< qc|	D ]}| d¡\}}||vr�||vr�||v r�|| ||< qr|
D ] }||v r°|	D ]}| d¡\}}||kr¯||vr¯|| ||< q˜q�|r¸t	||ƒ ||d< ||d< ||d< |S )zh
    Parses the command line and returns a dictionary.
    Builds the argument parser if p is None.
    N)r)   c                 S   s,   g | ]}|  d ¡r| d d¡ d¡d ‘qS )rª   r	   r¢   r   )rs   r«   r   r¨   r   r   r   r�   Â  s   , zparse_args.<locals>.<listcomp>z	args.savez	args.loadzargs.unknownr4   c                 S   s   g | ]}d |v r|‘qS ©r
   r   ©r-   r   r   r   r   r�   Ì  ó    c                 S   s   g | ]}d |vr|‘qS rÒ   r   rÓ   r   r   r   r�   Í  rÔ   r
   )
rÑ   r_   ÚargvÚextendÚparse_known_argsÚvarsr   r   r�   r|   )r¯   r)   Ú	used_argsÚknownÚunknownr>   Zload_args_pathZsave_args_pathZ
debug_argsZpattern_keysZtop_level_argsr   r   rA   Zloaded_argsZpattern_keyr   r   r   Ú
parse_args¼  sR   


€€€€
rÜ   )r	   r‰   )r&   )Nr&   )(r6   Ú
contextlibr   r¶   Útypingr   r   r   r¼   rÂ   ro   r_   rk   Úpathlibr   rœ   Ú	functoolsr   rT   rK   r   r;   r   r<   rÄ   r   r%   r5   rR   rV   Úbind_to_parserrW   rf   r|   r�   r‡   r—   r›   rÑ   rÜ   r   r   r   r   Ú<module>   sB    
 	o0|