ó
†Å•Zc           @   s¿   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	 d  d
 l
 m Z d e f d „  ƒ  YZ d „  Z d S(   iÿÿÿÿN(   t   builtins(   t
   exceptions(   t   libutils(   t   pynames(   t	   pyobjects(   t	   resources(   t   resourceobserver(   t
   taskhandle(   t   importutilst
   AutoImportc           B   sÎ   e  Z d  Z e e d „ Z d „  Z d „  Z d „  Z d „  Z	 d d e j ƒ  d „ Z d e j ƒ  d „ Z d „  Z d	 „  Z d d
 „ Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sÁ   A class for finding the module that provides a name

    This class maintains a cache of global names in python modules.
    Note that this cache is not accurate and might be out of date.

    c         C   s–   | |  _  | |  _ | j j d ƒ |  _ |  j d k rB i  |  _ n  | j j |  j ƒ t j	 d |  j
 d |  j d |  j ƒ } | r’ | j | ƒ n  d S(   sÇ   Construct an AutoImport object

        If `observe` is `True`, listen for project changes and update
        the cache.

        If `underlined` is `True`, underlined names are cached, too.
        t   globalnamest   changedt   movedt   removedN(   t   projectt
   underlinedt
   data_filest	   read_datat   namest   Nonet   add_write_hookt   _writeR   t   ResourceObservert   _changedt   _movedt   _removedt   add_observer(   t   selfR   t   observeR   t   observer(    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyt   __init__   s    			c         C   s[   g  } xN |  j  D]C } x: |  j  | D]+ } | j | ƒ r$ | j | | f ƒ q$ q$ Wq W| S(   s   Return a list of ``(name, module)`` tuples

        This function tries to find modules that have a global name
        that starts with `starting`.
        (   R   t
   startswitht   append(   R   t   startingt   resultt   modulet   global_name(    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyt   import_assist*   s    c         C   sA   g  } x4 |  j  D]) } | |  j  | k r | j | ƒ q q W| S(   s2   Return the list of modules that have global `name`(   R   R    (   R   t   nameR"   R#   (    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyt   get_modules8   s
    c         C   s;   t  ƒ  } x+ |  j D]  } | j t  |  j | ƒ ƒ q W| S(   s*   Return the list of all cached global names(   t   setR   t   update(   R   R"   R#   (    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyt   get_all_names@   s    	c         C   sÛ   g  } xÎ |  j  D]Ã } | |  j  | k r y |  j j | ƒ } | | k r¸ | | } | j ƒ  \ } } | d k	 r¸ | j ƒ  j ƒ  } | d k	 rµ | d k	 rµ | j | | f ƒ qµ q¸ n  WqÓ t j k
 rÏ qÓ Xq q W| S(   s.   Return a list of ``(resource, lineno)`` tuplesN(	   R   R   t
   get_modulet   get_definition_locationR   t   get_resourceR    R   t   ModuleNotFoundError(   R   R&   R"   R#   t   pymodulet   pynamet   linenot   resource(    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyt   get_name_locationsG   s    
 c         C   sy   | d k r |  j j ƒ  } n  | j d t | ƒ ƒ } x< | D]4 } | j d | j ƒ |  j | | ƒ | j ƒ  q= Wd S(   sß   Generate global name cache for project files

        If `resources` is a list of `rope.base.resource.File`\s, only
        those files are searched; otherwise all python modules in the
        project are cached.

        s   Generatig autoimport caches   Working on <%s>N(	   R   R   t   get_python_filest   create_jobsett   lent   started_jobt   patht   update_resourcet   finished_job(   R   R   R   t   task_handlet   job_sett   file(    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyt   generate_cacheY   s    	c         C   s°   | j  d t | ƒ ƒ } x‘ | D]‰ } | j d | ƒ | j d ƒ rŽ |  j j | d  ƒ } | rž x' t | ƒ D] } |  j | | ƒ qn Wqž n |  j | | ƒ | j	 ƒ  q Wd S(   s:   Generate global name cache for modules listed in `modules`s&   Generatig autoimport cache for moduless   Working on <%s>s   .*iþÿÿÿN(
   R5   R6   R7   t   endswithR   t   find_modulet
   submodulesR9   t   update_moduleR:   (   R   t   modulesR   R;   R<   t   modnamet   modt   sub(    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyt   generate_modules_cachek   s    c         C   s   |  j  j ƒ  d S(   s   Clear all entries in global-name cache

        It might be a good idea to use this function before
        regenerating global names.

        N(   R   t   clear(   R   (    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyt   clear_cache{   s    c   	      C   sÚ   t  j d | ƒ } | d k	 r1 | | j ƒ   } n  y t j |  j | ƒ } Wn t j k
 ra d SXd } t	 j
 | d f f ƒ } t	 j |  j | ƒ } | j | ƒ | j ƒ  } | j | ƒ } | j d d | ƒ d } | S(   s4   Guess at what line the new import should be inserteds   ^(def|class)\s+i   t   __rope_testmodule_ropes   
i    N(   t   ret   searchR   t   startR   t   get_string_moduleR   R   t   ModuleSyntaxErrorR   t   NormalImportt   get_module_importst
   add_importt   get_changed_sourcet   indext   count(	   R   t   codet   matchR/   t   testmodnamet
   importinfot   module_importst   offsetR1   (    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyt   find_insertion_line„   s     	c         C   sS   y8 |  j  j | ƒ } |  j | ƒ } |  j | | | ƒ Wn t j k
 rN n Xd S(   s/   Update the cache for global names in `resource`N(   R   t   get_pymodulet   _module_namet
   _add_namesR   RO   (   R   R2   R   R/   RD   (    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyR9   —   s    c         C   sD   y) |  j  j | ƒ } |  j | | | ƒ Wn t j k
 r? n Xd S(   sj   Update the cache for global names in `modname` module

        `modname` is the name of a module.
        N(   R   R+   R_   R   R.   (   R   RD   R   R/   (    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyRB       s
    c         C   s   t  j | ƒ S(   N(   R   RD   (   R   R2   (    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyR^   «   s    c         C   sâ   | d  k r |  j } n  g  } t | t j ƒ r? | j ƒ  } n | j ƒ  } xƒ | j ƒ  D]u \ } } | r€ | j d ƒ r€ qX n  t | t	 j
 t	 j f ƒ r« | j | ƒ n  t | t j ƒ rX | j | ƒ qX qX W| |  j | <d  S(   Nt   _(   R   R   t
   isinstanceR   t   PyDefinedObjectt   _get_structural_attributest   get_attributest   itemsR   R   t   AssignedNamet   DefinedNameR    R    t   BuiltinModuleR   (   R   R/   RD   R   t   globalst
   attributesR&   R0   (    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyR_   ®   s    c         C   s   |  j  j j d |  j ƒ d  S(   NR
   (   R   R   t
   write_dataR   (   R   (    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyR   ¿   s    c         C   s    | j  ƒ  s |  j | ƒ n  d  S(   N(   t	   is_folderR9   (   R   R2   (    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyR   Â   s    c         C   sK   | j  ƒ  sG |  j | ƒ } | |  j k r7 |  j | =n  |  j | ƒ n  d  S(   N(   Rl   R^   R   R9   (   R   R2   t   newresourceRD   (    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyR   Æ   s
    c         C   s>   | j  ƒ  s: |  j | ƒ } | |  j k r: |  j | =q: n  d  S(   N(   Rl   R^   R   (   R   R2   RD   (    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyR   Í   s    N(   t   __name__t
   __module__t   __doc__t   Truet   FalseR   R%   R'   R*   R3   R   R   t   NullTaskHandleR>   RG   RI   R\   R9   RB   R^   R_   R   R   R   R   (    (    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyR	      s(   													c         C   s—   t  |  t j ƒ rG |  j j d ƒ r@ |  j d k r@ t |  g ƒ St ƒ  S|  j d ƒ s] t ƒ  St |  g ƒ } x$ |  j ƒ  D] } | t | ƒ O} qy W| S(   Ns   .pys   __init__.py(	   Ra   R   t   FileR&   R?   R(   t	   has_childt   get_childrenRA   (   RE   R"   t   child(    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyRA   Ô   s    !(   RK   t	   rope.baseR    R   R   R   R   R   R   R   t   rope.refactorR   t   objectR	   RA   (    (    (    s6   lib/python2.7/site-packages/rope/contrib/autoimport.pyt   <module>   s   Æ