ó
†Å•Zc           @   sU  d  Z  d d l Z d d l Z d d l m Z d d l m Z m Z m Z m	 Z	 d d l
 m Z m Z d d l m Z d e j f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d „  Z d S(   s0   This module can be used for finding similar codeiÿÿÿÿN(   t   libutils(   t   codeanalyzet
   exceptionst   astt   builtins(   t
   patchedastt	   wildcards(   t   MismatchedTokenErrort   BadNameInCheckErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR      s   t   SimilarFinderc           B   s>   e  Z d  Z d d „ Z i  d d d „ Z d „  Z d „  Z RS(   s   `SimilarFinder` can be used to find similar pieces of code

    See the notes in the `rope.refactor.restructure` module for more
    info.

    c         C   sÁ   | j  |  _ y% t | j  | j ƒ  |  j ƒ |  _ Wn# t k
 rV d | j j GH‚  n X| |  _	 | d k r´ i  |  _ xE t j j j | j j ƒ g D] } | |  j | j ƒ  <q” Wn	 | |  _ d S(   s   Construct a SimilarFinders
   in file %sN(   t   source_codet   sourcet   RawSimilarFindert   get_astt   _does_matcht
   raw_finderR   t   resourcet   patht   pymodulet   NoneR   t   ropet   refactort   DefaultWildcardt   pycoret   projectt   get_name(   t   selfR   R   t   wildcard(    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyt   __init__   s    "		i    c         C   sœ   | |  _  | d  k r' t |  j ƒ } n  d  } d | j d i  ƒ k rz | d d \ } } | |  j j ƒ  k rz | } qz n  |  j j | d | d | d | ƒS(   Nt   skipt    t   startt   end(	   t   argsR   t   lenR   t   getR   t   get_resourceR   t   get_matches(   R   t   codeR#   R!   R"   t   skip_regionR   t   region(    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR'   *   s    	c         o   s,   x% |  j  | | Ž  D] } | j ƒ  Vq Wd  S(   N(   R'   t
   get_region(   R   R#   t   kwdst   match(    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyt   get_match_regions6   s    c         C   sv   |  j  j | d ƒ } d } t | t t f ƒ rG | d } | d } n  t j |  j | | ƒ } |  j | j | | ƒ S(   NR    t   defaulti    i   (	   R#   R%   t
   isinstancet   tuplet   listR   t   SuspectR   t   matches(   R   t   nodet   namet   argt   kindt   suspect(    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR   :   s    
N(   R	   R
   t   __doc__R   R   R'   R.   R   (    (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR      s
   	R   c           B   s\   e  Z d  Z d	 d	 d „ Z d „  Z d „  Z d d	 d	 d „ Z d „  Z d „  Z	 d „  Z
 RS(
   s6   A class for finding similar expressions and statementsc         C   sV   | d  k r t j | ƒ } n  | d  k r9 |  j |  _ n	 | |  _ |  j | | ƒ d  S(   N(   R   R   t   parset   _simple_does_matcht
   does_matcht   _init_using_ast(   R   R   R5   R=   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR   G   s    	c         C   s   t  | t j t j f ƒ S(   N(   R0   R   t   exprt   Name(   R   R5   R6   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR<   P   s    c         C   sA   | |  _  i  |  _ t | d ƒ s4 t j | | ƒ n  | |  _ d  S(   NR*   (   R   t   _matched_astst   hasattrR   t	   patch_astR   (   R   R5   R   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR>   S   s
    		i    c         c   s    | d k r t |  j ƒ } n  x{ |  j | ƒ D]j } | j ƒ  \ } } | | k r. | | k r. | d k	 r | d | k  r | d | k r q. n  | Vq. q. Wd S(   s%  Search for `code` in source and return a list of `Match`\es

        `code` can contain wildcards.  ``${name}`` matches normal
        names and ``${?name} can match any expression.  You can use
        `Match.get_ast()` for getting the node that has matched a
        given pattern.

        i    i   N(   R   R$   R   t   _get_matched_astsR+   (   R   R(   R!   R"   R   R-   t   match_startt	   match_end(    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR'   Z   s    	c         C   sW   | |  j  k rL |  j | ƒ } t |  j | |  j ƒ j ƒ  } | |  j  | <n  |  j  | S(   N(   RA   t   _create_patternt   _ASTMatcherR   R=   t   find_matches(   R   R(   t   wantedR4   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyRD   m   s    c         C   si   |  j  | ƒ } t j | ƒ } | j } t | ƒ d k r_ t | d t j ƒ r_ | d j } n | } | S(   Ni   i    (   t   _replace_wildcardsR   R;   t   bodyR$   R0   t   Exprt   value(   R   t
   expressionR5   t   nodesRJ   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyRG   u   s    	(c         C   sR   t  ƒ  } t | ƒ } i  } x' | j ƒ  D] } | j | ƒ | | <q( W| j | ƒ S(   N(   t   _RopeVariablet   CodeTemplatet	   get_namest   get_vart
   substitute(   R   RO   t   ropevart   templatet   mappingR6   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyRK      s    	N(   R	   R
   R:   R   R   R<   R>   R'   RD   RG   RK   (    (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR   D   s   					RH   c           B   sb   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z RS(
   c         C   s4   | |  _  | |  _ d |  _ t ƒ  |  _ | |  _ d S(   s—   Searches the given pattern in the body AST.

        body is an AST node and pattern can be either an AST node or
        a list of ASTs nodes
        N(   RL   t   patternR   R4   RQ   RV   t   matches_callback(   R   RL   RY   R=   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR   Œ   s
    			c         C   s>   |  j  d  k r7 g  |  _  t j |  j |  j d t ƒn  |  j  S(   Nt	   recursive(   R4   R   R   t   call_for_nodesRL   t   _check_nodet   True(   R   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyRI   ˜   s    	c         C   s3   t  |  j t ƒ r" |  j | ƒ n |  j | ƒ d  S(   N(   R0   RY   R2   t   _check_statementst   _check_expression(   R   R5   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR]   ž   s    c         C   s>   i  } |  j  |  j | | ƒ r: |  j j t | | ƒ ƒ n  d  S(   N(   t   _match_nodesRY   R4   t   appendt   ExpressionMatch(   R   R5   RX   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR`   ¤   s    c         C   sC   x< t  j | ƒ D]+ } t | t t f ƒ r |  j | ƒ q q Wd  S(   N(   R   t   get_childrenR0   R2   R1   t   _ASTMatcher__check_stmt_list(   R   R5   t   child(    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR_   ©   s    c         C   s‘   xŠ t  t | ƒ ƒ D]v } t | ƒ | t |  j ƒ k r | | | t |  j ƒ !} i  } |  j | | ƒ r‰ |  j j t | | ƒ ƒ q‰ q q Wd  S(   N(   t   rangeR$   RY   t   _match_stmtsR4   Rb   t   StatementMatch(   R   RP   t   indext   current_stmtsRX   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyt   __check_stmt_list®   s    c   
      C   sŽ  t  | t j ƒ r= |  j j | j ƒ r= |  j | | | ƒ Sn  t  | t j ƒ sY | | k S| j | j k ro t	 S|  j
 | ƒ } |  j
 | ƒ } t | ƒ t | ƒ k r© t	 SxÞ t | | ƒ D]Í \ } } t  | t j ƒ ró |  j | | | ƒ s†t	 Sq¹ t  | t t f ƒ rvt  | t t f ƒ s6t | ƒ t | ƒ k r:t	 SxI t | | ƒ D]% \ } }	 |  j | |	 | ƒ sJt	 SqJWq¹ | | k r¹ t	 Sq¹ Wt S(   N(   R0   R   R@   RV   t   is_vart   idt   _match_wildcardt   ASTt	   __class__t   Falset   _get_childrenR$   t   zipRa   R2   R1   R^   (
   R   t   expectedR5   RX   t	   children1t	   children2t   child1t   child2t   c1t   c2(    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyRa   ¶   s2    
c         C   s8   t  j | ƒ } g  | D] } t | t  j ƒ s | ^ q S(   s0   Return not `ast.expr_context` children of `node`(   R   Rd   R0   t   expr_context(   R   R5   t   childrenRf   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyRs   Ó   s    c         C   s_   t  | ƒ t  |  j ƒ k r t Sx9 t | |  j ƒ D]% \ } } |  j | | | ƒ s2 t Sq2 Wt S(   N(   R$   RY   Rr   Rt   Ra   R^   (   R   Rk   RX   t   stmtRu   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyRh   Ù   s    c         C   s`   |  j  j | j ƒ } | | k rE |  j | | ƒ rA | | | <t St S|  j | | | i  ƒ Sd  S(   N(   RV   t   get_baseRn   RZ   R^   Rr   Ra   (   R   t   node1t   node2RX   R6   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyRo   á   s    
(   R	   R
   R   RI   R]   R`   R_   Re   Ra   Rs   Rh   Ro   (    (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyRH   Š   s   									t   Matchc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   RX   (   R   RX   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR   î   s    c         C   s   d S(   s   Returns match regionN(    (   R   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR+   ñ   R    c         C   s   |  j  j | d ƒ S(   s3   Return the ast node that has matched rope variablesN(   RX   R%   R   (   R   R6   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR   ô   s    (   R	   R
   R   R+   R   (    (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR‚   ì   s   		Rc   c           B   s   e  Z d  „  Z d „  Z RS(   c         C   s#   t  t |  ƒ j | ƒ | |  _ d  S(   N(   t   superRc   R   R   (   R   R   RX   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR   û   s    c         C   s
   |  j  j S(   N(   R   R*   (   R   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR+   ÿ   s    (   R	   R
   R   R+   (    (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyRc   ù   s   	Ri   c           B   s   e  Z d  „  Z d „  Z RS(   c         C   s#   t  t |  ƒ j | ƒ | |  _ d  S(   N(   Rƒ   Ri   R   t   ast_list(   R   R„   RX   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR     s    c         C   s&   |  j  d j d |  j  d j d f S(   Ni    iÿÿÿÿi   (   R„   R*   (   R   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR+   	  s    (   R	   R
   R   R+   (    (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyRi     s   	RR   c           B   sA   e  Z d  „  Z d „  Z d „  Z d „  Z d Z e d „  ƒ Z	 RS(   c         C   s   | |  _  |  j ƒ  d  S(   N(   RW   t   _find_names(   R   RW   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR     s    	c         C   sÀ   i  |  _  x° t j ƒ  j |  j ƒ D]– } d | j ƒ  k r" | j d ƒ d  k	 r" | j d ƒ \ } } |  j | d | d !} | |  j  k r› g  |  j  | <n  |  j  | j	 | | f ƒ q" q" Wd  S(   NR6   i   i   (
   t   namesRR   t   _get_patternt   finditerRW   t	   groupdictt   groupR   t   spanRb   (   R   R-   R!   R"   R6   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR…     s    	c         C   s   |  j  j ƒ  S(   N(   R†   t   keys(   R   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyRS     s    c         C   s…   t  j |  j ƒ } xM |  j j ƒ  D]< \ } } x- | D]% } | j | d | d | | ƒ q5 Wq" W| j ƒ  } | d  k r |  j S| S(   Ni    i   (   R   t   ChangeCollectorRW   R†   t   itemst
   add_changet   get_changedR   (   R   RX   t	   collectorR6   t   occurrencesR*   t   result(    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyRU   !  s    'c         C   sM   |  j  d  k rF t j ƒ  d t j ƒ  d d } t j | ƒ |  _  n  |  j  S(   Nt   |s   (?P<name>\$\{[^\s\$\}]*\})(   t   _match_patternR   R   t   get_comment_patternt   get_string_patternt   ret   compile(   t   clsRY   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR‡   -  s
    N(
   R	   R
   R   R…   RS   RU   R   R•   t   classmethodR‡   (    (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyRR     s   				
RQ   c           B   sY   e  Z d  Z d Z d Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z RS(
   s.   Transform and identify rope inserted wildcardst   __rope__variable_normal_t   __rope__variable_any_c         C   s-   | j  d ƒ r |  j | ƒ S|  j | ƒ Sd  S(   Nt   ?(   t
   startswitht   _get_anyt   _get_normal(   R   R6   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyRT   =  s    c         C   s   |  j  | ƒ p |  j | ƒ S(   N(   t
   _is_normalt   _is_var(   R   R6   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyRm   C  s    c         C   sH   |  j  | ƒ r  | t |  j ƒ S|  j | ƒ rD d | t |  j ƒ Sd  S(   NRž   (   R¢   R$   t   _normal_prefixR£   t   _any_prefix(   R   R6   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR   F  s    c         C   s   |  j  | S(   N(   R¤   (   R   R6   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR¡   L  s    c         C   s   |  j  | d S(   Ni   (   R¥   (   R   R6   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR    O  s    c         C   s   | j  |  j ƒ S(   N(   RŸ   R¤   (   R   R6   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR¢   R  s    c         C   s   | j  |  j ƒ S(   N(   RŸ   R¥   (   R   R6   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR£   U  s    (   R	   R
   R:   R¤   R¥   RT   Rm   R   R¡   R    R¢   R£   (    (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyRQ   7  s   						c   
      C   s®   t  | ƒ } t j |  ƒ } d „  } t |  d | ƒ} xU | D]M } xD | j d | ƒ D]/ } | j ƒ  \ } } | j | | d | ƒ qW Wq= W| j ƒ  }	 |	 d  k	 rª |	 S|  S(   Nc         S   s   t  |  t j ƒ o |  j | k S(   N(   R0   R   R@   Rn   (   R5   R6   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyR=   ]  s    R=   s   ${%s}(	   t   setR   R   R   R'   R+   R   R   R   (
   R(   t	   variablesR‘   R=   t   findert   variableR-   R!   R"   R“   (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyt   make_patternY  s    	c         C   s   g  } t  |  t j t j f ƒ r/ d |  j ƒ  Sx2 |  j d  k	 rc | j d |  j ƒ  ƒ |  j }  q2 Wt j	 |  j
 ƒ } d j | j d ƒ | ƒ S(   Ns   __builtins__.i    t   .(   R0   R   t   BuiltinClasst   BuiltinFunctionR   t   parentR   t   insertR    t   modnameR   t   joint   split(   t	   pydefinedt   addresst   module_name(    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyt   _pydefined_to_strh  s    (   R:   R˜   t   rope.refactor.wildcardsR   t	   rope.baseR    R   R   R   R   t   rope.refactorR   R   t   rope.refactor.patchedastR   t   RefactoringErrorR   t   objectR   R   RH   R‚   Rc   Ri   RR   RQ   Rª   R¶   (    (    (    s:   lib/python2.7/site-packages/rope/refactor/similarfinder.pyt   <module>   s"   "4Fb

*"	