ó
†Å•Zc           @   s©   d  d l  Z  d  d l m Z m Z m Z m Z m Z d  d l m Z d  d l m	 Z	 m
 Z
 m Z d  d l m Z d e f d „  ƒ  YZ d „  Z d	 e f d
 „  ƒ  YZ d S(   iÿÿÿÿN(   t   changet
   taskhandlet   builtinst   astt   codeanalyze(   t   libutils(   t
   patchedastt   similarfindert   sourceutils(   t   module_importst   Restructurec           B   sk   e  Z d  Z d d d d „ Z d d d e j ƒ  d „ Z d „  Z d „  Z	 d „  Z
 d „  Z e d „ Z RS(	   s  A class to perform python restructurings

    A restructuring transforms pieces of code matching `pattern` to
    `goal`.  In the `pattern` wildcards can appear.  Wildcards match
    some piece of code based on their kind and arguments that are
    passed to them through `args`.

    `args` is a dictionary of wildcard names to wildcard arguments.
    If the argument is a tuple, the first item of the tuple is
    considered to be the name of the wildcard to use; otherwise the
    "default" wildcard is used.  For getting the list arguments a
    wildcard supports, see the pydoc of the wildcard.  (see
    `rope.refactor.wildcard.DefaultWildcard` for the default
    wildcard.)

    `wildcards` is the list of wildcard types that can appear in
    `pattern`.  See `rope.refactor.wildcards`.  If a wildcard does not
    specify its kind (by using a tuple in args), the wildcard named
    "default" is used.  So there should be a wildcard with "default"
    name in `wildcards`.

    `imports` is the list of imports that changed modules should
    import.  Note that rope handles duplicate imports and does not add
    the import if it already appears.

    Example #1::

      pattern ${pyobject}.get_attribute(${name})
      goal ${pyobject}[${name}]
      args pyobject: instance=rope.base.pyobjects.PyObject

    Example #2::

      pattern ${name} in ${pyobject}.get_attributes()
      goal ${name} in {pyobject}
      args pyobject: instance=rope.base.pyobjects.PyObject

    Example #3::

      pattern ${pycore}.create_module(${project}.root, ${name})
      goal generate.create_module(${project}, ${name})

      imports
       from rope.contrib import generate

      args
       project: type=rope.base.project.Project

    Example #4::

      pattern ${pow}(${param1}, ${param2})
      goal ${param1} ** ${param2}
      args pow: name=mod.pow, exact

    Example #5::

      pattern ${inst}.longtask(${p1}, ${p2})
      goal
       ${inst}.subtask1(${p1})
       ${inst}.subtask2(${p2})
      args
       inst: type=mod.A,unsure

    c         C   s…   | |  _  | |  _ | |  _ | |  _ |  j d k r? i  |  _ n  | |  _ |  j d k rc g  |  _ n  | |  _ t j |  j ƒ |  _	 d S(   s_   Construct a restructuring

        See class pydoc for more info about the arguments.

        N(
   t   projectt   patternt   goalt   argst   Nonet   importst	   wildcardsR   t   CodeTemplatet   template(   t   selfR   R   R   R   R   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyt   __init__K   s    						c         C   sÞ  | d k	 rX t j d t d d ƒx3 | j ƒ  D]" \ } } t j | ƒ |  j | <q/ Wn  | d k	 r† t j d t d d ƒ| |  _ n  t	 j
 d |  j |  j f ƒ } | d k	 râ g  | D]! } t j |  j | ƒ r¸ | ^ q¸ }	 n |  j j ƒ  }	 | j d t |	 ƒ ƒ }
 xÎ |	 D]Æ } |
 j | j ƒ |  j j | ƒ } t j | d |  j ƒ} t | j |  j |  j ƒ ƒ } |  j | | ƒ } | j ƒ  } | d k	 rÌ|  j | | |  j ƒ } | j t	 j | | ƒ ƒ n  |
 j ƒ  qW| S(	   só  Get the changes needed by this restructuring

        `resources` can be a list of `rope.base.resources.File`\s to
        apply the restructuring on.  If `None`, the restructuring will
        be applied to all python files.

        `checks` argument has been deprecated.  Use the `args` argument
        of the constructor.  The usage of::

          strchecks = {'obj1.type': 'mod.A', 'obj2': 'mod.B',
                       'obj3.object': 'mod.C'}
          checks = restructuring.make_checks(strchecks)

        can be replaced with::

          args = {'obj1': 'type=mod.A', 'obj2': 'name=mod.B',
                  'obj3': 'object=mod.C'}

        where obj1, obj2 and obj3 are wildcard names that appear
        in restructuring pattern.

        s]   The use of checks parameter is deprecated; use the args parameter of the constructor instead.t
   stackleveli   s^   The use of imports parameter is deprecated; use imports parameter of the constructor, instead.s   Restructuring <%s> to <%s>s   Collecting ChangesR   N(    R   t   warningst   warnt   DeprecationWarningt   itemsR   t   _pydefined_to_strR   R   R    t	   ChangeSetR   R   R   t   is_python_fileR   t   get_python_filest   create_jobsett   lent   started_jobt   patht   get_pymodulet   SimilarFinderR   t   listt   get_matchest   _compute_changest   get_changedt   _add_importst
   add_changet   ChangeContentst   finished_job(   R   t   checksR   t	   resourcest   task_handlet   namet   valuet   changest   resourcet   filest   job_sett   pymodulet   findert   matchest   computert   resultt   imported_source(    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyt   get_changes^   sB    	$c         C   s%   t  | j | j ƒ  | j |  j | ƒ S(   N(   t   _ChangeComputert   source_codet   get_astt   linesR   (   R   R8   R6   (    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyR'   ›   s    c         C   sq   | s
 | S|  j  | | ƒ } t j |  j | | ƒ } t j |  j | ƒ } x | D] } | j | ƒ qP W| j ƒ  S(   N(   t   _get_import_infosR   t   get_string_moduleR   R	   t   ModuleImportst
   add_importt   get_changed_source(   R   R3   t   sourceR   t   import_infosR6   t   import_info(    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyR)       s    c         C   sS   t  j |  j d j | ƒ | ƒ } t j |  j | ƒ } g  | j D] } | j ^ q@ S(   Ns   
(   R   RB   R   t   joinR	   RC   R   RH   (   R   R3   R   R6   (    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyRA   ª   s
    c         C   su   i  } xh | j  ƒ  D]Z \ } } | j d ƒ o< | j d ƒ } |  j | d | ƒ} | d k	 r | | | <q q W| S(   sp   Convert str to str dicts to str to PyObject dicts

        This function is here to ease writing a UI.

        s   .objects   .typet	   is_pynameN(   R   t   endswitht	   _evaluateR   (   R   t   string_checksR-   t   keyR1   RJ   t	   evaluated(    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyt   make_checks±   s    c         C   s¦   | j  d ƒ } d  } | d d k rG d t f d „  ƒ  Y} | ƒ  } n |  j j | d ƒ } x8 | d D], } | | } | d  k rˆ d  S| j ƒ  } qh W| r¢ | S| S(	   Nt   .i    t   __builtin__t   __builtins__t   _BuiltinsStubc           B   s   e  Z d  „  Z RS(   c         S   s   t  j  | S(   N(   R   (   R   R0   (    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyt   get_attributeÅ   s    (   t   __name__t
   __module__RU   (    (    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyRT   Ä   s   i   (   RR   RS   (   t   splitR   t   objectR   t
   get_modulet
   get_object(   R   t   codeRJ   t
   attributest   pynameRT   t   pyobjectt	   attribute(    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyRL   À   s    
N(   RV   RW   t   __doc__R   R   R   t   NullTaskHandleR<   R'   R)   RA   RP   t   TrueRL   (    (    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyR
   	   s   @	<		
		c   
      C   s‰   t  j |  ƒ } t | j | ƒ ƒ } t j |  ƒ } t j |  ƒ } t  j | ƒ } t	 |  | | | | ƒ } | j
 ƒ  }	 |	 d k r… |  S|	 S(   s   used by other refactoringsN(   R   t   RawSimilarFinderR%   R&   R   t   get_patched_astR   t   SourceLinesAdapterR   R=   R(   R   (
   R\   R   R   R7   R8   R   R@   R   R9   R:   (    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyt   replaceÒ   s    R=   c           B   sJ   e  Z d  „  Z d „  Z d „  Z d „  Z e d „ Z d „  Z d „  Z	 RS(   c         C   sv   | |  _  | |  _ | |  _ | |  _ | |  _ i  |  _ i  |  _ |  j ƒ  rr x$ |  j D] } | |  j | j <qU Wn  d  S(   N(   RF   R   R8   R   R@   t   matched_astst   _nearest_rootst   _is_expression(   R   R\   R   R@   R   R8   t   match(    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyR   â   s    							c         C   sÊ   |  j  ƒ  r5 |  j |  j ƒ } | |  j k r1 d  S| St j |  j ƒ } d } xl |  j D]a } | j ƒ  \ } } | | k  r |  j  ƒ  s qW q n  | } |  j	 | ƒ } | j
 | | | ƒ qW W| j ƒ  Sd  S(   Niÿÿÿÿ(   Rj   t   _get_node_textR   RF   R   R   t   ChangeCollectorR8   t
   get_regiont   _get_matched_textR*   R(   (   R   R:   t	   collectort   last_endRk   t   startt   endt   replacement(    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyR(   î   s     	c         C   s    |  j  o t |  j  d t j ƒ S(   Ni    (   R8   t
   isinstanceR   t   ExpressionMatch(   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyRj     s    c         C   s®   i  } xy |  j  j ƒ  D]h } | j | ƒ } | d  k rM t j d | ƒ ‚ n  |  j ƒ  oe | j | k } |  j | | ƒ | | <q W|  j  j	 | ƒ } |  j
 | j ƒ  d | ƒ S(   Ns   Unknown name <%s>i    (   R   t	   get_namesR?   R   R   t   BadNameInCheckErrorRj   R   Rl   t
   substitutet   _auto_indentRn   (   R   Rk   t   mappingR0   t   nodet   forcet
   unindented(    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyRo     s    c   
      C   sÑ   | r* | |  j  k r* |  j |  j  | ƒ St j | ƒ \ } } |  j | | !} t j | ƒ } xP |  j | ƒ D]? } t j | ƒ \ } } | j | | | | |  j	 | ƒ ƒ qn W| j
 ƒ  }	 |	 d  k rÍ | S|	 S(   N(   Rh   Ro   R   t   node_regionRF   R   Rm   t   _get_nearest_rootsR*   Rl   R(   R   (
   R   R|   R}   Rr   Rs   t	   main_textRp   t	   sub_startt   sub_endR:   (    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyRl     s    c         C   s™   |  j  j | ƒ } t j |  j  | ƒ } g  } x\ t | j t ƒ ƒ D]E \ } } | d k r{ | j ƒ  r{ | j d | ƒ n  | j | ƒ qC Wd j	 | ƒ S(   Ni    t    t    (
   R@   t   get_line_numberR   t   get_indentst	   enumeratet
   splitlinesRc   t   stript   appendRI   (   R   t   offsett   textt   linenot   indentsR:   t   indext   line(    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyRz      s    "c         C   s   | |  j  k rt g  } xL t j | ƒ D]; } | |  j k rJ | j | ƒ q% | j |  j | ƒ ƒ q% W| |  j  | <n  |  j  | S(   N(   Ri   R   t   get_child_nodesRh   R‹   t   extendR€   (   R   R|   R:   t   child(    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyR€   *  s    (
   RV   RW   R   R(   Rj   Ro   t   FalseRl   Rz   R€   (    (    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyR=   à   s   					
(   R   t	   rope.baseR    R   R   R   R   R   t   rope.refactorR   R   R   t   rope.refactor.importutilsR	   RY   R
   Rg   R=   (    (    (    s8   lib/python2.7/site-packages/rope/refactor/restructure.pyt   <module>   s   (É	