U
    ºmœd|7  ã                   @   s¸   d 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m	Z
 ddlmZ dZe de¡Zd	d
„ Zdd„ Zddd„Zdd„ Zdd„ Zdeƒ fdd„Zdd„ Zdd„ Zddd„ZdS ) z>
My own variation on function-specific inspect-like features.
é    N)Úislice)Úopené   )ÚpformatzAargs varargs varkw defaults kwonlyargs kwonlydefaults annotationsÚFullArgSpecc              	   C   sú   d}z°| j }|j}tj |¡spd t | ¡d ¡}d}| d¡rdt	 
d|¡ ¡ \}}t|ƒ}d| }|||fW S t|ƒ� }|j}tt||d dƒƒ}W 5 Q R X d t |¡¡||fW S    t| dƒrÞt| j  ¡ ƒ|d	f Y S t| ƒ|d	f Y S Y nX dS )
al   Attempts to retrieve a reliable function code hash.

        The reason we don't use inspect.getsource is that it caches the
        source, whereas we want this to be modified on the fly when the
        function is modified.

        Returns
        -------
        func_code: string
            The function code
        source_file: string
            The path to the file in which the function is defined.
        first_line: int
            The first line of the code in the source file.

        Notes
        ------
        This function does a bit more magic than inspect, and is thus
        more robust.
    NÚ r   r   z	<doctest z\<doctest (.*\.rst)\[(.*)\]\>z<doctest %s>Ú__code__éÿÿÿÿ)r   Úco_filenameÚosÚpathÚexistsÚjoinÚinspectÚgetsourcelinesÚ
startswithÚreÚmatchÚgroupsÚintÚopen_py_sourceÚco_firstlinenoÚlistr   ÚgetblockÚhasattrÚstrÚ__hash__Úrepr)ÚfuncZsource_fileÚcodeZsource_codeÚline_noZsource_file_objÚ
first_lineZsource_lines© r"   úL/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/joblib/func_inspect.pyÚget_func_code   s.    
 ÿ

r$   c                 C   sH   ddl }t|dƒr|j}nddl}|jj}dD ]}|  |||ƒ¡} q.| S )z2Windows cannot encode some characters in filename.r   NÚquote)ú<ú>ú!ú:ú\)Úurllibr   r%   Úurllib.parseÚparseÚreplace)Ústringr+   r%   Úcharr"   r"   r#   Ú_clean_win_charsQ   s    
r1   Tc           	      C   s.  t | dƒr| j}n<zt | ¡}W n, tk
rL   t | dƒrD| jj}nd}Y nX |dkrZd}|dk�r6ztj t 	| ¡¡}W n   d}Y nX |dk	�r6| 
tj¡}|d  d¡râ|d  
d	¡}d	 |dd
… |dd…  ¡|d< n&t|ƒd
k�r|d  d¡�rd|d< d	 |¡}| d¡�r*|dd… }|d	 | }| 
d¡}t | dƒ�rT| j}nt | dƒ�rh| j}nd}|�r¢t | dƒ�r¢|| jk�r¢| j| | k	�r¢d| }t | dƒ�rÔ| j|k�rÔ| | j 
d¡dd… ¡ t | ¡�rþt | dƒ�rþ| j}| |j¡ tjdk�r&|�r&t|ƒ}dd„ |D ƒ}||fS )aâ   Return the function import path (as a list of module names), and
        a name for the function.

        Parameters
        ----------
        func: callable
            The func to inspect
        resolv_alias: boolean, optional
            If true, possible local aliases are indicated.
        win_characters: boolean, optional
            If true, substitute special characters using urllib.quote
            This is useful in Windows, as it cannot encode some filenames
    Ú
__module__Ú	__class__ÚunknownNr   Ú__main__r	   z<ipython-inputú-é   é   éþÿÿÿZ
ipykernel_Z	ipykernelz.pyéýÿÿÿÚ.Ú	func_nameÚ__name__Úfunc_globalsz%s-aliasÚ__qualname__Úim_classÚntc                 S   s   g | ]}t |ƒ‘qS r"   )r1   )Ú.0Úsr"   r"   r#   Ú
<listcomp>µ   s     z!get_func_name.<locals>.<listcomp>)r   r2   r   Ú	getmoduleÚ	TypeErrorr3   r   r   ÚabspathÚgetsourcefileÚsplitÚsepr   r   ÚlenÚendswithr<   r=   r>   r?   ÚextendÚismethodr@   ÚappendÚnamer1   )	r   Úresolv_aliasZwin_charactersÚmoduleÚfilenameÚpartsZsplittedrP   Úklassr"   r"   r#   Úget_func_name_   s\    






$

