B
    S\>                 @   s   d 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lm	Z	 ddlm
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ dZdZe
 Zdd Zdd ZG dd dejZdd Zd!ddZdd Zdd Zd"dd ZdS )#zThe AstroidBuilder makes astroid from living object and / or from _ast

The builder is not thread safe and can't be used to parse different sources
at the same time.
    N)detect_encoding)_parse)bases)
exceptions)manager)modutils)raw_building)	rebuilder)nodes)util__z#@c          	   C   sF   t | d}t|jd }W d Q R X t | dd |d}| }|||fS )Nrbr   r)newlineencoding)openr   readlineread)filenameZbyte_streamr   streamdata r   .lib/python3.7/site-packages/astroid/builder.pyopen_source_file/   s
    r   c             C   s@   y|   }W n tk
r    Y nX |r<|dd |D kr<dS dS )Nc             S   s   h | ]
}|j qS r   )value).0Zslotr   r   r   	<setcomp>=   s    z#_can_assign_attr.<locals>.<setcomp>FT)slotsNotImplementedError)nodeattrnamer   r   r   r   _can_assign_attr7   s    r!   c                   s`   e Zd ZdZd fdd	ZdddZddd	ZdddZdd Zdd Z	dd Z
dd Z  ZS )AstroidBuildera  Class for building an astroid tree from source code or from a live module.

    The param *manager* specifies the manager class which should be used.
    If no manager is given, then the default one will be used. The
    param *apply_transforms* determines if the transforms should be
    applied after the tree was built from source or from a live object,
    by default being True.
    NTc                s"   t t|   |pt| _|| _d S )N)superr"   __init__MANAGER_manager_apply_transforms)selfr   apply_transforms)	__class__r   r   r$   M   s    
zAstroidBuilder.__init__c             C   s   d}t |dd}|dk	rVtjt|\}}|dkrVtj|d rV| |d |}|dkr| j|||d}| j	r| j
|}|S )z/Build an astroid from a living module instance.N__file__)z.pyz.pycz.pyoz.py)modnamepath)getattrosr-   splitextr   Z_path_from_filenameexists
file_buildZinspect_buildr'   r&   visit_transforms)r(   moduler,   r   r-   Zpath_Zextr   r   r   module_buildR   s    zAstroidBuilder.module_buildc          
   C   s*  yt |\}}}W n tk
rH } ztjd|||d|W dd}~X Y nj ttfk
r } ztjd|||d|W dd}~X Y n2 tk
r } ztjd|d|W dd}~X Y nX |j |dkrydt	
|}W n. tk
r   tjtj|d }Y nX | |||}| ||S Q R X dS )	zxBuild astroid from a source code file (i.e. from an ast)

        *path* is expected to be a python source file
        z#Unable to load file {path}:
{error})r,   r-   errorNzBPython 3 encoding specification error or unknown encoding:
{error}z.Wrong or no encoding specified for {filename}.)r   .r   )r   IOErrorr   AstroidBuildingErrorSyntaxErrorLookupErrorAstroidSyntaxErrorUnicodeErrorjoinr   Zmodpath_from_fileImportErrorr/   r-   r0   basename_data_build_post_build)r(   r-   r,   r   r   r   excr4   r   r   r   r2   d   s8    
zAstroidBuilder.file_build c             C   s&   |  |||}|d|_| |dS )z&Build astroid from source code string.zutf-8)rA   encodeZ
file_bytesrB   )r(   r   r,   r-   r4   r   r   r   string_build   s    zAstroidBuilder.string_buildc             C   s   ||_ | j| xB|jD ]8}|jdkrHx|jD ]\}}|j| q0W | | qW x|j	D ]}| 
| q^W | jr| j|}|S )z@Handles encoding and delayed nodes after a module has been builtZ
__future__)file_encodingr&   cache_module_import_from_nodesr,   namesZfuture_importsaddadd_from_names_to_locals_delayed_assattrdelayed_assattrr'   r3   )r(   r4   r   Z	from_nodeZsymbol_Zdelayedr   r   r   rB      s    
zAstroidBuilder._post_buildc       
   
   C   s   yt |d }W n> tttfk
