ó
†Å•Zc           @   s•  d  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
 e
 f d „  ƒ  YZ e e d e d e e d „ Z d e
 f d „  ƒ  YZ d „  Z e d „ Z 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 S(!   s  Find occurrences of a name in a project.

This module consists of a `Finder` that finds all occurrences of a name
in a project. The `Finder.find_occurrences()` method is a generator that
yields `Occurrence` instances for each occurrence of the name. To create
a `Finder` object, use the `create_finder()` function:

    finder = occurrences.create_finder(project, 'foo', pyname)
    for occurrence in finder.find_occurrences():
        pass

It's possible to filter the occurrences. They can be specified when
calling the `create_finder()` function.

  * `only_calls`: If True, return only those instances where the name is
    a function that's being called.

  * `imports`: If False, don't return instances that are in import
    statements.

  * `unsure`: If a prediate function, return instances where we don't
    know what the name references. It also filters based on the
    predicate function.

  * `docs`: If True, it will search for occurrences in regions normally
    ignored. E.g., strings and comments.

  * `in_hierarchy`: If True, it will find occurrences if the name is in
    the class's hierarchy.

  * `instance`: Used only when you want implicit interfaces to be
    considered.

  * `keywords`: If False, don't return instances that are the names of keyword
    arguments
iÿÿÿÿN(   t   codeanalyze(   t   evaluate(   t
   exceptions(   t   pynames(   t	   pyobjects(   t   utils(   t   wordert   Finderc           B   s2   e  Z d  Z d „  g e d „ Z d d d „ Z RS(   s  For finding occurrences of a name

    The constructor takes a `filters` argument.  It should be a list
    of functions that take a single argument.  For each possible
    occurrence, these functions are called in order with the an
    instance of `Occurrence`:

      * If it returns `None` other filters are tried.
      * If it returns `True`, the occurrence will be a match.
      * If it returns `False`, the occurrence will be skipped.
      * If all of the filters return `None`, it is skipped also.

    c         C   s   t  S(   N(   t   True(   t   o(    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyt   <lambda>@   t    c         C   s=   | |  _  | |  _ | |  _ | |  _ t | d | ƒ|  _ d  S(   Nt   docs(   t   projectt   nameR   t   filterst   _TextualFindert   _textual_finder(   t   selfR   R   R   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyt   __init__@   s
    				c         c   s˜   t  |  j d | d | d |  j ƒ} xm |  j j | j ƒ D]V } t | | ƒ } x> |  j D]3 } | | ƒ } | d k r} qY n  | r‹ | Vn  PqY Wq: Wd S(   s   Generate `Occurrence` instancest   resourcet   pymoduleR   N(	   t   _OccurrenceToolsCreatorR   R   R   t   find_offsetst   source_codet
   OccurrenceR   t   None(   R   R   R   t   toolst   offsett
   occurrencet   filtert   result(    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyt   find_occurrencesG   s    N(   t   __name__t
   __module__t   __doc__t   FalseR   R   R    (    (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR   1   s   c
         C   s-  t  | g ƒ }
 g  } | r. | j t ƒ  ƒ n  | sG | j t ƒ  ƒ n  |	 s` | j t ƒ  ƒ n  t | t j ƒ r¸ xC | j ƒ  D]2 } y |
 j	 | | ƒ Wq t
 j k
 r° q Xq Wn  x= |
 D]5 } | j t | ƒ ƒ | r¿ | j t | ƒ ƒ q¿ q¿ W| r| j t | ƒ ƒ n  t |  | d | d | ƒS(   s¶   A factory for `Finder`

    Based on the arguments it creates a list of filters.  `instance`
    argument is needed only when you want implicit interfaces to be
    considered.

    R   R   (   t   sett   appendt   CallsFiltert   NoImportsFiltert   NoKeywordsFiltert
   isinstanceR   t   ParameterNamet   get_objectst   addR   t   AttributeNotFoundErrort   PyNameFiltert   InHierarchyFiltert   UnsureFilterR   (   R   R   t   pynamet
   only_callst   importst   unsureR   t   instancet   in_hierarchyt   keywordst   pynames_R   t   pyobject(    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyt   create_finderV   s*    
R   c           B   s¹   e  Z d  „  Z e j d „  ƒ Z e j d „  ƒ Z e j d „  ƒ Z e j d „  ƒ Z e j d „  ƒ Z	 d „  Z
 d „  Z d „  Z d	 „  Z d
 „  Z d „  Z e e j d „  ƒ ƒ Z RS(   c         C   s"   | |  _  | |  _ | j |  _ d  S(   N(   R   R   R   (   R   R   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR   y   s    		c         C   s   |  j  j j |  j ƒ S(   N(   R   t   word_findert   get_word_rangeR   (   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR=   ~   s    c         C   s   |  j  j j |  j ƒ S(   N(   R   R<   t   get_primary_rangeR   (   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR>   ‚   s    c         C   s5   y |  j  j j |  j ƒ SWn t j k
 r0 n Xd  S(   N(   R   t   name_findert   get_pyname_atR   R   t   BadIdentifierError(   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyt
   get_pyname†   s    c         C   s5   y |  j  j j |  j ƒ SWn t j k
 r0 n Xd  S(   N(   R   R?   t   get_primary_and_pyname_atR   R   RA   (   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyt   get_primary_and_pyname   s
    c         C   s.   |  j  j j |  j ƒ p- |  j  j j |  j ƒ S(   N(   R   R<   t   is_from_statementR   t   is_import_statement(   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyt   is_in_import_statement•   s    c         C   s   |  j  j j |  j ƒ S(   N(   R   R<   t   is_a_function_being_calledR   (   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyt	   is_calledš   s    c         C   s   |  j  j j |  j ƒ S(   N(   R   R<   t%   is_a_class_or_function_name_in_headerR   (   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyt
   is_defined   s    c         C   s.   |  j  j j |  j ƒ p- |  j  j j |  j ƒ S(   N(   R   R<   RJ   R   t   is_a_name_after_from_import(   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyt   is_a_fixed_primary¡   s    c         C   s   |  j  j j |  j ƒ S(   N(   R   R<   t   is_assigned_hereR   (   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyt
   is_written¦   s    c         C   s   t  |  j ƒ  ƒ S(   N(   t   unsure_pynameRB   (   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyt	   is_unsure©   s    c         C   s   |  j  j j |  j ƒ S(   N(   R   R<   t   is_function_keyword_parameterR   (   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyRR   ¬   s    c         C   s&   |  j  ƒ  d } |  j j j j | ƒ S(   Ni    (   R=   R   R   t   linest   get_line_number(   R   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyt   lineno°   s    (   R!   R"   R   R   t   saveitR=   R>   RB   RD   RG   RI   RK   RM   RO   RQ   RR   t   propertyRU   (    (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR   w   s   							c         C   sš   |  d k s | d k r t S|  | k r, t St |  ƒ t j t j f k rl t | ƒ t j t j f k rl t S|  j ƒ  | j ƒ  k o™ |  j ƒ  | j ƒ  k S(   s2   Check whether `expected` and `pyname` are the sameN(	   R   R$   R   t   typeR   t   ImportedModulet   ImportedNamet   get_definition_locationt
   get_object(   t   expectedR2   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyt   same_pyname·   s    		c         C   sM   |  d k r t S| r- t |  t j ƒ r- t S|  j ƒ  t j ƒ  k rI t Sd S(   s8   Return `True` if we don't know what this name referencesN(	   R   R   R*   R   t   UnboundNameR$   R\   R   t   get_unknown(   R2   t   unbound(    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyRP   Æ   s    R/   c           B   s    e  Z d  Z d „  Z d „  Z RS(   s"   For finding occurrences of a name.c         C   s   | |  _  d  S(   N(   R2   (   R   R2   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR   Ó   s    c         C   s    t  |  j | j ƒ  ƒ r t Sd  S(   N(   R^   R2   RB   R   (   R   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyt   __call__Ö   s    (   R!   R"   R#   R   Rb   (    (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR/   Ð   s   	R0   c           B   s5   e  Z d  Z e d „ Z d „  Z d „  Z d „  Z RS(   s=   Finds the occurrence if the name is in the class's hierarchy.c         C   ss   | |  _  | |  _ |  j | ƒ |  _ |  j d  k	 rf | j ƒ  j ƒ  |  _ |  j |  j |  j ƒ |  _	 n	 d  |  _	 d  S(   N(
   R2   t	   impl_onlyt   _get_containing_classt   pyclassR   R\   t   get_nameR   t   _get_root_classest   roots(   R   R2   t   implementations_only(    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR   Þ   s    		c         C   sf   |  j  d  k r d  S|  j | j ƒ  ƒ } | d  k	 rb |  j | |  j ƒ } |  j  j | ƒ rb t Sn  d  S(   N(   Rh   R   Rd   RB   Rg   R   t   intersectionR   (   R   R   Re   Rh   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyRb   è   s    c         C   sY   t  | t j ƒ rU | j ƒ  j ƒ  } | j } | d  k	 rU | j ƒ  d k rU | j Sn  d  S(   Nt   Class(	   R*   R   t   DefinedNameR\   t	   get_scopet   parentR   t   get_kindR:   (   R   R2   t   scopeRn   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyRd   ñ   s
    	c         C   s„   |  j  r% | |  j k r% t | g ƒ St ƒ  } x< | j ƒ  D]. } | | k r; | j |  j | | ƒ ƒ q; q; W| s€ t | g ƒ S| S(   N(   Rc   Re   R%   t   get_superclassest   updateRg   (   R   Re   R   R   t
   superclass(    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyRg   ø   s    	 (   R!   R"   R#   R$   R   Rb   Rd   Rg   (    (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR0   Û   s
   
			R1   c           B   s    e  Z d  Z d „  Z d „  Z RS(   s:   Occurrences where we don't knoow what the name references.c         C   s   | |  _  d  S(   N(   R5   (   R   R5   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR     s    c         C   s#   | j  ƒ  r |  j | ƒ r t Sd  S(   N(   RQ   R5   R   (   R   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyRb   
  s    (   R!   R"   R#   R   Rb   (    (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR1     s   	R(   c           B   s   e  Z d  Z d „  Z RS(   s/   Don't include import statements as occurrences.c         C   s   | j  ƒ  r t Sd  S(   N(   RG   R$   (   R   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyRb     s    (   R!   R"   R#   Rb   (    (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR(     s   R'   c           B   s   e  Z d  Z d „  Z RS(   s    Filter out non-call occurrences.c         C   s   | j  ƒ  s t Sd  S(   N(   RI   R$   (   R   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyRb     s    (   R!   R"   R#   Rb   (    (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR'     s   R)   c           B   s   e  Z d  Z d „  Z RS(   s   Filter out keyword parameters.c         C   s   | j  ƒ  r t Sd  S(   N(   RR   R$   (   R   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyRb   "  s    (   R!   R"   R#   Rb   (    (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR)     s   R   c           B   sb   e  Z e d  „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 e d „  ƒ Z RS(	   c         C   sa   | |  _  | |  _ t j d d g ƒ |  _ t j d t j ƒ  g ƒ |  _ |  j |  j  ƒ |  _	 d  S(   Nt   comments   #[^\n]*t   string(
   R   R   R   t   anyt   comment_patternR    t   get_string_patternt   string_patternt   _get_occurrence_patternt   pattern(   R   R   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR   )  s    		c         c   sQ   |  j  | ƒ s d  S|  j r( |  j } n	 |  j } x | | ƒ D] } | Vq> Wd  S(   N(   t   _fast_file_queryR   t   _normal_searcht
   _re_search(   R   t   sourcet   searchert   matched(    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR   1  s    		c         c   sg   x` |  j  j | ƒ D]L } xC | j ƒ  j ƒ  D]/ \ } } | r, | d k r, | j | ƒ Vq, q, Wq Wd  S(   NR   (   R{   t   finditert	   groupdictt   itemst   start(   R   R   t   matcht   keyt   value(    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR~   ;  s    c         c   sª   d } x t  r¥ y~ | j |  j | ƒ } | t |  j ƒ } | d k s^ |  j | | d ƒ rŒ | t | ƒ k s„ |  j | | ƒ rŒ | Vn  Wq	 t k
 r¡ Pq	 Xq	 Wd  S(   Ni    i   (   R   t   indexR   t   lent   _is_id_chart
   ValueError(   R   R   t   currentt   found(    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR}   A  s    	c         C   s   | j  ƒ  p | d k S(   Nt   _(   t   isalnum(   R   t   c(    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR‹   O  s    c         C   s1   y | j  |  j ƒ t SWn t k
 r, t SXd  S(   N(   R‰   R   R   RŒ   R$   (   R   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR|   R  s
    c         C   s!   | d  k	 r | j ƒ  S| j Sd  S(   N(   R   t   readR   (   R   R   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyt   _get_sourceY  s    
c         C   sF   t  j d d | d g ƒ } t j | d |  j d |  j ƒ } | S(   NR   s   \bt   |(   R   Rv   t   ret   compileRw   Ry   (   R   R   t   occurrence_patternR{   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyRz   _  s
    	c         C   s   d |  d j  | ƒ d S(   Ns   (?P<%s>R”   t   )(   t   join(   R   t   list_(    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyRv   f  s    (   R!   R"   R$   R   R   R~   R}   R‹   R|   R“   Rz   t   staticmethodRv   (    (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR   '  s   	
						R   c           B   s’   e  Z d d e d  „ Z e e j d „  ƒ ƒ Z e e j d „  ƒ ƒ Z	 e e j d „  ƒ ƒ Z
 e e j d „  ƒ ƒ Z e e j d „  ƒ ƒ Z RS(   c         C   s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   R   t!   _OccurrenceToolsCreator__resourcet!   _OccurrenceToolsCreator__pymoduleR   (   R   R   R   R   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR   m  s    			c         C   s   t  j |  j ƒ S(   N(   R   t   ScopeNameFinderR   (   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR?   s  s    c         C   s*   |  j  d  k	 r |  j j ƒ  S|  j j Sd  S(   N(   Rœ   R   R   R’   R   R   (   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR   x  s    c         C   s   t  j |  j |  j ƒ S(   N(   R   t   WorderR   R   (   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR<   €  s    c         C   s3   |  j  d  k	 r |  j  S|  j d  k	 r/ |  j j Sd  S(   N(   Rœ   R   R   R   (   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR   …  s    c         C   s)   |  j  d  k	 r |  j  S|  j j |  j ƒ S(   N(   R   R   R   t   get_pymoduleR   (   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR     s    N(   R!   R"   R   R$   R   RW   R   RV   R?   R   R<   R   R   (    (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyR   k  s   (   R#   R•   t	   rope.baseR    R   R   R   R   R   R   t   objectR   R$   R   R   R;   R   R^   RP   R/   R0   R1   R(   R'   R)   R   R   (    (    (    s8   lib/python2.7/site-packages/rope/refactor/occurrences.pyt   <module>$   s.   %@	
)D