ó
č¾bc           @   s  d  Z  d d l m Z d d l m Z d d l m Z m Z d d l m	 Z
 d d l m Z d   Z d	   Z d
   Z d   Z d5 d  Z d   Z d   Z d   Z e   e   d  Z d5 d5 d  Z d   Z d   Z d5 d  Z d   Z d5 d  Z d5 d  Z d   Z d   Z d   Z  d   Z! e" d d d d d  d! d" d# d$ d% g
  Z# d&   Z$ d' a% d( a& d) a' e( a) d*   Z* d+   Z+ d,   Z, d-   Z- d.   Z. d/   Z/ d0   Z0 d1   Z1 e" e
 j2 e
 j3 g  Z4 d5 d2  Z5 e" e
 j3 e
 j2 e
 j6 g  Z7 d3   Z8 d5 d4  Z9 d5 S(6   s1   Utility functions, node construction macros, etc.i’’’’(   t   islicei   (   t   token(   t   Leaft   Node(   t   python_symbols(   t   patcompc         C   s%   t  t j |  t t j d  | g  S(   Nu   =(   R   t   symst   argumentR   R   t   EQUAL(   t   keywordt   value(    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt
   KeywordArg   s    	c           C   s   t  t j d  S(   Nu   ((   R   R   t   LPAR(    (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   LParen   s    c           C   s   t  t j d  S(   Nu   )(   R   R   t   RPAR(    (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   RParen   s    c         C   sl   t  |  t  s |  g }  n  t  | t  s? d | _ | g } n  t t j |  t t j d d d g |  S(   s   Build an assignment statementu    u   =t   prefix(	   t
   isinstancet   listR   R   R   t   atomR   R   R   (   t   targett   source(    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   Assign   s    		c         C   s   t  t j |  d | S(   s   Return a NAME leafR   (   R   R   t   NAME(   t   nameR   (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   Name&   s    c         C   s   |  t  t j t   | g  g S(   s   A node tuple for obj.attr(   R   R   t   trailert   Dot(   t   objt   attr(    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   Attr*   s    c           C   s   t  t j d  S(   s   A comma leafu   ,(   R   R   t   COMMA(    (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   Comma.   s    c           C   s   t  t j d  S(   s   A period (.) leafu   .(   R   R   t   DOT(    (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyR   2   s    c         C   sM   t  t j | j   | j   g  } |  rI | j d t  t j |    n  | S(   s-   A parenthesised argument list, used by Call()i   (   R   R   R   t   clonet   insert_childt   arglist(   t   argst   lparent   rparent   node(    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   ArgList6   s    $c         C   s:   t  t j |  t |  g  } | d k	 r6 | | _ n  | S(   s   A function callN(   R   R   t   powerR)   t   NoneR   (   t	   func_nameR%   R   R(   (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   Call=   s    c           C   s   t  t j d  S(   s   A newline literalu   
(   R   R   t   NEWLINE(    (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   NewlineD   s    c           C   s   t  t j d  S(   s   A blank lineu    (   R   R   R.   (    (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt	   BlankLineH   s    c         C   s   t  t j |  d | S(   NR   (   R   R   t   NUMBER(   t   nR   (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   NumberL   s    c         C   s1   t  t j t t j d  |  t t j d  g  S(   s   A numeric or string subscriptu   [u   ](   R   R   R   R   R   t   LBRACEt   RBRACE(   t
   index_node(    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt	   SubscriptO   s    c         C   s   t  t j |  d | S(   s   A string leafR   (   R   R   t   STRING(   t   stringR   (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   StringU   s    c   	      C   s  d |  _  d | _  d | _  t t j d  } d | _  t t j d  } d | _  | | | | g } | rÆ d | _  t t j d  } d | _  | j t t j | | g   n  t t j |  t t j	 |  g  } t t j
 t t j d  | t t j d  g  S(   su   A list comprehension of the form [xp for fp in it if test].

    If test is None, the "if test" part is omitted.
    u    u    u   foru   inu   ifu   [u   ](   R   R   R   R   t   appendR   R   t   comp_ift	   listmakert   comp_forR   R4   R5   (	   t   xpt   fpt   itt   testt   for_leaft   in_leaft
   inner_argst   if_leaft   inner(    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   ListCompY   s$    							"$	c         C   s   x | D] } | j    q Wt t j d  t t j |  d d t t j d d d t t j |  g } t t j |  } | S(   sO    Return an import statement in the form:
        from package import name_leafsu   fromR   u    u   import(   t   removeR   R   R   R   R   t   import_as_namest   import_from(   t   package_namet
   name_leafst   leaft   childrent   imp(    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt
   FromImportq   s    c         C   s¾   t  |  t  r. |  j t   t   g k r. t St  |  t  o½ t |  j  d k o½ t  |  j d t  o½ t  |  j d t  o½ t  |  j d t  o½ |  j d j d k o½ |  j d j d k S(   s(   Does the node represent a tuple literal?i   i    i   i   u   (u   )(	   R   R   RO   R   R   t   Truet   lenR   R
   (   R(   (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   is_tuple   s    *c         C   sz   t  |  t  oy t |  j  d k oy t  |  j d t  oy t  |  j d t  oy |  j d j d k oy |  j d j d k S(   s'   Does the node represent a list literal?i   i    i’’’’u   [u   ](   R   R   RS   RO   R   R
   (   R(   (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   is_list   s    c         C   s   t  t j t   |  t   g  S(   N(   R   R   R   R   R   (   R(   (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   parenthesize£   s    t   sortedR   t   sett   anyt   allt   tuplet   sumt   mint   maxt	   enumeratec         c   s4   t  |  |  } x | r/ | Vt  | |  } q Wd S(   sl  Follow an attribute chain.

    If you have a chain of objects where a.foo -> b, b.foo-> c, etc,
    use this to iterate over all objects in the chain. Iteration is
    terminated by getattr(x, attr) is None.

    Args:
        obj: the starting object
        attr: the name of the chaining attribute

    Yields:
        Each successive object in the chain.
    N(   t   getattr(   R   R   t   next(    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt
   attr_chainŖ   s    	se   for_stmt< 'for' any 'in' node=any ':' any* >
        | comp_for< 'for' any 'in' node=any any* >
     s³   
power<
    ( 'iter' | 'list' | 'tuple' | 'sorted' | 'set' | 'sum' |
      'any' | 'all' | 'enumerate' | (any* trailer< '.' 'join' >) )
    trailer< '(' node=any ')' >
    any*
>
s`   
power<
    ( 'sorted' | 'enumerate' )
    trailer< '(' arglist<node=any any*> ')' >
    any*
>
c         C   s¤   t  s< t j t  a t j t  a t j t  a t a  n  t t t g } xR t | t |  d   D]8 \ } } i  } | j	 | |  rd | d |  k rd t Sqd Wt
 S(   s    Returns true if node is in an environment where all that is required
        of it is being iterable (ie, it doesn't matter if it returns a list
        or an iterator).
        See test_map_nochange in test_fixers.py for some examples and tests.
        t   parentR(   (   t
   pats_builtR   t   compile_patternt   p0t   p1t   p2RR   t   zipRb   t   matcht   False(   R(   t   patternst   patternRc   t   results(    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   in_special_contextŠ   s    	%"c         C   sŁ   |  j  } | d k	 r+ | j t j k r+ t S|  j } | j t j t j	 f k rS t S| j t j
 k r| | j d |  k r| t S| j t j k sŃ | j t j k rÕ | d k	 r¾ | j t j k sŃ | j d |  k rÕ t St S(   sG   
    Check that something isn't an attribute or function name etc.
    i    N(   t   prev_siblingR+   t   typeR   R!   Rk   Rc   R   t   funcdeft   classdeft	   expr_stmtRO   t
   parameterst   typedargslistR   RR   (   R(   t   prevRc   (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   is_probably_builtinć   s    		%c         C   sp   xi |  d k	 rk |  j t j k r_ t |  j  d k r_ |  j d } | j t j k r_ | j Sn  |  j	 }  q Wd S(   s   Find the indentation of *node*.i   i   u    N(
   R+   Rq   R   t   suiteRS   RO   R   t   INDENTR
   Rc   (   R(   t   indent(    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   find_indentationś   s    '
c         C   sW   |  j  t j k r |  S|  j   }  |  j d  } |  _ t t j |  g  } | | _ | S(   N(   Rq   R   Ry   R"   Rc   R+   R   (   R(   Rc   Ry   (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt
   make_suite  s    	c         C   s;   x4 |  j  t j k r6 |  j }  |  s t d   q q W|  S(   s   Find the top level namespace.s,   root found before file_input node was found.(   Rq   R   t
   file_inputRc   t
   ValueError(   R(   (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt	   find_root  s
    	c         C   s"   t  | t |  |   } t |  S(   sŽ    Returns true if name is imported from package at the
        top level of the tree which node belongs to.
        To cover the case of an import like 'import foo', use
        None for the package and 'foo' for the name. (   t   find_bindingR   t   bool(   t   packageR   R(   t   binding(    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   does_tree_import  s    c         C   s   |  j  t j t j f k S(   s0   Returns true if the node is an import statement.(   Rq   R   t   import_nameRK   (   R(   (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt	   is_import"  s    c         C   sÆ  d   } t  |  } t |  | |  r+ d Sd } } xn t | j  D]] \ } } | |  sc qE n  x1 t | j |  D] \ } } | |  sw Pqw qw W| | } PqE W| d k rxb t | j  D]N \ } } | j t j k rĀ | j rĀ | j d j t j k rĀ | d } PqĀ qĀ Wn  |  d k r\t
 t j t t j d  t t j | d d g  }	 n$ t |  t t j | d d g  }	 |	 t   g }
 | j | t
 t j |
   d S(   s\    Works like `does_tree_import` but adds an import statement
        if it was not imported. c         S   s,   |  j  t j k o+ |  j o+ t |  j d  S(   Ni    (   Rq   R   t   simple_stmtRO   R   (   R(   (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   is_import_stmt)  s    Ni    i   u   importR   u    (   R   R   R_   RO   Rq   R   R   R   R8   R+   R   R   R   R   RQ   R/   R#   (   R   R   R(   R   t   roott
   insert_post   offsett   idxt   node2t   import_RO   (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   touch_import&  s4    	
 

	!$c         C   sK  xD| j  D]9} d } | j t j k rs t |  | j  d  rB | St |  t | j  d  |  } | r | } q n­| j t j t j	 f k ræ t |  t | j  d  |  } | r | } q na| j t j
 k r|t |  t | j  d  |  } | r’ | } q xt | j  d  D]b \ } } | j t j k r| j d k rt |  t | j  | d  |  } | ru| } quqqWn¤ | j t k rŖ| j  d j |  k rŖ| } nv t | |  |  rÅ| } n[ | j t j k rģt |  | |  } n4 | j t j k r t |  | j  d  r | } q n  | r
 | s0| St |  rC| Sq
 q
 Wd S(	   sŗ    Returns the node which binds variable name, otherwise None.
        If optional argument package is supplied, only imports will
        be returned.
        See test cases for examples.i   i’’’’i   i   t   :i   i    N(   RO   R+   Rq   R   t   for_stmtt   _findR   R}   t   if_stmtt
   while_stmtt   try_stmtR_   R   t   COLONR
   t	   _def_symst   _is_import_bindingR   Rt   R   (   R   R(   R   t   childt   retR2   t   it   kid(    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyR   T  sH      	 !# %		c         C   s|   | g } xl | rw | j    } | j d k rO | j t k rO | j | j  q | j t j k r | j |  k r | Sq Wd  S(   Ni   (	   t   popRq   t   _block_symst   extendRO   R   R   R
   R+   (   R   R(   t   nodes(    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyR     s    		!c         C   s'  |  j  t j k r| r|  j d } | j  t j k r£ xŌ | j D]Z } | j  t j k rw | j d j | k r |  SqB | j  t j k rB | j | k rB |  SqB Wq#| j  t j k rź | j d } | j  t j k r| j | k r|  Sq#| j  t j k r#| j | k r#|  Sn|  j  t j	 k r#| rMt
 |  j d  j   | k rMd S|  j d } | rst d |  rsd S| j  t j k rt | |  r|  S| j  t j k rß| j d } | j  t j k r | j | k r |  Sq#| j  t j k r| j | k r|  S| r#| j  t j k r#|  Sn  d S(   s    Will reuturn node if node will import name, or node
        will import * from package.  None is returned otherwise.
        See test cases for examples. i   i   i’’’’i   u   asN(   Rq   R   R   RO   t   dotted_as_namest   dotted_as_nameR
   R   R   RK   t   unicodet   stripR+   R   RJ   t   import_as_namet   STAR(   R(   R   R   RP   R   t   lastR2   (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyR     s@    !!!%!!!N(:   t   __doc__t	   itertoolsR    t   pgen2R   t   pytreeR   R   t   pygramR   R   t    R   R   R   R   R   R+   R   R   R    R   R)   R-   R/   R0   R3   R7   R:   RH   RQ   RT   RU   RV   RX   t   consuming_callsRb   Rf   Rg   Rh   Rk   Rd   Ro   Rx   R|   R}   R   R   R   R   Rs   Rr   R   R   R   R   R   R   (    (    (    s(   /usr/lib/python2.7/lib2to3/fixer_util.pyt   <module>   sZ   																									-*	