rN } ztjd||||d|W dd}~X Y nX |dk	rftj|}nd}|	dr|dd }d}n$|dk	otj
tj|d	 d
k}t| j}|||||}	|j|	_|j|	_|	S )z3Build tree node from data and add some informations
z#Parsing Python code failed:
{error})sourcer,   r-   r6   Nz<?>z	.__init__iTr   r$   )r   	TypeError
ValueErrorr:   r   r<   r/   r-   abspathendswithr0   r@   r	   ZTreeRebuilderr&   Zvisit_modulerI   rM   )
r(   r   r,   r-   r   rC   Z	node_filepackagebuilderr4   r   r   r   rA      s.    
zAstroidBuilder._data_buildc          	      s   dd   fdd}x|j D ]\}}|dkry| }W n tjk
rP   wY nX x^| D ]&}|j|| ||j j|  q\W q|j|p|| ||j j|p|  qW dS )zdStore imported names to the locals

        Resort the locals if coming from a delayed node
        c             S   s   | j S )N)
fromlineno)r   r   r   r   <lambda>   s    z9AstroidBuilder.add_from_names_to_locals.<locals>.<lambda>c                s   | j  d d S )N)key)sort)Zmy_list)	_key_funcr   r   sort_locals   s    z<AstroidBuilder.add_from_names_to_locals.<locals>.sort_locals*N)	rJ   Zdo_import_moduler   r9   Zpublic_namesparentZ	set_localZscopelocals)r(   r   r]   nameZasnameZimportedr   )r\   r   rL      s    z'AstroidBuilder.add_from_names_to_localsc          	   C   s   y|  }x|j D ]}|tjkr&qyP|jtjkrP|j}|j	}t
||jstwn$t|tjr`wn|jrn|j	}n|j}W n tk
r   wY nX ||jg }||krq|jdkr|r|d   jdkr|d| q|| qW W n tjk
r   Y nX dS )z^Visit a AssAttr node

        This adds name to locals and handle members definition.
        r$   r   N)frameexprZinferr   ZUninferabler*   r   ZInstanceZ_proxiedZinstance_attrsr!   r    
isinstanceZis_functionr`   AttributeError
setdefaultra   insertappendr   ZInferenceError)r(   r   rb   ZinferredZiattrsvaluesr   r   r   rN      s8    


zAstroidBuilder.delayed_assattr)NT)N)N)rD   N)__name__
__module____qualname____doc__r$   r5   r2   rF   rB   rA   rL   rN   __classcell__r   r   )r*   r   r"   B   s   

&
r"   c             C   s   t j| d|ddS )NrD   T)docr-   rV   )r
   ZModule)ra   r-   r   r   r   build_namespace_package_module  s    rp   rD   Tc             C   s&   t | } tt|d}|j| ||dS )av  Parses a source string in order to obtain an astroid AST from it

    :param str code: The code for the module.
    :param str module_name: The name for the module, if any
    :param str path: The path for the module
    :param bool apply_transforms:
        Apply the transforms for the give code. Use it if you
        don't want the default transforms to be applied.
    )r   r)   )r,   r-   )textwrapdedentr"   r%   rF   )codemodule_namer-   r)   rW   r   r   r   parse  s    

