ó
&^Ê[c           @   sÂ  d  Z  d d l m Z m Z m Z m Z m Z m Z d d l m	 Z	 m
 Z
 d d l m Z m Z d d l m Z d d l m
 Z
 m Z m Z m Z m Z m Z d d l Z d „  Z d d	 „ Z d d
 „ Z d d „ Z d „  Z d „  Z d „  Z d „  Z d d d „ Z e j e j e j  e j! e j" f Z# e j$ e j% f Z& d „  Z' d d „ Z( d „  Z) d „  Z* d „  Z+ d „  Z, d „  Z- d „  Z. d „  Z/ d Z0 d Z1 d „  Z2 d „  Z3 d d „ Z4 d S(   så   
Utility functions from 2to3, 3to2 and python-modernize (and some home-grown
ones).

Licences:
2to3: PSF License v2
3to2: Apache Software License (from 3to2/setup.py)
python-modernize licence: BSD (from python-modernize/LICENSE)
iÿÿÿÿ(   t
   FromImportt   Newlinet	   is_importt	   find_roott   does_tree_importt   Comma(   t   Leaft   Node(   t   python_symbolst   python_grammar(   t   token(   R   t   Callt   Namet   symsR   t   NumberNc         C   sÂ   d |  k r |  S|  j  d ƒ r, |  d }  n  g  | D]$ } | j d j |  ƒ ƒ r3 | ^ q3 } t | ƒ d k r• t d d j d „  | Dƒ ƒ ƒ ‚ n! t | ƒ d	 k r¶ t d
 ƒ ‚ n  | d	 Sd S(   sl  
    Examples:
    >>> canonical_fix_name('fix_wrap_text_literals')
    'libfuturize.fixes.fix_wrap_text_literals'
    >>> canonical_fix_name('wrap_text_literals')
    'libfuturize.fixes.fix_wrap_text_literals'
    >>> canonical_fix_name('wrap_te')
    ValueError("unknown fixer name")
    >>> canonical_fix_name('wrap')
    ValueError("ambiguous fixer name")
    s   .fix_t   fix_i   s   fix_{0}i   sO   Ambiguous fixer name. Choose a fully qualified module name instead from these:
s   
c         s   s   |  ] } d  | Vq d S(   s     N(    (   t   .0t   myf(    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pys	   <genexpr>-   s    i    s1   Unknown fixer. Use --list-fixes or -l for a list.N(   t
   startswitht   endswitht   formatt   lent
   ValueErrort   join(   t   fixt   avail_fixest   ft   found(    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyt   canonical_fix_name   s    $ c         C   s   t  t j d d |  ƒS(   Nu   *t   prefix(   R   R
   t   STAR(   R   (    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyt   Star6   s    c         C   s   t  t j d d |  ƒS(   Nu   **R   (   R   R
   t
   DOUBLESTAR(   R   (    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyt
   DoubleStar9   s    c         C   s   t  t j d d |  ƒS(   Nu   -R   (   R   R
   t   MINUS(   R   (    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyt   Minus<   s    c         C   s?   g  } x+ |  D]# } | j  | ƒ | j  t ƒ  ƒ q W| d =| S(   s{   
    Accepts/turns: (Name, Name, ..., Name, Name)
    Returns/into: (Name, Comma, Name, Comma, ..., Name, Comma, Name)
    iÿÿÿÿ(   t   appendR   (   t   leafst	   new_leafst   leaf(    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyt	   commatize?   s    c         C   s¬   x1 |  j  d k	 r3 |  j  j t j k r3 |  j  }  q W|  j  d k rG d S|  j t j k r` |  j S|  j d k	 rŽ |  j j t j k rŽ |  j j S|  j d k r¡ d S|  j	 Sd S(   sf   
    Returns the indentation for this node
    Iff a node is in a suite, then it has indentation.
    u    N(
   t   parentt   Nonet   typeR   t   suiteR
   t   INDENTt   valuet   prev_siblingR   (   t   node(    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyt   indentationK   s    '$
c         C   s@   t  |  ƒ } t d „  | j ƒ  Dƒ ƒ } | s2 d St | ƒ Sd S(   s  
    Dirty little trick to get the difference between each indentation level
    Implemented by finding the shortest indentation string
    (technically, the "least" of all of the indentation strings, but
    tabs and spaces mixed won't get this far, so those are synonymous.)
    c         s   s*   |  ]  } | j  t j k r | j Vq d  S(   N(   R+   R
   R-   R.   (   R   t   i(    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pys	   <genexpr>i   s    u       N(   R   t   sett	   pre_ordert   min(   R0   t   rt   all_indents(    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyt   indentation_step`   s
    c         C   så   x' |  j  D] } | j t j k r
 d Sq
 Wx? t |  j  ƒ D]" \ } } | j t j k r: Pq: q: Wt d ƒ ‚ t t j t	 ƒ  t
 t j t | ƒ t | ƒ ƒ g ƒ } |  j  | d } | j ƒ  d | _ | j | ƒ |  j | ƒ d S(   sj   
    Turn the stuff after the first colon in parent's children
    into a suite, if it wasn't already
    Nu   No class suite and no ':'!i   u    (   t   childrenR+   R   R,   t	   enumerateR
   t   COLONR   R   R   R   R-   R1   R8   t   removeR   t   append_child(   R)   R0   R2   R,   t   one_node(    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyt   suitifyp   s    7
	c         C   sw   | d k r d } n  t d d | ƒ|  g } | d k	 rg | j t d d d ƒt | d d ƒg ƒ n  t t j | ƒ S(   s—   
    Accepts a package (Name node), name to import it as (string), and
    optional prefix and returns a node:
    import <package> [as <as_name>]
    u    u   importR   u   asu    N(   R*   R   t   extendR   R   t   import_name(   t   packaget   as_nameR   R9   (    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyt
   NameImportˆ   s    	c         c   s‡  |  j  t k s t ‚ |  j } xC | j  t j k rc | j } | j  t j k rR Pn | V| j } q! W|  j } | j  t j	 k s… t ‚ | j } x | d k	 r® | V| j } q‘ W| j } | j  t k r | } x$ | j d k	 ró | j V| j } qÐ W| j } n  | j } | d k rd Sxg | j  t k r‚| j  t j k rE| Vn  | j } | d k r| j } | j } | d k rPqqqWd S(   s‘   
    Generator yields all nodes for which a node (an import_stmt) has scope
    The purpose of this is for a call to _find() on each of them
    N(   R+   t   _import_stmtst   AssertionErrort   next_siblingR
   t   SEMIt   NEWLINER)   R   t   simple_stmtR*   t   _compound_stmtsR,   (   R0   t   testt   nxtR)   t   contextt   ct   p(    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyt   import_binding_scope™   sB    									c         C   sg   t  |  ƒ } t  d d d ƒ} t  | d d ƒ} t t j | | | g ƒ } | d  k	 rc | | _ n  | S(   Nu   asR   u    (   R   R   R   t   import_as_nameR*   R   (   t   nameRC   R   t   new_namet   new_ast   new_as_namet   new_node(    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyt   ImportAsNameÔ   s    c         C   s>   |  j  t j k o= t |  j ƒ d k o= |  j d j  t j k S(   s<   
    Returns True if the node appears to be a docstring
    i    (   R+   R   RJ   R   R9   R
   t   STRING(   R0   (    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyt   is_docstringÞ   s    c         C   s2  t  | ƒ } t d |  | ƒ r" d Sd } xv t | j ƒ D]e \ } } t | ƒ s\ t | ƒ re | } n  t | ƒ rw q8 n  t | ƒ } | s Pn  |  | k r8 d Sq8 Wt	 d t
 t j |  d d ƒg ƒ } | d k r| d k r| j d j | _ d | j d _ n  | t ƒ  g } | j | t t j | ƒ ƒ d S(   s   
    This seems to work
    u
   __future__NR   t    i    u    (   R   R   R*   R:   R9   t   is_shebang_commentt   is_encoding_commentRZ   t   check_future_importR    R   R
   t   NAMER   R   t   insert_childR   R   RJ   (   t   featureR0   t   roott   shebang_encoding_idxt   idxt   namest   import_R9   (    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyt   future_importæ   s(    	$c   	      C   s6  t  | ƒ } t d |  | ƒ r" d Sd } x_ t | j ƒ D]N \ } } | j t j k r8 | j r8 | j d j t j k r8 | d } Pq8 q8 WxP | j | D]; } | j t j	 k rÀ | d 7} q˜ n  | j
 } d | _
 Pq˜ Wd } t d t t j |  d d ƒg ƒ } | t ƒ  g } | j | t t j | d | ƒƒ d S(   sD   
    An alternative to future_import() which might not work ...
    u
   __future__Ni    i   u    R   u    (   R   R   R:   R9   R+   R   RJ   R
   RY   RI   R   R    R   R_   R   R`   R   (	   Ra   R0   Rb   t
   insert_posRd   t   thing_afterR   Rf   R9   (    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyt   future_import2  s(    

		$c         C   sÔ   g  |  D] } | j  t j k r | ^ q }  t g  | D] } | d f ^ q5 ƒ } x} t |  ƒ D]o \ } } | j  t j k r¸ | j d j  t j	 k r¸ | j d j
 } | j d | | <q] | | } | | | <q] W| S(   u/   
    Parse a list of arguments into a dict
    i   i    i   N(   R+   R
   t   COMMAt   dictR*   R:   R   t   argumentR9   t   EQUALR.   (   t   arglistt   schemeR2   t   kt   ret_mappingt   argt   slot(    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyt
   parse_args.  s    +%+
c         C   s,   |  j  t j k o+ |  j o+ t |  j d ƒ S(   Ni    (   R+   R   RJ   R9   R   (   R0   (    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyt   is_import_stmtH  s    c         C   sÿ  t  | ƒ } t |  | | ƒ r" d St } x6 d d d d g D]" } t d | | ƒ r; t } Pq; q; W| rd \ } } xo t | j ƒ D]^ \ } } t | ƒ rƒ | } | }	 x3 | rÜ | j } |	 d 7}	 t | ƒ sª |	 } Pqª qª WPqƒ qƒ W| d k	 s÷ t	 ‚ | d k	 s	t	 ‚ | }
 nL xC t | j ƒ D]2 \ } } | j
 t j k rDPn  t | ƒ s"Pq"q"W| }
 |  d k r£t t j t t j d ƒ t t j | d	 d
 ƒg ƒ } nÒ t |  t t j | d	 d
 ƒg ƒ } | d k rot t j t t j t t j d ƒ t t j t t j d ƒ t t j d ƒ g ƒ t t j t t j d ƒ t t j d ƒ g ƒ g ƒ g ƒ } | t ƒ  g } n g  } | t ƒ  g } | j |
 j } d | j |
 _ | j |
 t t j | d	 | ƒƒ t | ƒ d k rû| j |
 d t t j | ƒ ƒ n  d S(   sÍ  Works like `does_tree_import` but adds an import statement at the
    top if it was not imported (but below any __future__ imports) and below any
    comments such as shebang lines).

    Based on lib2to3.fixer_util.touch_import()

    Calling this multiple times adds the imports in reverse order.

    Also adds "standard_library.install_aliases()" after "from future import
    standard_library".  This should probably be factored into another function.
    Nt   absolute_importt   divisiont   print_functiont   unicode_literalst
   __future__i   u   importR   u    u   standard_libraryu   .u   install_aliasesu   (u   )u    i    (   NN(   R   R   t   Falset   TrueR*   R:   R9   R^   RG   RF   R+   R   RJ   RZ   R   RA   R   R
   R_   R    t   powert   trailert   DOTt   LPARt   RPARR   R   R`   R   (   RB   t   name_to_importR0   Rb   R   RS   t   startt   endRd   t   idx2Rh   Rf   t   install_hookst   children_hookst   children_importt
   old_prefix(    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyt   touch_import_topM  sh    		
		!$		$"c         C   sÕ  |  } |  j  t j k o |  j s( t ƒ  S|  j d }  |  j  t j k op t |  j d d ƒ op |  j d j d k sz t ƒ  S|  j d j  t j	 k r£ |  j d }  n |  j d }  |  j  t j
 k rRt ƒ  } x€ |  j D]u } | j  t j k r | j | j ƒ qÕ | j  t j k rÕ | j d } | j  t j k s7t ‚ | j | j ƒ qÕ qÕ W| S|  j  t j k r™|  j d }  |  j  t j k s‰t ‚ t |  j g ƒ S|  j  t j k r»t |  j g ƒ St sÑt d | ƒ ‚ d S(	   sZ   If this is a future import, return set of symbols that are imported,
    else return None.i    i   R.   u
   __future__i   i   s   strange import: %sN(   R+   R   RJ   R9   R3   t   import_fromt   hasattrR.   R
   R   t   import_as_namesR_   t   addRR   RF   R|   (   R0   t   savenodet   resultt   n(    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyR^   °  s8    	s   ^#!.*pythons   ^#.*coding[:=]\s*([-\w.]+)c         C   s   t  t j t |  j ƒ ƒ S(   sÍ   
    Comments are prefixes for Leaf nodes. Returns whether the given node has a
    prefix that looks like a shebang line or an encoding line:

        #!/usr/bin/env python
        #!/usr/bin/python3
    (   t   boolt   ret   matcht   SHEBANG_REGEXR   (   R0   (    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyR\   Þ  s    c         C   s   t  t j t |  j ƒ ƒ S(   s  
    Comments are prefixes for Leaf nodes. Returns whether the given node has a
    prefix that looks like an encoding line:

        # coding: utf-8
        # encoding: utf-8
        # -*- coding: <encoding name> -*-
        # vim: set fileencoding=<encoding name> :
    (   R“   R”   R•   t   ENCODING_REGEXR   (   R0   (    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyR]   é  s    
c         C   sj   t  | ƒ d k s t ‚ t  | ƒ d k rK | \ } } | t ƒ  | g } n | } t t |  ƒ | d | ƒS(   sö   
    Example:
    >>> wrap_in_fn_call("oldstr", (arg,))
    oldstr(arg)

    >>> wrap_in_fn_call("olddiv", (arg1, arg2))
    olddiv(arg1, arg2)

    >>> wrap_in_fn_call("olddiv", [arg1, comma, arg2, comma, arg3])
    olddiv(arg1, arg2, arg3)
    i    i   R   (   R   RF   R   R   R   (   t   fn_namet   argsR   t   expr1t   expr2t   newargs(    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyt   wrap_in_fn_callö  s    (5   t   __doc__t   lib2to3.fixer_utilR    R   R   R   R   R   t   lib2to3.pytreeR   R   t   lib2to3.pygramR   R   R	   R
   R   R   R   R”   R   R*   R   R!   R#   R(   R1   R8   R?   RD   t   if_stmtt
   while_stmtt   for_stmtt   try_stmtt	   with_stmtRK   RA   RŒ   RE   RQ   RX   RZ   Rg   Rj   Ru   Rv   R‹   R^   R–   R—   R\   R]   R   (    (    (    s5   lib/python2.7/site-packages/libfuturize/fixer_util.pyt   <module>	   s>   ..	"				$	;
		(	 			c	*		