;
Ðâ"Ic               @   s"  d  Z  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 e d „ Z d „  Z d „  Z d „  Z Gd „  d e ƒ Z Gd „  d e ƒ Z d „  Z d S(   uÛ   Refactoring framework.

Used as a main program, this can refactor any number of files and/or
recursively descend down directories.  Imported as a module, this
provides infrastructure to write your own refactoring tool.
u#   Guido van Rossum <guido@python.org>i    N(   u   defaultdict(   u   chaini   (   u   driver(   u   tokenize(   u   pytree(   u   patcomp(   u   fixes(   u   pygramc             C   s­   t  |  g  g  d g ƒ } t j j | j ƒ } g  } xs t t j | ƒ ƒ D]\ } | j d ƒ oF | j d ƒ o6 | o | d d … } n | j	 | d d … ƒ qI qI W| S(   uE   Return a sorted list of all available fix names in the given package.u   *u   fix_u   .pyi   Niýÿÿÿ(
   u
   __import__u   osu   pathu   dirnameu   __file__u   sortedu   listdiru
   startswithu   endswithu   append(   u	   fixer_pkgu   remove_prefixu   pkgu	   fixer_diru	   fix_namesu   name(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   get_all_fix_names"   s      c             C   sÓ   t  |  t j t j f ƒ o t |  j g ƒ St  |  t j ƒ o& |  j o t |  j ƒ St d g ƒ St  |  t j
 ƒ oF t ƒ  } x5 |  j D]* } x! | D] } | j t | ƒ ƒ q™ WqŒ W| St d |  ƒ ‚ d S(   uf    Accepts a pytree Pattern Node and returns a set
        of the pattern types which will match first. u$   Oh no! I don't understand pattern %sN(   u
   isinstanceu   pytreeu   NodePatternu   LeafPatternu   setu   typeu   NegatedPatternu   contentu   get_head_typesu   Noneu   WildcardPatternu   updateu	   Exception(   u   patu   ru   pu   x(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   get_head_types.   s    
	
  c             C   sn   t  t ƒ } x[ |  D]S } | j p | d j | ƒ q n x( t | j ƒ D] } | | j | ƒ qK Wq W| S(   u^    Accepts a list of fixers and returns a dictionary
        of head node type --> fixer list.  N(   u   defaultdictu   listu   patternu   Noneu   appendu   get_head_types(   u
   fixer_listu
   head_nodesu   fixeru   t(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   get_headnode_dictG   s     
 c                s   ‡  f d †  t  ˆ  d ƒ Dƒ S(   uN   
    Return the fully qualified names for fixers in the package pkg_name.
    c                s#   g  } |  ] } | ˆ  d  | q
 S(   u   .(    (   u   .0u   _[1]u   fix_name(   u   pkg_name(    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu
   <listcomp>W   s   F(   u   get_all_fix_namesu   False(   u   pkg_name(    (   u   pkg_nameu-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   get_fixers_from_packageS   s    c             B   s   |  Ee  Z d  Z d S(   u   A fixer could not be loaded.N(   u   __name__u
   __module__u   __doc__(   u
   __locals__(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu
   FixerError[   s   
u
   FixerErrorc             B   s  |  Ee  Z i d d  6Z d Z d Z d d d „ Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d d d	 „ Z d d d
 „ Z d d d „ Z d „  Z d d „ Z d „  Z d „  Z d d d „ Z d „  Z d Z d Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   u   print_functionu   Fixu   fix_c             C   s  | |  _  | p g  |  _ |  j j ƒ  |  _ | d k	 o |  j j | ƒ n g  |  _ t j	 d ƒ |  _
 g  |  _ d |  _ |  j d o t j j d =n t j t j d t j d |  j
 ƒ|  _ |  j ƒ  \ |  _ |  _ t |  j ƒ |  _ t |  j ƒ |  _ g  |  _ d S(   uÒ   Initializer.

        Args:
            fixer_names: a list of fixers to import
            options: an dict with configuration.
            explicit: a list of fixers to run even if they are explicit.
        u   RefactoringToolu   print_functionu   printu   convertu   loggerNF(   u   fixersu   explicitu   _default_optionsu   copyu   optionsu   Noneu   updateu   errorsu   loggingu	   getLoggeru   loggeru	   fixer_logu   Falseu   wroteu   pygramu   python_grammaru   keywordsu   driveru   Driveru   pytreeu   convertu
   get_fixersu	   pre_orderu
   post_orderu   get_headnode_dictu   files(   u   selfu   fixer_namesu   optionsu   explicit(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   __init__f   s$    					c          
   C   sá  g  } g  } x™|  j  D]Ž} t | i  i  d g ƒ } | j d d ƒ d } | j |  j ƒ o | t |  j ƒ d … } n | j d ƒ } |  j d j d „  | Dƒ ƒ } y t	 | | ƒ } Wn* t
 k
 o t d	 | | f ƒ ‚ Yn X| |  j |  j ƒ }	 |	 j o7 |  j d k	 o' | |  j k o |  j d
 | ƒ q n |  j d | ƒ |	 j d k o | j |	 ƒ q |	 j d k o | j |	 ƒ q t d |	 j ƒ ‚ q Wt j d ƒ }
 | j d |
 ƒ | j d |
 ƒ | | f S(   u  Inspects the options to load the requested patterns and handlers.

        Returns:
          (pre_order, post_order), where pre_order is the list of fixers that
          want a pre-order AST traversal, and post_order is the list that want
          post-order traversal.
        u   *u   .i   iÿÿÿÿNu   _u    c             S   s!   g  } |  ] } | | j  ƒ  q
 S(    (   u   title(   u   .0u   _[1]u   p(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu
   <listcomp>“   s    u   Can't find %s.%su   Skipping implicit fixer: %su   Adding transformation: %su   preu   postu   Illegal fixer order: %ru	   run_orderu   keyT(   u   fixersu
   __import__u   rsplitu
   startswithu   FILE_PREFIXu   lenu   splitu   CLASS_PREFIXu   joinu   getattru   AttributeErroru
   FixerErroru   optionsu	   fixer_logu   explicitu   Trueu   log_messageu	   log_debugu   orderu   appendu   operatoru
   attrgetteru   sort(   u   selfu   pre_order_fixersu   post_order_fixersu   fix_mod_pathu   modu   fix_nameu   partsu
   class_nameu	   fix_classu   fixeru   key_func(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu
   get_fixersƒ   s:    
  c             O   s   ‚  d S(   u   Called when an error occurs.N(    (   u   selfu   msgu   argsu   kwds(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu	   log_error«   s    c             G   s)   | o | | } n |  j  j | ƒ d S(   u   Hook to log a message.N(   u   loggeru   info(   u   selfu   msgu   args(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   log_message¯   s    c             G   s)   | o | | } n |  j  j | ƒ d  S(   N(   u   loggeru   debug(   u   selfu   msgu   args(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu	   log_debugµ   s    c             C   s   d S(   u0   Called with lines of output to give to the user.N(    (   u   selfu   lines(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   print_outputº   s    c             C   sR   xK | D]C } t  j j | ƒ o |  j | | | ƒ q |  j | | | ƒ q Wd S(   u)   Refactor a list of files and directories.N(   u   osu   pathu   isdiru   refactor_diru   refactor_file(   u   selfu   itemsu   writeu   doctests_onlyu   dir_or_file(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   refactor¾   s
     c       	      C   sÃ   x¼ t  j | ƒ D]« \ } } } |  j d | ƒ | j ƒ  | j ƒ  x[ | D]S } | j d ƒ o< | j d ƒ o, t  j j | | ƒ } |  j | | | ƒ qJ qJ Wd „  | Dƒ | d d … <q Wd S(   uÄ   Descends down a directory and refactor every Python file found.

        Python files are assumed to have a .py extension.

        Files and subdirectories starting with '.' are skipped.
        u   Descending into %su   .u   pyc             S   s/   g  } |  ]! } | j  d  ƒ p | | q
 q
 S(   u   .(   u
   startswith(   u   .0u   _[1]u   dn(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu
   <listcomp>Ö   s    N(	   u   osu   walku	   log_debugu   sortu
   startswithu   endswithu   pathu   joinu   refactor_file(	   u   selfu   dir_nameu   writeu   doctests_onlyu   dirpathu   dirnamesu	   filenamesu   nameu   fullname(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   refactor_dirÆ   s     

 !c       	      C   s;  y t  | ƒ } Wn> t k
 o2 } z |  j d | | ƒ d SWYd d } ~ Xn Xz | j ƒ  d } Wd | j ƒ  X| o` |  j d | ƒ |  j | | ƒ } | | k o |  j | | | d | ƒq7|  j d | ƒ n^ |  j | | ƒ } | o4 | j	 o* |  j t
 | ƒ d d … | d | ƒn |  j d | ƒ d S(	   u   Refactors a file.u   Can't open %s: %sNu   
u   Refactoring doctests in %su   writeu   No doctest changes in %siÿÿÿÿu   No changes in %s(   u   openu   IOErroru	   log_erroru   readu   closeu	   log_debugu   refactor_docstringu   processed_fileu   refactor_stringu   was_changedu   str(	   u   selfu   filenameu   writeu   doctests_onlyu   fu   erru   inputu   outputu   tree(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   refactor_fileØ   s$    *c             C   s„   y |  j  j | ƒ } WnG t k
 o; } z% |  j d | | j j | ƒ d SWYd d } ~ Xn X|  j d | ƒ |  j | | ƒ | S(   uF  Refactor a given input string.

        Args:
            data: a string holding the code to be refactored.
            name: a human-readable name for use in error/log messages.

        Returns:
            An AST corresponding to the refactored input stream; None if
            there were errors during the parse.
        u   Can't parse %s: %s: %sNu   Refactoring %s(   u   driveru   parse_stringu	   Exceptionu	   log_erroru	   __class__u   __name__u	   log_debugu   refactor_tree(   u   selfu   datau   nameu   treeu   err(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   refactor_stringò   s    	c             C   s»   t  j j ƒ  } | oT |  j d ƒ |  j | d ƒ } | | k o |  j | d | ƒ q· |  j d ƒ nN |  j | d ƒ } | o' | j o |  j t | ƒ d | ƒ n |  j d ƒ d  S(   Nu   Refactoring doctests in stdinu   <stdin>u   No doctest changes in stdinu   No changes in stdin(	   u   sysu   stdinu   readu	   log_debugu   refactor_docstringu   processed_fileu   refactor_stringu   was_changedu   str(   u   selfu   doctests_onlyu   inputu   outputu   tree(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   refactor_stdin  s    c             C   s¨   t  t  |  j j ƒ  Œ  t  |  j j ƒ  Œ  ƒ } x | D] } | j | | ƒ q4 W|  j |  j | j ƒ  ƒ |  j |  j | j ƒ  ƒ x | D] } | j | | ƒ q‡ W| j S(   uA  Refactors a parse tree (modifying the tree in place).

        Args:
            tree: a pytree.Node instance representing the root of the tree
                  to be refactored.
            name: a human-readable name for this tree.

        Returns:
            True if the tree was modified, False otherwise.
        (   u   chainu	   pre_orderu   valuesu
   post_orderu
   start_treeu   traverse_byu   finish_treeu   was_changed(   u   selfu   treeu   nameu
   all_fixersu   fixer(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   refactor_tree  s      c             C   s·   | p d Sx¤ | D]œ } x“ | | j  | d D]| } | j | ƒ } | o` | j | | ƒ } | d k	 o= | | k p t | ƒ t | ƒ k o | j | ƒ | } q« q/ q/ Wq Wd S(   u  Traverse an AST, applying a set of fixers to each node.

        This is a helper method for refactor_tree().

        Args:
            fixers: a list of fixer instances.
            traversal: a generator that yields AST nodes.

        Returns:
            None
        N(   u   typeu   Noneu   matchu	   transformu   stru   replace(   u   selfu   fixersu	   traversalu   nodeu   fixeru   resultsu   new(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   traverse_by1  s      c             C   s   |  j  j | ƒ | d k ov y t | d ƒ } Wn> t k
 o2 } z |  j d | | ƒ d SWYd d } ~ Xn Xz | j ƒ  } Wd | j ƒ  Xn | | k o |  j d | ƒ d S|  j	 t
 | | | ƒ ƒ | o |  j | | | ƒ n |  j d | ƒ d S(   uP   
        Called when a file has been refactored, and there are changes.
        u   ru   Can't read %s: %sNu   No changes to %su   Not writing changes to %s(   u   filesu   appendu   Noneu   openu   IOErroru	   log_erroru   readu   closeu	   log_debugu   print_outputu
   diff_textsu
   write_file(   u   selfu   new_textu   filenameu   old_textu   writeu   fu   err(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   processed_fileI  s"    c             C   s×   y t  | d ƒ } WnA t j k
 o2 } z |  j d | | ƒ d SWYd d } ~ Xn XzU y | j | ƒ Wn= t j k
 o. } z |  j d | | ƒ WYd d } ~ Xn XWd | j ƒ  X|  j d | ƒ d |  _ d S(   uÑ   Writes a string to a file.

        It first shows a unified diff between the old text and the new text, and
        then rewrites the file; the latter is only done if the write option is
        set.
        u   wu   Can't create %s: %sNu   Can't write %s: %su   Wrote changes to %sT(	   u   openu   osu   erroru	   log_erroru   writeu   closeu	   log_debugu   Trueu   wrote(   u   selfu   new_textu   filenameu   old_textu   fu   err(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu
   write_filea  s    +u   >>> u   ... c       
      C   s™  g  } d } d } d } d } x;| j d ƒ D]*} | d 7} | j ƒ  j |  j ƒ oe | d k	 o# | j |  j | | | | ƒ ƒ n | } | g } | j |  j ƒ }	 | d |	 … } q. | d k	 oF | j | |  j	 ƒ p | | |  j	 j
 ƒ  d k o | j | ƒ q. | d k	 o# | j |  j | | | | ƒ ƒ n d } d } | j | ƒ q. W| d k	 o# | j |  j | | | | ƒ ƒ n d j | ƒ S(   uË  Refactors a docstring, looking for doctests.

        This returns a modified version of the input string.  It looks
        for doctests, which start with a ">>>" prompt, and may be
        continued with "..." prompts, as long as the "..." is indented
        the same as the ">>>".

        (Unfortunately we can't use the doctest module's parser,
        since, like most parsers, it is not geared towards preserving
        the original source.)
        i    i   Nu   
u    T(   u   Noneu
   splitlinesu   Trueu   lstripu
   startswithu   PS1u   extendu   refactor_doctestu   findu   PS2u   rstripu   appendu   join(
   u   selfu   inputu   filenameu   resultu   blocku   block_linenou   indentu   linenou   lineu   i(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   refactor_docstringz  s<     
	c       
         sp  y ˆ j  | | ˆ  ƒ } WnŽ t k
 o‚ } zl ˆ j j t j ƒ o. x+ | D] } ˆ j d | j d ƒ ƒ qL Wn ˆ j d | | | j	 j
 | ƒ | SWYd d } ~ Xn Xˆ j | | ƒ o¯ t | ƒ j d	 ƒ } | d | d … | | d d … }	 } | d j d ƒ p | d c d 7<n ˆ  ˆ j | j d ƒ g } | o! | ‡  ‡ f d †  | Dƒ 7} qln | S(
   uÞ   Refactors one doctest.

        A doctest is given as a block of lines, the first of which starts
        with ">>>" (possibly indented), while the remaining lines start
        with "..." (identically indented).

        u
   Source: %su   
u+   Can't parse docstring in %s line %s: %s: %sNi   iÿÿÿÿi    c                s&   g  } |  ] } | ˆ  ˆ j  | q
 S(    (   u   PS2(   u   .0u   _[1]u   line(   u   indentu   self(    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu
   <listcomp>¿  s    T(   u   parse_blocku	   Exceptionu   logu   isEnabledForu   loggingu   DEBUGu	   log_debugu   rstripu	   log_erroru	   __class__u   __name__u   refactor_treeu   stru
   splitlinesu   Trueu   endswithu   PS1u   pop(
   u   selfu   blocku   linenou   indentu   filenameu   treeu   erru   lineu   newu   clipped(    (   u   indentu   selfu-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   refactor_doctest¥  s&     !	)%c          	   C   s'  |  j  o
 d } n d } |  j p |  j d | ƒ n2 |  j d | ƒ x |  j D] } |  j | ƒ qR W|  j o2 |  j d ƒ x" |  j D] } |  j | ƒ qŠ Wn |  j ot t |  j ƒ d k o |  j d ƒ n |  j d t |  j ƒ ƒ x1 |  j D]" \ } } } |  j | | | Ž qù Wn d  S(	   Nu   wereu
   need to beu   No files %s modified.u   Files that %s modified:u$   Warnings/messages while refactoring:i   u   There was 1 error:u   There were %d errors:(   u   wroteu   filesu   log_messageu	   fixer_logu   errorsu   len(   u   selfu   wereu   fileu   messageu   msgu   argsu   kwds(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu	   summarizeÂ  s*    



 

 

 c             C   s   |  j  j |  j | | | ƒ ƒ S(   u³   Parses a block into a tree.

        This is necessary to get correct line number / offset information
        in the parser diagnostics and embedded into the parse tree.
        (   u   driveru   parse_tokensu	   wrap_toks(   u   selfu   blocku   linenou   indent(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   parse_blockÙ  s    c             c   sŠ   t  j |  j | | ƒ j ƒ } xe | D]] \ } } \ } } \ }	 }
 } | | d 7} |	 | d 7}	 | | | | f |	 |
 f | f Vq% Wd S(   u;   Wraps a tokenize stream to systematically modify start/end.i   N(   u   tokenizeu   generate_tokensu	   gen_linesu   __next__(   u   selfu   blocku   linenou   indentu   tokensu   typeu   valueu   line0u   col0u   line1u   col1u	   line_text(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu	   wrap_toksá  s     !c             c   s¥   | |  j  } | |  j } | } xs | D]k } | j | ƒ o | t | ƒ d … Vn7 | | j ƒ  d k o	 d Vn t d | | f ƒ ‚ | } q' Wx d Vq™ d S(   u–   Generates lines as expected by tokenize from a list of lines.

        This strips the first len(indent + self.PS1) characters off each line.
        Nu   
u   line=%r, prefix=%ru    (   u   PS1u   PS2u
   startswithu   lenu   rstripu   AssertionError(   u   selfu   blocku   indentu   prefix1u   prefix2u   prefixu   line(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu	   gen_linesï  s     	
NF(   u   __name__u
   __module__u   Falseu   _default_optionsu   CLASS_PREFIXu   FILE_PREFIXu   Noneu   __init__u
   get_fixersu	   log_erroru   log_messageu	   log_debugu   print_outputu   refactoru   refactor_diru   refactor_fileu   refactor_stringu   refactor_stdinu   refactor_treeu   traverse_byu   processed_fileu
   write_fileu   PS1u   PS2u   refactor_docstringu   refactor_doctestu	   summarizeu   parse_blocku	   wrap_toksu	   gen_lines(   u
   __locals__(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   RefactoringTool_   s4   
	(									+				u   RefactoringToolc          	   C   s:   |  j  ƒ  }  | j  ƒ  } t j |  | | | d d d d ƒS(   u%   Return a unified diff of two strings.u
   (original)u   (refactored)u   linetermu    (   u
   splitlinesu   difflibu   unified_diff(   u   au   bu   filename(    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu
   diff_texts  s
    	(   u   __doc__u
   __author__u   osu   sysu   difflibu   loggingu   operatoru   collectionsu   defaultdictu	   itertoolsu   chainu   pgen2u   driveru   tokenizeu    u   pytreeu   patcompu   fixesu   pygramu   Trueu   get_all_fix_namesu   get_head_typesu   get_headnode_dictu   get_fixers_from_packageu	   Exceptionu
   FixerErroru   objectu   RefactoringToolu
   diff_texts(    (    (    u-   /mit/python/lib/python3.0/lib2to3/refactor.pyu   <module>
   s,   			ÿ ¥