a
    Žù»jÁ6  ã                   @   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 d  ƒ n1 s¨0    Y  d t |¡¡||fW S    t| dƒròt| j  ¡ ƒ|d	f Y S t| ƒ|d	f Y S Y n0 dS )
a/  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)ÚfuncÚsource_fileÚcodeZsource_codeÚline_noZsource_file_objÚ
first_lineZsource_lines© r#   ú9E:\soccer\ml_env\Lib\site-packages\joblib/func_inspect.pyÚget_func_code   s,    
ÿ
2
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_charsR   s    
r2   Tc           	      C   s,  t | dƒr| j}n:zt | ¡}W n* tyJ   t | dƒrB| jj}nd}Y n0 |du rXd}|dk�r4ztj t 	| ¡¡}W n   d}Y n0 |du�r4| 
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ƒ�rR| j}nt | dƒ�rf| j}nd}|�r t | dƒ�r || jv �r | j| | u�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#   )r2   )Ú.0Úsr#   r#   r$   Ú
<listcomp>¸   ó    z!get_func_name.<locals>.<listcomp>)r   r3   r   Ú	getmoduleÚ	TypeErrorr4   r   r   ÚabspathÚgetsourcefileÚsplitÚsepr   r   ÚlenÚendswithr=   r>   r?   r@   ÚextendÚismethodrA   ÚappendÚnamer2   )	r   Úresolv_aliasZwin_charactersÚmoduleÚfilenameÚpartsrK   rR   Úklassr#   r#   r$   Úget_func_nameb   s\    






$

rX   c                 C   s   d  | |¡S )z.Helper function to output a function signaturez{}{})Úformat)Úfunction_nameÚarg_sigr#   r#   r$   Ú_signature_str¼   s    r\   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#   )rC   ÚkÚvr#   r#   r$   Ú	<genexpr>Æ   rF   z'_function_called_str.<locals>.<genexpr>)r   r   ÚitemsrY   )rZ   ÚargsÚkwargsZtemplate_strÚargs_strZ
kwargs_strr#   r#   r$   Ú_function_called_strÁ   s    rf   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u r | |
j¡ nL|
j|
ju rÆ| |
j¡ | |
j¡ n&|
j|
ju rÚ|
j}n|
j|
ju rì|
j}	|
j|
jur‚| |
j¡ q‚t | ¡�rB| jg| }t 
| j¡}tt|jƒƒ}|g| }t| dd�\}}tƒ }d	}t|ƒD ]Ò\}}|t|ƒk �r´||v�r’|| ||< n td
|t||ƒt|||ƒf ƒ‚n€|t|ƒ }||v �rØ|| ||< n\z|| ||< W nJ t t!f�y2 } z,tdt||ƒt|||ƒf ƒ|‚W Y d}~n
d}~0 0 �qdtƒ }t"| #¡ ƒD ]B\}}||v �rf|||< n$|	du�rz|||< nt$d||f ƒ‚�qJ|	du�r ||d< |du�rÂ||d d… }||d< |D ]2}||v �rà| %|¡ ntd|t||ƒf ƒ‚�qÆ|S )aX  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.r8   )Ú
stacklevel)Ú*ú**NF)rS   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'ri   r   rh   z9Ignore list: argument '%s' is not defined for function %s)&r   Ú
isinstancer   Ú
ValueErrorÚtyper   rP   Ú
isfunctionÚwarningsÚwarnÚ	signatureÚ
parametersÚvaluesÚkindÚPOSITIONAL_OR_KEYWORDrQ   rR   ÚKEYWORD_ONLYÚVAR_POSITIONALÚVAR_KEYWORDÚdefaultÚemptyÚ__self__Ú__func__ÚnextÚiterrX   ÚdictÚ	enumeraterM   r\   rf   Ú
IndexErrorÚKeyErrorÚsortedrb   rH   Úpop)r   Z
ignore_lstrc   rd   r[   Ú	arg_namesZarg_defaultsZarg_kwonlyargsZarg_varargsZ	arg_varkwÚparamZclass_method_sigÚ	self_nameÚ_rR   Zarg_dictZarg_positionÚ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 )Nr8   )ÚindentiÜ  z%s...i¼  )r   rM   )ÚargÚformatted_argr#   r#   r$   Ú_format_argT  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#   )rC   Úmr#   r#   r$   rE   ^  rF   z$format_signature.<locals>.<listcomp>r<   r   éP   z
%sc                 S   s    g | ]\}}d |t |ƒf ‘qS )r^   )r’   )rC   r`   Úir#   r#   r$   rE   l  rF   r]   z%s(%s))rX   rQ   r   r   r’   rM   rO   rb   )r   rc   rd   rT   rR   Úmodule_pathZarg_strZprevious_lengthr�   r‘   rp   r#   r#   r$   Úformat_signature[  s$    

r—   ÚMemoryc                 C   s.   t | g|¢R i |¤Ž\}}dd|||f }|S )zdReturns a nicely formatted statement displaying the function
    call with the given arguments.
    z%s
[%s] Calling %s...
%sZP________________________________________________________________________________)r—   )r   rc   rd   Zobject_namer   rp   Úmsgr#   r#   r$   Úformat_calls  s    rš   )TT)r˜   )Ú__doc__Úcollectionsr   r   r   rn   Ú	itertoolsr   Útokenizer   r   Úloggerr   Zfull_argspec_fieldsÚ
namedtupleZfull_argspec_typer%   r2   rX   r\   rf   r~   rŽ   r’   r—   rš   r#   r#   r#   r$   Ú<module>   s*   ÿ9
Z	 