3
QfXJ,                 @   s   d Z ddlm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d	 Zd
d ZdddZf e fddZdd ZdddZdS )z>
My own variation on function-specific inspect-like features.
    )isliceN   )_basestring)pformat)open_py_sourcec             C   s   d}y| j }|j}tjj|sndjtj| d }d}|jdrdt	j
d|j \}}t|}d| }|||fS t| }|j}tt||d d}W dQ R X djtj|||fS    t| drt| j j |d	fS t| |d
f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	   )r   co_filenameospathexistsjoininspectgetsourcelines
startswithrematchgroupsintr   co_firstlinenolistr   getblockhasattrstr__hash__repr)funcsource_filecodeZsource_codeZline_noZsource_file_obj
first_lineZsource_lines r!   Z/home/psgendb/BIRCHDEV/pkg/SPAdes-3.15.4/linux-x86_64/share/spades/joblib3/func_inspect.pyget_func_code   s,    



r#   c             C   sL   ddl }t|dr|j}nddl}|jj}xd	D ]}| j|||} q0W | S )
z2Windows cannot encode some characters in filename.r   Nquote<>!:\)r%   r&   r'   r(   r)   )urllibr   r$   urllib.parseparsereplace)stringr*   r$   charr!   r!   r"   _clean_win_charsL   s    

r0   Tc             C   s  t | dr| j}n<ytj| }W n, tk
rL   t | drD| jj}nd}Y nX |dkrZd}|dkrytjjtj	| }W n   d}Y nX |dk	r|j
tj}|d jdrd	|d< d
j|}|jdr|dd }|d
 | }|j
d}t | d r| j}nt | dr| j}nd}|rLt | drL|| jkrL| j| | k	rLd| }tj| rvt | drv| j}|j|j tjdkr|rt|}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-inputz__ipython-input__-z.py   .	func_name__name__func_globalsz%s-aliasim_classntc             S   s   g | ]}t |qS r!   )r0   ).0sr!   r!   r"   
<listcomp>   s    z!get_func_name.<locals>.<listcomp>r	   r	   )r   r1   r   	getmodule	TypeErrorr2   r   r   abspathgetsourcefilesplitsepr   r   endswithr8   r9   r:   ismethodr;   appendnamer0   )r   resolv_aliaswin_charactersmodulefilenamepartsrJ   klassr!   r!   r"   get_func_nameZ   sR    







rQ   c             C   sj  t |}t|tr&td|t|f tj|  r^tj|  r^|rTtj	d|  dd ||dS tj
| }t|dr|j}|j}|j}|j}n|\}}}}|pi }tj| r| jg| }t| dd\}	}
t }d}xt|D ]\}}|t|k r|| ||< q|t| }||kr"|j|||< qy|| ||< W q ttfk
r   td
|
tjtj
|  |
t|d	d djdd |j D f Y qX qW t }xRt|j D ]B\}}||kr|||< n$|dk	r|||< ntd|
|f qW |dk	r ||d< |dk	r"||d	 d }||d< xB|D ]:}||krB|j| ntd||
tj||||f q(W |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.   )
stacklevel)*z**argsF)rK   r   z?Wrong number of arguments for %s%s:
     %s(%s, %s) was called.z, c             s   s   | ]\}}d ||f V  qdS )z%s=%sNr!   )r=   kvr!   r!   r"   	<genexpr>   s   zfilter_args.<locals>.<genexpr>NzAIgnore list for %s() contains an unexpected keyword argument '%s'z**rT   z;Ignore list: argument '%s' is not defined for function %s%sr	   r	   )r   
isinstancer   
ValueErrortyper   rH   
isfunctionwarningswarn
getargspecr   rU   defaultskeywordsvarargs__self__rQ   dict	enumeratelenpop
IndexErrorKeyErrorformatargspecr   r   itemssortedrB   )r   Z
ignore_lstrU   kwargsZarg_spec	arg_namesZarg_defaultsZarg_keywordsZarg_varargs_rJ   Zarg_dictZarg_positionZarg_namepositionZ	varkwargs	arg_valuerb   itemr!   r!   r"   filter_args   sz    














rs   c       
      O   s   t | \}}dd |D }|r4|j| dj|}n|}t }d}xV|D ]N}t|dd}t|dkrtd|d d	  }|d
krd| }t|}|j| qHW |jdd |j D  dj|}d||f }	||	fS )Nc             S   s   g | ]}|r|qS r!   r!   )r=   mr!   r!   r"   r?     s    z$format_signature.<locals>.<listcomp>r7   r   rR   )indenti  z%s...i  P   z
%sc             S   s    g | ]\}}d |t |f qS )z%s=%s)r   )r=   rW   ir!   r!   r"   r?     s    z, z%s(%s))rQ   rI   r   r   r   rf   extendrk   )
r   rU   rm   rM   rJ   Zmodule_pathZarg_strZprevious_lengtharg	signaturer!   r!   r"   format_signature  s(    


r{   Memoryc             C   s,   t | f||\}}ddd |||f }|S )zi Returns a nicely formatted statement displaying the function
        call with the given arguments.
    z%s
[%s] Calling %s...
%srv   ro   )r{   )r   rU   rm   object_namer   rz   msgr!   r!   r"   format_call&  s    

r   )TT)r|   )__doc__	itertoolsr   r   r]   r   r   _compatr   loggerr   _memory_helpersr   r#   r0   rQ   rd   rs   r{   r   r!   r!   r!   r"   <module>   s   9
Em