ru   c             c   s   t | tjrt | jtjr| jjtkr| jd }| j|_xh| jj	D ]\}t
| j|}t |ttfrx<t|D ]\}}|| krj|||< qjW qB|| krBt| j|| qBW |V  n x|  D ]}t|E dH  qW dS )a  Find expressions in a call to _TRANSIENT_FUNCTION and extract them.

    The function walks the AST recursively to search for expressions that
    are wrapped into a call to _TRANSIENT_FUNCTION. If it finds such an
    expression, it completely removes the function call node from the tree,
    replacing it by the wrapped expression inside the parent.

    :param node: An astroid node.
    :type node:  astroid.bases.NodeNG
    :yields: The sequence of wrapped expressions on the modified tree
    expression can be found.
    r   N)rd   r
   ZCallfuncNamera   _TRANSIENT_FUNCTIONargsr_   Z_astroid_fieldsr.   listtuple	enumeratesetattrget_children_extract_expressions)r   Z	real_exprra   childidxZcompound_childr   r   r   r     s     
r   c             C   sT   t | tjtjfr| j}n| j}||kr,| S x"|  D ]}t||}|r6|S q6W dS )a  Extracts the statement on a specific line from an AST.

    If the line number of node matches line, it will be returned;
    otherwise its children are iterated and the function is called
    recursively.

    :param node: An astroid node.
    :type node: astroid.bases.NodeNG
    :param line: The line number of the statement to extract.
    :type line: int
    :returns: The statement on the line, or None if no statement for the line
      can be found.
    :rtype:  astroid.bases.NodeNG or None
    N)rd   r
   ZClassDefZFunctionDefrX   linenor~   _find_statement_by_line)r   lineZ	node_liner   resultr   r   r   r   =  s    
r   c                s   dd  g }x4t |  D ]$\}}| tr||d  qW t| |djs\tdg }|rvfdd|D }|	t
 |s|jd   fd	d|D }t|dkr|d
 S |S )a  Parses some Python code as a module and extracts a designated AST node.

    Statements:
     To extract one or more statement nodes, append #@ to the end of the line

     Examples:
       >>> def x():
       >>>   def y():
       >>>     return 1 #@

       The return statement will be extracted.

       >>> class X(object):
       >>>   def meth(self): #@
       >>>     pass

      The function object 'meth' will be extracted.

    Expressions:
     To extract arbitrary expressions, surround them with the fake
     function call __(...). After parsing, the surrounded expression
     will be returned and the whole AST (accessible via the returned
     node's parent attribute) will look like the function call was
     never there in the first place.

     Examples:
       >>> a = __(1)

       The const node will be extracted.

       >>> def x(d=__(foo.bar)): pass

       The node containing the default argument will be extracted.

       >>> def foo(a, b):
       >>>   return 0 < __(len(a)) < b

       The node containing the function call 'len' will be extracted.

    If no statements or expressions are selected, the last toplevel
    statement will be returned.

    If the selected statement is a discard statement, (i.e. an expression
    turned into a statement), the wrapped expression is returned instead.

    For convenience, singleton lists are unpacked.

    :param str code: A piece of Python code that is parsed as
    a module. Will be passed through textwrap.dedent first.
    :param str module_name: The name of the module.
    :returns: The designated node from the parse tree, or a list of nodes.
    :rtype: astroid.bases.NodeNG, or a list of nodes.
    c             S   s   t | tjr| jS | S )N)rd   r
   ZExprr   )r   r   r   r   _extract  s    zextract_node.<locals>._extract   )rt   z"Empty tree, cannot extract from itc                s   g | ]}t  |qS r   )r   )r   r   )treer   r   
<listcomp>  s    z extract_node.<locals>.<listcomp>c                s   g | ]} |qS r   r   )r   r   )r   r   r   r     s    r   )r|   
splitlinesstriprU   _STATEMENT_SELECTORrh   ru   ZbodyrS   extendr   len)rs   rt   Zrequested_linesr   r   Z	extractedr   )r   r   r   extract_node`  s$    7r   )rD   NT)rD   )rm   r/   rq   tokenizer   Zastroid._astr   Zastroidr   r   r   r   r   r	   r
   r   rx   r   ZAstroidManagerr%   r   r!   ZInspectBuilderr"   rp   ru   r   r   r   r   r   r   r   <module>   s0    B
'#