
V]c           @   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	 m
 Z
 m Z m Z d d l m Z d d l m Z d d	 l m Z 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 m  Z  d d l! m" Z" m# Z# d d l$ m% Z% d d l& m' Z' d d l( m) Z) d e* f d     YZ+ e d e#  e, d   Z- d e j. f d     YZ/ d   Z0 d   Z1 d e* f d     YZ2 e) j3   e% d      Z4 d d e, d!  Z6 d d d"  Z7 d#   Z8 d$   Z9 d S(%   sk  
:mod:`jedi.evaluate.imports` is here to resolve import statements and return
the modules/classes/functions/whatever, which they stand for. However there's
not any actual importing done. This module is about finding modules in the
filesystem. This can be quite tricky sometimes, because Python imports are not
always that simple.

This module uses imp for python up to 3.2 and importlib for python 3.3 on; the
correct implementation is delegated to _compatibility.

This module also supports import autocompletion, which means to complete
statements like ``from datetim`` (cursor at the end would return ``datetime``).
iN(   t   tree(   t   search_ancestor(   t   python_bytes_to_unicode(   t   FileNotFoundErrort   ImplicitNSInfot   force_unicodet   unicode(   t   debug(   t   settings(   t   KnownContentFileIOt   FileIO(   t   get_cached_code_lines(   t   sys_path(   t   helpers(   t   compiled(   t   analysis(   t   unite(   t   evaluator_method_cache(   t
   ImportNamet   SubModuleName(   t
   ContextSett   NO_CONTEXTS(   t   import_module_decorator(   t   iter_module_names(   t   plugin_managert   ModuleCachec           B   s,   e  Z d    Z d   Z d   Z d   Z RS(   c         C   s   i  |  _  i  |  _ d  S(   N(   t   _path_cachet   _name_cache(   t   self(    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt   __init__(   s    	c         C   s    | d  k	 r | |  j | <n  d  S(   N(   t   NoneR   (   R   t   string_namest   context_set(    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt   add,   s    c         C   s   |  j  | S(   N(   R   (   R   R   (    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt   get2   s    c         C   s   |  j  | S(   N(   R   (   R   t   path(    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt   get_from_path5   s    (   t   __name__t
   __module__R   R!   R"   R$   (    (    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyR   '   s   			t   defaultc            s    j    } t | d d  } | j |  } d     j } y | j   } Wn t k
 r_ n0 Xt |  d t |  k r | d  | } n  t | t	 |  | | j
  } | j   }	 |	 s t S d  k	 rht     f d   |	 D  }	  st |	  }	 n  |	 s|  g }
 t | t	 |
  | | j
  } | j   }	  ret d   |	 D  }	 qeqn  rt d   |	 D  }	 n  t j d |	  |	 S(	   Nt   import_namet   import_fromi   ic      	   3   s0   |  ]& } | j   d    d  d t Vq d S(   t   name_contextt   is_gotot   analysis_errorsN(   t   py__getattribute__t   False(   t   .0t   t(   t   contextt   from_import_nameR+   (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pys	   <genexpr>[   s   c         s   s   |  ] } | j  Vq d  S(   N(   t   name(   R/   t   s(    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pys	   <genexpr>m   s    c         s   s   |  ] } | j  Vq d  S(   N(   R3   (   R/   R4   (    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pys	   <genexpr>q   s    s   after import: %s(   t   get_root_contextR   t   get_path_for_nameR   t	   evaluatort   get_from_namest   AttributeErrort   lent   Importert   tuplet   levelt   followR   R   R   t   setR   t   dbg(   R1   t	   tree_nameR+   t   module_contextt   import_nodet   import_pathR7   t
   from_namest   importert   typesR#   (    (   R1   R2   R+   s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt   infer_import;   sD    	
	t   NestedImportModulec           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   s   
    TODO while there's no use case for nested import module right now, we might
        be able to use them for static analysis checks later on.
    c         C   s   | |  _  | |  _ d  S(   N(   t   _modulet   _nested_import(   R   t   modulet   nested_import(    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyR   |   s    	c         C   s   |  j  } d } g  | j d D] } t |  ^ q } t j | |  j   } t j | j | | |  } |  j | _	 t
 j d |  t j t | j d  |  S(   sY   
        Generates an Import statement, that can be used to fake nested imports.
        i    i   s   Generated a nested import: %s(   i    i    (   RK   t   namespace_namesR   R   t   FakeNameR    t   Importt   _sub_moduleRJ   t   parentR   R@   t   str(   R   t   it   zeroR3   t   namest   new(    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt   _get_nested_import_name   s    	&c         C   s   t  |  j |  S(   N(   t   getattrRJ   (   R   R3   (    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt   __getattr__   s    c         C   s   d |  j  j |  j |  j f S(   Ns   <%s: %s of %s>(   t	   __class__R%   RJ   RK   (   R   (    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt   __repr__   s    (   R%   R&   t   __doc__R   RX   RZ   R\   (    (    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyRI   w   s
   			c         C   sI   t  | d  r4 |  d  k	 r4 t j |  d | |  n t j d |  d  S(   NRR   s   import-errors   ImportError without origin: (   t   hasattrR   R   R!   R   t   warning(   R1   R3   t   message(    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt
   _add_error   s    c         C   s   x@ t  | d  D]. } | } t j j |  } | | k r d Sq W| } g  } xg t r | |  k rn | | f St j j |  } | r | j d |  t j j |  } qR d | f SqR Wd S(   s   
    In case the level is outside of the currently known package (something like
    import .....foo), we can still try our best to help the user for
    completions.
    i   i    N(   NN(   t   ranget   osR#   t   dirnameR   t   Truet   basenamet   insert(   t   project_patht	   directoryR=   RT   t   oldt   dt   level_import_pathst   dir_name(    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt   _level_to_base_import_path   s    	
R;   c           B   sP   e  Z d  d  Z e d    Z d   Z d   Z d d d  Z e	 d  Z
 RS(   i    c   
      C   s~  t  j d | | f  | |  _ | |  _ | |  _ d |  _ t |  _ | rq| j	   } | t
 |  k r t |  } | d k r | | d  } n  | t |  } qq| j   } t |  } | d k r t j   } n t j j |  } t |  j j j | |  \ } }	 |	 d k r#t |  _ n t |	  g |  _ | d k rd| rnt | | d d d qnqq| | } n  | |  _ d S(   s  
        An implementation similar to ``__import__``. Use `follow`
        to actually follow the imports.

        *level* specifies whether to use absolute or relative imports. 0 (the
        default) means only perform absolute imports. Positive values for level
        indicate the number of parent directories to search relative to the
        directory of the module calling ``__import__()`` (see PEP 328 for the
        details).

        :param import_path: List of namespaces (strings or Names).
        s   import %s %si   i    R`   s3   Attempted relative import beyond top-level package.N(   R   t   speedt
   _evaluatorR=   RB   R   t   _fixed_sys_pathRe   t   _inference_possiblet   py__package__R:   R<   t
   py__file__t   listRc   t   getcwdR#   Rd   Rn   t   projectt   _pathR.   R   Ra   RD   (
   R   R7   RD   RB   R=   t   baseR#   Ri   t   base_import_patht   base_directory(    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyR      s<    					
c         C   s   t  d   |  j D  S(   s:   Returns the import path as pure strings instead of `Name`.c         s   s0   |  ]& } t  | t j  r$ | j n | Vq d  S(   N(   t
   isinstanceR    t   Namet   value(   R/   R3   (    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pys	   <genexpr>  s   (   R<   RD   (   R   (    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt   _str_import_path   s    c         C   s   |  j  d  k	 r |  j  S|  j j   t j |  j  } |  j j j j	 d k r |  j j
   } | d  k	 r | j t t j j |    q n  | S(   Ni   (   Rq   R   Rp   t   get_sys_pathR   t   check_sys_path_modificationsRB   t   environmentt   version_infot   majorRt   t   appendR   Rc   R#   Rd   (   R   t   sys_path_modt	   file_path(    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt   _sys_path_with_modifications  s    %c         C   s   |  j  s |  j r t St d   |  j  D  } |  j   } d  g } x t |  j   D]x \ } } t j g  | D]& } |  j	 j
 | | d  | |  ^ qo  } | sV d d j |  } t |  j | |  t SqV W| S(   Nc         s   s6   |  ], } t  t | t j  r' | j n |  Vq d  S(   N(   R   R|   R    R}   R~   (   R/   RT   (    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pys	   <genexpr>  s   i   s   No module named t   .(   RD   Rr   R   R<   R   R   t	   enumerateR   t	   from_setsRp   t   import_modulet   joinRa   RB   (   R   t   import_namesR   R    RT   R3   t   parent_module_contextR`   (    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyR>     s     	3c         C   s   g  } | d k rV | d k rV | g  |  j j j   D] } t |  j |  ^ q4 7} n  | d k rq |  j   } n  xW t |  j |  D]C } | d k r t |  j |  } n t | |  } | j	 |  q W| S(   s   
        Get the names of all modules in the search_path. This means file names
        and not names defined in the files.
        N(
   R   Rp   t   compiled_subprocesst   get_builtin_module_namesR   RB   R   R   R   R   (   R   t   search_patht	   in_moduleRV   R3   t   n(    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt   _get_module_names1  s    2c         C   s  |  j  s g  Sg  } |  j r|  j d	 k r xX |  j   D]J } | j } | j d  r8 | t d  } | j t |  j	 |   q8 q8 WxW |  j
   D]F } t j j | d  } t j j |  r | |  j | g  7} q q Wn  |  j   }	 x9 |	 D]1 }
 |
 j d k rq n  | |
 j   j   7} q W| sd d l m } |	 | |	  B} x> | D]3 } x* | j d t  D] } | | j   7} qnWqUWqn2 |  j r| |  j |  j  7} n | |  j   7} | S(
   s   
        :param only_modules: Indicates wheter it's possible to import a
            definition that is not defined in a module.
        t   flaskt   extt   flask_t   flaskextRL   i(   t   convert_contextst   search_global(   R   R   (   Rr   RD   R   R   t   string_namet
   startswithR:   R   R   RB   R   Rc   R#   R   t   isdirR>   t   api_typet   sub_modules_dictt   valuest    jedi.evaluate.gradual.conversionR   t   get_filtersR.   R=   Rq   (   R   R7   t   only_modulesRV   t   modt   modnamet   extnamet   dirR   t   contextsR1   R   t   both_contextst   ct   filter(    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt   completion_namesG  s:    			  	N(   R%   R&   R   t   propertyR   R   R>   R   R   R.   R   (    (    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyR;      s   E		c      
   C   s  | d t  j k rB t |  | |  } | d k r5 t St | g  Sd j |  } | d k r |  j j d | d d | d | d t	  \ } } | d k r=t Sn y | j
 } Wn t k
 r t SX|   }	 xn |	 D]b }
 t |
 t  s |
 g }
 n  |  j j d | d d |
 d | d t  \ } } | d k	 r Pq q Wt St | t  r}d d	 l m } | |  d
 | j d | j } nO | d k rt |  | |  } | d k rt Sn t |  | | d | d | } | d k rt j d | d |  n t j d | |	 |  t | g  S(   sC   
    This method is very similar to importlib's `_gcd_import`.
    i    R   t   stringit	   full_nameR   t   is_global_searchR#   (   t   ImplicitNamespaceContextt   fullnamet   pathsR   t
   is_packages   global search_module %s: %ss    search_module %s in paths %s: %sN(   R   t   auto_import_modulest   _load_builtin_moduleR   R   R   R   R   t   get_module_infoRe   t
   py__path__R9   R|   Ru   R.   R   t   jedi.evaluate.context.namespaceR   R3   R   t   _load_python_moduleR   R@   (   R7   R   R   R   RL   t   module_namet   file_io_or_nst   is_pkgt   methodR   R#   R   (    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyR   w  s`    
	
		c         C   s   y |  j  j | j  SWn t k
 r* n X|  j d | d t d t j d t j  } d d l	 m
 } | |  | d | d | d t |  j | j  d	 | S(
   Nt   file_iot   cachet
   diff_cachet
   cache_pathi(   t   ModuleContextR   t
   code_linesR   (   t   module_cacheR$   R#   t   KeyErrort   parseRe   R   t   fast_parsert   cache_directoryt   jedi.evaluate.contextR   R   t   grammar(   R7   R   R   R   R   t   module_nodeR   (    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyR     s     			c         C   sk   | d  k r |  j   } n  d j |  } | d  k	 s< t  t j |  d | d | } | d  k rg d  S| S(   NR   t   dotted_nameR   (   R   R   R   t   AssertionErrorR   t   load_module(   R7   R   R   R   RL   (    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyR     s    c   	   	   C   s   |  j    } | j } | rg t j j |  } t j |  } | d k } | rW | } q | | f } n t j | |  \ } } t |  | d | d | d | } |  j j	 | t
 | g   | S(   s   
    This should pretty much only be used for get_modules_containing_name. It's
    here to ensure that a random path is still properly loaded into the Jedi
    module structure.
    R   R   R   R   (   R   R#   Rc   Rf   R   t   remove_python_path_suffixt   transform_path_to_dottedR   R   R!   R   (	   R7   R   t
   base_namest
   e_sys_pathR#   R   R   R   RL   (    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt   _load_module_from_path  s"    				c   	      #   s   d       f d   } t     g   xo | D]g } | j d k	 r | j j } |  k r  j |   j | j j   | j   f  q n  | Vq1 Wt j	 s d S    f d   } x: |   D]/ \ } } | | |  } | d k	 r | Vq q Wd S(   s6   
    Search a name in the directories of modules.
    c         s   s;   x4 |  j    D]& } | j d  r |  j |  Vq q Wd  S(   Ns   .py(   Ru   t   endswitht   get_file_io(   t	   folder_iot	   file_name(    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt   check_directory  s    c            s   y |  j    } Wn t k
 r$ d  SXt | d d }  | k rG d  St |  j |  } t   | |  } t | t j	  r d  S| S(   Nt   errorst   replace(
   t   readR   R   R   R	   R#   R   R|   R   t   CompiledObject(   R   R   t   codet   new_file_iot   m(   R7   R3   (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt   check_fs  s    Nc          3   s   x6  D]. \ }  } x   |   D] } | | f Vq  Wq WxC t  j D]8 } t j j |  } |  k rC t |  d  f VqC qC Wd  S(   N(   R   t   additional_dynamic_modulesRc   R#   t   abspathR
   R   (   R   R   R   t   p(   R   t    folders_with_names_to_be_checkedt   used_mod_paths(    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt   get_file_ios_to_check+  s    (
   R?   R   R   R#   R!   R   t   get_parent_folderRs   R   t    dynamic_params_for_other_modules(	   R7   t   modulesR3   R   R   R#   R   R   R   (    (   R   R7   R   R3   R   s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt   get_modules_containing_name  s(    				
(:   R]   Rc   t   parso.pythonR    t
   parso.treeR   t   parsoR   t   jedi._compatibilityR   R   R   R   t   jediR   R   t   jedi.file_ioR	   R
   t   jedi.parser_utilsR   t   jedi.evaluateR   R   R   R   t   jedi.evaluate.utilsR   t   jedi.evaluate.cacheR   t   jedi.evaluate.namesR   R   t   jedi.evaluate.base_contextR   R   t   jedi.evaluate.gradual.typeshedR   t   jedi.evaluate.context.moduleR   t   jedi.pluginsR   t   objectR   R.   RH   t   ModuleRI   Ra   Rn   R;   t   decorateR   R   R   R   R   R   (    (    (    s4   lib/python2.7/site-packages/jedi/evaluate/imports.pyt   <module>   sD   "; 			H	