rV   c                 C   s   d  | |¡S )z.Helper function to output a function signaturez{}{})Úformat)Úfunction_nameÚarg_sigr"   r"   r#   Ú_signature_str¹   s    rZ   c                 C   s:   d}t |ƒdd… }d dd„ | ¡ D ƒ¡}| | ||¡S )z)Helper function to output a function callz{0}({1}, {2})r   r	   ú, c                 s   s   | ]\}}d ||f V  qdS )ú%s=%sNr"   )rB   ÚkÚvr"   r"   r#   Ú	<genexpr>Ã   s   ÿz'_function_called_str.<locals>.<genexpr>)r   r   ÚitemsrW   )rX   ÚargsÚkwargsZtemplate_strÚargs_strZ
kwargs_strr"   r"   r#   Ú_function_called_str¾   s    
ÿÿrd   r"   c                 C   sü  t |ƒ}t|tƒr&td|t|ƒf ƒ‚t | ¡sZt | ¡sZ|rPtj	d|  dd� ||dœS t 
| ¡}g }g }g }d}d}	|j ¡ D ]‚}
|
j|
jkr | |
j¡ nL|
j|
jkrÆ| |
j¡ | |
j¡ n&|
j|
jkrÚ|
j}n|
j|
jkrì|
j}	|
j|
jk	r‚| |
j¡ q‚t | ¡�rB| jg| }t 
| j¡}tt|jƒƒ}|g| }t| dd�\}}tƒ }d	}t|ƒD ]Ð\}}|t|ƒk �r´||k�r’|| ||< n td
|t||ƒt|||ƒf ƒ‚n~|t|ƒ }||k�rØ|| ||< nZz|| ||< W nH t t!fk
�r0 } z$tdt||ƒt|||ƒf ƒ|‚W 5 d}~X Y nX �qdtƒ }t"| #¡ ƒD ]B\}}||k�rd|||< n$|	dk	�rx|||< nt$d||f ƒ‚�qH|	dk	�rž||d< |dk	�rÀ||d d… }||d< |D ]2}||k�rÞ| %|¡ ntd|t||ƒf ƒ‚�qÄ|S )a™   Filters the given args and kwargs using a list of arguments to
        ignore, and a function specification.

        Parameters
        ----------
        func: callable
            Function giving the argument specification
        ignore_lst: list of strings
            List of arguments to ignore (either a name of an argument
            in the function spec, or '*', or '**')
        *args: list
            Positional arguments passed to the function.
        **kwargs: dict
            Keyword arguments passed to the function

        Returns
        -------
        filtered_args: list
            List of filtered positional and keyword arguments.
    zHignore_lst must be a list of parameters to ignore %s (type %s) was givenz4Cannot inspect object %s, ignore list will not work.r7   )Ú
stacklevel)Ú*ú**NF)rQ   r	   zZKeyword-only parameter '%s' was passed as positional parameter for %s:
     %s was called.z5Wrong number of arguments for %s:
     %s was called.zAIgnore list for %s() contains an unexpected keyword argument '%s'rg   r   rf   z9Ignore list: argument '%s' is not defined for function %s)&r   Ú
isinstancer   Ú
ValueErrorÚtyper   rN   Ú
isfunctionÚwarningsÚwarnÚ	signatureÚ
parametersÚvaluesÚkindÚPOSITIONAL_OR_KEYWORDrO   rP   ÚKEYWORD_ONLYÚVAR_POSITIONALÚVAR_KEYWORDÚdefaultÚemptyÚ__self__Ú__func__ÚnextÚiterrV   ÚdictÚ	enumeraterK   rZ   rd   Ú
IndexErrorÚKeyErrorÚsortedr`   rF   Úpop)r   Z
ignore_lstra   rb   rY   Ú	arg_namesZarg_defaultsZarg_kwonlyargsZarg_varargsZ	arg_varkwÚparamZclass_method_sigÚ	self_nameÚ_rP   Zarg_dictZarg_positionZarg_nameÚpositionÚeZ	varkwargsÚ	arg_valueÚvarargsÚitemr"   r"   r#   Úfilter_argsÉ   s¶    

ÿÿÿÿ




þýÿ


ÿþÿû



ÿ



ÿþ
r‹   c                 C   s,   t | dd�}t|ƒdkr(d|d d…  }|S )Nr7   )ÚindentiÜ  z%s...i¼  )r   rK   )ÚargÚformatted_argr"   r"   r#   Ú_format_argI  s    r�   c                 O   s¬   t | ƒ\}}dd„ |D ƒ}|r4| |¡ d |¡}n|}tƒ }d}|D ].}t|ƒ}	|dkrbd|	 }	t|	ƒ}| |	¡ qF| dd„ | ¡ D ƒ¡ d |¡}d	||f }
||
fS )
Nc                 S   s   g | ]}|r|‘qS r"   r"   )rB   Úmr"   r"   r#   rD   S  s      z$format_signature.<locals>.<listcomp>r;   r   éP   z
%sc                 S   s    g | ]\}}d |t |ƒf ‘qS )r\   )r�   )rB   r^   Úir"   r"   r#   rD   a  s     r[   z%s(%s))rV   rO   r   r   r�   rK   rM   r`   )r   ra   rb   rR   rP   Úmodule_pathZarg_strZprevious_lengthr�   rŽ   rn   r"   r"   r#   Úformat_signatureP  s$    

r”   ÚMemoryc                 C   s(   t | f|ž|Ž\}}dd|||f }|S )zi Returns a nicely formatted statement displaying the function
        call with the given arguments.
    z%s
[%s] Calling %s...
%sZP________________________________________________________________________________)r”   )r   ra   rb   Zobject_namer   rn   Úmsgr"   r"   r#   Úformat_callh  s     ÿr—   )TT)r•   )Ú__doc__r   rl   r   r   ÚcollectionsÚ	itertoolsr   Útokenizer   r   Úloggerr   Zfull_argspec_fieldsÚ
namedtupleZfull_argspec_typer$   r1   rV   rZ   rd   r|   r‹   r�   r”   r—   r"   r"   r"   r#   Ú<module>   s(   8
Z 