
%![c           @   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 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 d   Z e j d k rd d l m Z d   Z n! e j d  Z d   Z d   Z e	 j   Z d   Z d e j f d     YZ d   Z d d e  d  Z! d   Z" d   Z# d d  Z$ d S(   s   The 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.
iN(   t   bases(   t
   exceptions(   t   manager(   t   modutils(   t   raw_building(   t	   rebuilder(   t   nodes(   t   utilt   __s   #@c         C   s   t  |  d d t j  S(   Ns   <string>t   exec(   t   compilet   _astt   PyCF_ONLY_AST(   t   string(    (    s.   lib/python2.7/site-packages/astroid/builder.pyt   _parse(   s    i   i    (   t   detect_encodingc         C   s_   t  |  d   } t | j  d } Wd  QXt  |  d d d  d | } | j   } | | | f S(   Nt   rbi    t   rt   newlinet   encoding(   t   openR   t   readlinet   Nonet   read(   t   filenamet   byte_streamR   t   streamt   data(    (    s.   lib/python2.7/site-packages/astroid/builder.pyt   open_source_file/   s
    s   \s*#+.*coding[:=]\s*([-\w.]+)c         C   s`   |  j  d  r d SxF |  j d d  d  D]. } t j |  } | d k	 r* | j d  Sq* Wd S(   sE   get encoding from a python file as string or return None if not founds   ﻿s   UTF-8s   
i   i   N(   t
   startswitht   splitt   _ENCODING_RGXt   matchR   t   group(   R   t   lineR    (    (    s.   lib/python2.7/site-packages/astroid/builder.pyt   _guess_encoding9   s    c         C   s4   t  |  d  } | j   } t |  } | | | f S(   s   get data for parsing a filet   U(   R   R   R#   (   R   R   R   R   (    (    s.   lib/python2.7/site-packages/astroid/builder.pyR   E   s    c         C   sN   y |  j    } Wn t k
 r# n' X| rJ | t d   | D  k rJ t St S(   Nc         s   s   |  ] } | j  Vq d  S(   N(   t   value(   t   .0t   slot(    (    s.   lib/python2.7/site-packages/astroid/builder.pys	   <genexpr>V   s    (   t   slotst   NotImplementedErrort   sett   Falset   True(   t   nodet   attrnameR(   (    (    s.   lib/python2.7/site-packages/astroid/builder.pyt   _can_assign_attrP   s    "t   AstroidBuilderc           B   sh   e  Z d  Z d
 e d  Z d
 d  Z d
 d  Z d d
 d  Z d   Z	 d   Z
 d   Z d	   Z RS(   s  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.
    c         C   s/   t  t |   j   | p t |  _ | |  _ d  S(   N(   t   superR0   t   __init__t   MANAGERt   _managert   _apply_transforms(   t   selfR   t   apply_transforms(    (    s.   lib/python2.7/site-packages/astroid/builder.pyR2   e   s    c         C   s   d } t | d d  } | d k	 r t j j t j |   \ } } | d k r t j j | d  r |  j | d |  } q n  | d k r |  j	 | d | d | } |  j
 r |  j j |  } q n  | S(	   s/   Build an astroid from a living module instance.t   __file__s   .pys   .pycs   .pyot   modnamet   pathN(   s   .pys   .pycs   .pyo(   R   t   getattrt   osR:   t   splitextR   t   _path_from_filenamet   existst
   file_buildt   inspect_buildR5   R4   t   visit_transforms(   R6   t   moduleR9   R-   R:   t   path_t   ext(    (    s.   lib/python2.7/site-packages/astroid/builder.pyt   module_buildj   s    !"	c      	   C   sZ  y t  |  \ } } } Wn t k
 rV } t j t j d d | d | d |  nm t t f k
 r } t j t j d d | d | d |  n- t	 k
 r t j t j d d |  n X|  | d
 k r+y d j t j |   } Wq+t k
 r't j j t j j |   d	 } q+Xn  |  j | | |  } |  j | |  SWd
 QXd
 S(   sx   Build astroid from a source code file (i.e. from an ast)

        *path* is expected to be a python source file
        s#   Unable to load file {path}:
{error}R9   R:   t   errorsB   Python 3 encoding specification error or unknown encoding:
{error}s.   Wrong or no encoding specified for {filename}.R   t   .i    N(   R   t   IOErrorR   t   reraiseR   t   AstroidBuildingErrort   SyntaxErrort   LookupErrort   AstroidSyntaxErrort   UnicodeErrorR   t   joinR   t   modpath_from_filet   ImportErrorR<   R:   R=   t   basenamet   _data_buildt   _post_build(   R6   R:   R9   R   R   R   t   excRC   (    (    s.   lib/python2.7/site-packages/astroid/builder.pyR@   |   s,    )t    c         C   s7   |  j  | | |  } | j d  | _ |  j | d  S(   s&   Build astroid from source code string.s   utf-8(   RT   t   encodet
   file_bytesRU   (   R6   R   R9   R:   RC   (    (    s.   lib/python2.7/site-packages/astroid/builder.pyt   string_build   s    c         C   s   | | _  |  j j |  xZ | j D]O } | j d k re x* | j D] \ } } | j j |  qB Wn  |  j |  q# Wx | j	 D] } |  j
 |  q W|  j r |  j j |  } n  | S(   s@   Handles encoding and delayed nodes after a module has been builtt
   __future__(   t   file_encodingR4   t   cache_modulet   _import_from_nodesR9   t   namest   future_importst   addt   add_from_names_to_localst   _delayed_assattrt   delayed_assattrR5   RB   (   R6   RC   R   t	   from_nodet   symbolt   _t   delayed(    (    s.   lib/python2.7/site-packages/astroid/builder.pyRU      s    		c   
      C   s$  y t  | d  } WnJ t t t f k
 r` } t j t j d d | d | d | d |  n X| d k	 r t	 j
 j |  } n d } | j d  r | d	  } t } n4 | d k	 o t	 j
 j t	 j
 j |   d
 d k } t j |  j  } | j | | | |  }	 | j |	 _ | j |	 _ |	 S(   s3   Build tree node from data and add some informationss   
s#   Parsing Python code failed:
{error}t   sourceR9   R:   RG   s   <?>s	   .__init__ii    R2   N(   R   t	   TypeErrort
   ValueErrorRL   R   RJ   R   RN   R   R<   R:   t   abspatht   endswithR,   R=   RS   R   t   TreeRebuilderR4   t   visit_moduleR^   Rc   (
   R6   R   R9   R:   R-   RV   t	   node_filet   packaget   builderRC   (    (    s.   lib/python2.7/site-packages/astroid/builder.pyRT      s$     
	4c            s   d       f d   } x | j  D] \ } } | d k r y | j   } Wn t j k
 rc q" n Xx} | j   D]3 } | j j | |  | | j j   j |  qq Wq" | j j | p | |  | | j j   j | p |  q" Wd S(   sd   Store imported names to the locals

        Resort the locals if coming from a delayed node
        c         S   s   |  j  S(   N(   t
   fromlineno(   R-   (    (    s.   lib/python2.7/site-packages/astroid/builder.pyt   <lambda>   s    c            s   |  j  d    d  S(   Nt   key(   t   sort(   t   my_list(   t	   _key_func(    s.   lib/python2.7/site-packages/astroid/builder.pyt   sort_locals   s    t   *N(	   R_   t   do_import_moduleR   RK   t   public_namest   parentt	   set_localt   scopet   locals(   R6   R-   Ry   t   namet   asnamet   imported(    (   Rx   s.   lib/python2.7/site-packages/astroid/builder.pyRb      s    	!c         C   s_  yD| j    } x1| j j   D] } | t j k r: q n  yy | j t j k r| | j } | j	 } t
 | | j  s w q n6 t | t j  r w n | j r | j	 } n	 | j } Wn t k
 r q n X| j | j g   } | | k r q n  | j d k r2| r2| d j    j d k r2| j d |  q | j |  q WWn t j k
 rZn Xd S(   s^   Visit a AssAttr node

        This adds name to locals and handle members definition.
        R2   i    N(   t   framet   exprt   inferR   t   Uninferablet	   __class__R    t   Instancet   _proxiedt   instance_attrsR/   R.   t
   isinstancet   is_functionR   t   AttributeErrort
   setdefaultR   t   insertt   appendR   t   InferenceError(   R6   R-   R   t   inferredt   iattrst   values(    (    s.   lib/python2.7/site-packages/astroid/builder.pyRd      s6    				N(   t   __name__t
   __module__t   __doc__R   R,   R2   RF   R@   RZ   RU   RT   Rb   Rd   (    (    (    s.   lib/python2.7/site-packages/astroid/builder.pyR0   [   s   			c         C   s   t  j |  d d d | d t S(   Nt   docRW   R:   Rq   (   R   t   ModuleR,   (   R   R:   (    (    s.   lib/python2.7/site-packages/astroid/builder.pyt   build_namespace_package_module	  s    RW   c         C   s=   t  j |   }  t d t d |  } | j |  d | d | S(   sv  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   R7   R9   R:   (   t   textwrapt   dedentR0   R3   RZ   (   t   codet   module_nameR:   R7   Rr   (    (    s.   lib/python2.7/site-packages/astroid/builder.pyt   parse  s    
	c         c   s*  t  |  t j  r t  |  j t j  r |  j j t k r |  j d } |  j | _ x |  j j	 D] } t
 |  j |  } t  | t t f  r xX t |  D]% \ } } | |  k r | | | <q q Wq_ | |  k r_ t |  j | |  q_ q_ W| Vn3 x0 |  j   D]" } x t |  D] } | VqWq Wd S(   s  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.
    i    N(   R   R   t   Callt   funct   NameR   t   _TRANSIENT_FUNCTIONt   argsR}   t   _astroid_fieldsR;   t   listt   tuplet	   enumeratet   setattrt   get_childrent   _extract_expressions(   R-   t	   real_exprR   t   childt   idxt   compound_childt   result(    (    s.   lib/python2.7/site-packages/astroid/builder.pyR     s"    c         C   st   t  |  t j t j f  r' |  j } n	 |  j } | | k r@ |  Sx- |  j   D] } t | |  } | rM | SqM Wd S(   s  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(	   R   R   t   ClassDeft   FunctionDefRs   t   linenoR   t   _find_statement_by_lineR   (   R-   R"   t	   node_lineR   R   (    (    s.   lib/python2.7/site-packages/astroid/builder.pyR   C  s    	c   	      C   s  d   } g  } xI t  |  j    D]5 \ } } | j   j t  r" | j | d  q" q" Wt |  d | } g  } | r x' | D] } | j t | |   q Wn  | j t	 |   | s | j | j
 d  n  g  | D] } | |  ^ q } t |  d k r| d S| S(   s  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(   R   R   t   ExprR%   (   R-   (    (    s.   lib/python2.7/site-packages/astroid/builder.pyt   _extract  s    i   R   ii    (   R   t
   splitlinest   stripRm   t   _STATEMENT_SELECTORR   R   R   t   extendR   t   bodyt   len(	   R   R   R   t   requested_linesR   R"   t   treet	   extractedR-   (    (    s.   lib/python2.7/site-packages/astroid/builder.pyt   extract_nodef  s"    6	(   i   i    (%   R   t   reR<   t   sysR   R   t   astroidR    R   R   R   R   R   R   R   R   R   R   t   version_infot   tokenizeR   R   R
   R   R#   t   AstroidManagerR3   R/   t   InspectBuilderR0   R   R   R,   R   R   R   R   (    (    (    s.   lib/python2.7/site-packages/astroid/builder.pyt   <module>   s<   						&	#