
V]c           @   sv  d  Z  d d l Z d d l Z d d l Z d d l Z d d l m Z d d l m Z 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 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% 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 l0 m1 Z1 d d l2 m3 Z3 d d l4 m5 Z5 m6 Z6 d d l7 m8 Z8 e j9 d  d e: f d     YZ; d  e; f d!     YZ< e= e= d" e> e? e> e= d#  Z@ d$   ZA e jB e? e? e? d%  ZC d S(&   s  
The API basically only provides one class. You can create a :class:`Script` and
use its methods.

Additionally you can add a debug function with :func:`set_debug_function`.
Alternatively, if you don't need a custom function and are happy with printing
debug messages to stdout, simply call :func:`set_debug_function` without
arguments.

.. warning:: Please, note that Jedi is **not thread safe**.
iN(   t   tree(   t   force_unicodet	   cast_patht   is_py3(   t   get_executable_nodes(   t   debug(   t   settings(   t   cache(   t   KnownContentFileIO(   t   classes(   t   interpreter(   t   helpers(   t
   Completion(   t   InterpreterEnvironment(   t   get_default_projectt   Project(   t	   Evaluator(   t   imports(   t   usages(   t   try_iter_content(   t   get_module_namest   evaluate_call_of_leaf(   t   transform_path_to_dotted(   t   TreeNameDefinitiont	   ParamName(   t   tree_name_to_contexts(   t   ModuleContext(   t
   ContextSet(   t   unpack_tuple_to_dict(   t   convert_namest   convert_contexts(   t   load_proper_stub_modulei  t   Scriptc        	   B   s   e  Z d  Z d d d d d d d d d  Z e j d    Z d   Z d   Z	 d   Z
 e e d  Z e e d  Z e e d	  Z d d
  Z d   Z d   Z RS(   s  
    A Script is the base for completions, goto or whatever you want to do with
    |jedi|.

    You can either use the ``source`` parameter or ``path`` to read a file.
    Usually you're going to want to use both of them (in an editor).

    The script might be analyzed in a different ``sys.path`` than |jedi|:

    - if `sys_path` parameter is not ``None``, it will be used as ``sys.path``
      for the script;

    - if `sys_path` parameter is ``None`` and ``VIRTUAL_ENV`` environment
      variable is defined, ``sys.path`` for the specified environment will be
      guessed (see :func:`jedi.evaluate.sys_path.get_venv_path`) and used for
      the script;

    - otherwise ``sys.path`` will match that of |jedi|.

    :param source: The source code of the current file, separated by newlines.
    :type source: str
    :param line: The line to perform actions on (starting with 1).
    :type line: int
    :param column: The column of the cursor (starting with 0).
    :type column: int
    :param path: The path of the file in the file system, or ``''`` if
        it hasn't been saved yet.
    :type path: str or None
    :param encoding: The encoding of ``source``, if it is not a
        ``unicode`` object (default ``'utf-8'``).
    :type encoding: str
    :param sys_path: ``sys.path`` to use during analysis of the script
    :type sys_path: list
    :param environment: TODO
    :type environment: Environment
    s   utf-8c	         C   s  | |  _  | r! t j j |  n d  |  _ | d  k r] t | d   }	 |	 j   } Wd  QXn  t j   |  _	 | d  k	 r t
 r t t t |   } n  | }
 |
 d  k r t | r t j j |  j  n	 t j    }
 n  | d  k	 r | |
 _ n  t |
 d | d |  j |  _ t j d  |  j j d | d |  j d | d | oO| j d	  d
 t d t j d t j  \ |  _ } t j d  t j | d t |  _ | |  _  | d  k rt! t" |  j  d  n | } d | k  ot" |  j  k n st# d   n  |  j | d } t" |  } | j d  r;| d 8} n  | j d  rW| d 8} n  | d  k ri| n | } d | k o| k n st# d | | | | f   n  | | f |  _$ t% j&   t j'   d  S(   Nt   rbt   environmentt   script_patht   initt   codet   patht   encodingt   use_latest_grammars   .pyiR   t
   diff_cachet
   cache_patht   parsedt   keependsi   i    s)   `line` parameter is not in a valid range.s   
s   
sH   `column` parameter (%d) is not in a valid range (0-%d) for line %d (%r).((   t
   _orig_patht   osR&   t   abspatht   Nonet   opent   readt   parsot   load_grammart   _grammarR   t   listt   mapR   R   t   dirnamet   getcwdt	   _sys_pathR   t
   _evaluatorR   t   speedt   parse_and_get_codet   endswitht   FalseR   t   fast_parsert   cache_directoryt   _module_nodet   split_linest   Truet   _code_linest   _codet   maxt   lent
   ValueErrort   _posR   t   clear_time_cachest
   reset_time(   t   selft   sourcet   linet   columnR&   R'   t   sys_pathR"   t   _projectt   ft   projectt   line_stringt   line_len(    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyt   __init__W   sX    	!-			*%
c      
   C   sZ  d  } t } |  j d  k	 r` t |  j j d t  |  j  \ } } | d  k	 r` | } | } q` n  |  j d  k rx d  } n t t |  j  |  j  } |  j d  k	 r |  j j	 d  r t
 |  j | | |  j  } | d  k	 r | Sn  | d  k r d } n  t |  j |  j | d | d |  j d | } | d d k rV|  j j j | t | g   n  | S(   Nt   add_parent_pathss   .pyit   __main__t   string_namest
   code_linest
   is_packagei    t   builtinst   __builtin__t   typing(   RY   (   R]   R^   R_   (   R0   R?   R&   R   R;   t   get_sys_pathR   R   RF   R>   R   RB   R   RE   t   module_cachet   addR   (   RM   t   namesR\   t   import_namest   is_pt   file_iot   stub_modulet   module(    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyt   _get_module   s<    	!			"c         C   s&   d |  j  j t |  j  |  j j f S(   Ns   <%s: %s %r>(   t	   __class__t   __name__t   reprR-   R;   R"   (   RM   (    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyt   __repr__   s    	c      
   C   sN   t  j d  9 t |  j |  j   |  j |  j |  j  } | j   SWd QXd S(   s  
        Return :class:`classes.Completion` objects. Those objects contain
        information about the completions, more than just names.

        :return: Completion objects, sorted by name and __ comes last.
        :rtype: list of :class:`classes.Completion`
        t   completionsN(	   R   t   increase_indent_cmR   R;   Ri   RE   RJ   t   call_signaturesRn   (   RM   t
   completion(    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyRn      s
    c         K   s'   t  j d   |  j |   SWd QXd S(   s  
        Return the definitions of a the path under the cursor.  goto function!
        This follows complicated paths and returns the end, not the first
        definition. The big difference between :meth:`goto_assignments` and
        :meth:`goto_definitions` is that :meth:`goto_assignments` doesn't
        follow imports and statements. Multiple objects may be returned,
        because Python itself is a dynamic language, which means depending on
        an option you can have two different versions of a function.

        :param only_stubs: Only return stubs for this goto call.
        :param prefer_stubs: Prefer stubs to Python objects for this type
            inference call.
        :rtype: list of :class:`classes.Definition`
        t   goto_definitionsN(   R   Ro   t   _goto_definitions(   RM   t   kwargs(    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyRr      s    c         C   s   |  j  j |  j  } | d  k rI |  j  j |  j  } | d  k rI g  Sn  |  j j |  j   |  } t j	 |  j | |  } t
 | d | d | } g  | D] } t j |  j | j  ^ q } t j t |   S(   Nt
   only_stubst   prefer_stubs(   RB   t   get_name_of_positionRJ   R0   t   get_leaf_for_positionR;   t   create_contextRi   R   t   evaluate_goto_definitionR   R	   t
   Definitiont   namet   sorted_definitionst   set(   RM   Ru   Rv   t   leaft   contextt   contextst   ct   defs(    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyRs      s    	+c         K   s-   t  j d   |  j | | |  SWd QXd S(   s4  
        Return the first definition found, while optionally following imports.
        Multiple objects may be returned, because Python itself is a
        dynamic language, which means depending on an option you can have two
        different versions of a function.

        .. note:: It is deprecated to use follow_imports and follow_builtin_imports as
            positional arguments. Will be a keyword argument in 0.16.0.

        :param follow_imports: The goto call will follow imports.
        :param follow_builtin_imports: If follow_imports is True will decide if
            it follow builtin imports.
        :param only_stubs: Only return stubs for this goto call.
        :param prefer_stubs: Prefer stubs to Python objects for this goto call.
        :rtype: list of :class:`classes.Definition`
        t   goto_assignmentsN(   R   Ro   t   _goto_assignments(   RM   t   follow_importst   follow_builtin_importsRt   (    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyR     s    c   
         s      f d     |  j  j |  j  } | d  k rI |  j d | d |  S|  j j |  j   |  } t |  j j	 | |   } | r   | d    } n  t
 | d | d | } g  t |  D] } t j |  j |  ^ q }	 t j |	  S(   Nc         3   s   x |  D] } | |  r t    | j   |   } t }  rl x) | D] } | j d  k rG t } qG qG Wn  | rz | Vq x | D] } | Vq Wq | Vq Wd  S(   N(   R6   t   gotoR?   t	   start_posR0   RD   (   Rc   t   checkR|   t	   new_namest   found_builtint   new_name(   t   filter_follow_importsR   (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyR     s    Ru   Rv   c         S   s
   |  j    S(   N(   t	   is_import(   R|   (    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyt   <lambda>2  t    (   RB   Rw   RJ   R0   Rr   R;   Ry   Ri   R6   R   R   R~   R	   R{   R   R}   (
   RM   R   R   Ru   Rv   t	   tree_nameR   Rc   t   dR   (    (   R   R   s0   lib/python2.7/site-packages/jedi/api/__init__.pyR     s    	.c            s;   | r t  j d t d d n  t   f d  } | |   S(   s<  
        Return :class:`classes.Definition` objects, which contain all
        names that point to the definition of the name under the cursor. This
        is very useful for refactoring (renaming), or to show all usages of a
        variable.

        .. todo:: Implement additional_module_paths

        :param additional_module_paths: Deprecated, never ever worked.
        :param include_builtins: Default True, checks if a usage is a builtin
            (e.g. ``sys``) and in that case does not return it.
        :rtype: list of :class:`classes.Definition`
        sH   Deprecated since version 0.12.0. This never even worked, just ignore it.t
   stackleveli   c            s     j  j   j  } | d  k r% g  St j   j   |  } g  | D] } t j   j |  ^ qD } |  s g  | D] } | j	   sr | ^ qr } n  t
 j |  S(   N(   RB   Rw   RJ   R0   R   Ri   R	   R{   R;   t   in_builtin_moduleR   R}   (   t   include_builtinsR   Rc   t   nt   definitionsR   (   RM   (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyt   _usagesQ  s    (((   t   warningst   warnt   DeprecationWarningRD   (   RM   t   additional_module_pathsRt   R   (    (   RM   s0   lib/python2.7/site-packages/jedi/api/__init__.pyR   <  s    
c         C   s   t  j |  j |  j  } | d k r( g  S|  j j |  j   | j  } t  j	 |  j | | j |  j
 |  j  } t j d  g  | j   D] } t j |  j | |  ^ q S(   sh  
        Return the function object of the call you're currently in.

        E.g. if the cursor is here::

            abs(# <-- cursor is here

        This would return the ``abs`` function. On the other hand::

            abs()# <-- cursor is here

        This would return an empty list..

        :rtype: list of :class:`classes.CallSignature`
        s   func_call followedN(   R   t   get_call_signature_detailsRB   RJ   R0   R;   Ry   Ri   t   bracket_leaft   cache_call_signaturesRE   R   R<   t   get_signaturesR	   t   CallSignature(   RM   t   call_detailsR   R   t	   signature(    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyRp   _  s    		c         C   s  t  |  j _ |  j g |  j _ |  j   } zxht |  j  D]W} | j |  } | j d k r~ t	 |  j | | j
 d  n	t | t j  r t | j    } | j   r | t d   | j   D  O} n  x | D] } t j | |  q Wn | j d k rG| j |  } xq | j
 d  d d  D] } t | | |  q*Wn@ | j d k rn|  j j | |  } n t | |  } t |  |  j j   q= Wg  |  j j D] }	 |  j |	 j k r|	 ^ q}
 t t |
  d	 d
   SWd  t |  j _ Xd  S(   Nt   funcdeft   classdefi   c         s   s   |  ] } | d  Vq d S(   iN(    (   t   .0R&   (    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pys	   <genexpr>  s    t	   expr_stmtii   R|   t   keyc         S   s   |  j  S(   N(   RO   (   t   x(    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyR     R   (   R   R   (   RD   R;   t   is_analysisRB   t   analysis_modulesRi   R   Ry   t   typeR   t   childrent
   isinstanceR    t   ImportR~   t   get_defined_namest	   is_nestedt	   get_pathsR   t   infer_importt	   eval_nodeR   Rr   R   R   t   reset_recursion_limitationst   analysisR&   t   sortedR?   (   RM   Rh   t   nodeR   Rd   R   t   typest   testlistR   t   at   ana(    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyt	   _analysis  s4    #
1N(    (   Rk   t
   __module__t   __doc__R0   RW   R   t   memoize_methodRi   Rm   Rn   Rr   R?   Rs   R   R   R   Rp   R   (    (    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyR    2   s   $	@*			&#	&t   Interpreterc           B   s&   e  Z d  Z e Z d   Z d   Z RS(   s  
    Jedi API for Python REPLs.

    In addition to completion of simple attribute access, Jedi
    supports code completion based on static code analysis.
    Jedi can complete attributes of object which is not initialized
    yet.

    >>> from os.path import join
    >>> namespace = locals()
    >>> script = Interpreter('join("").up', [namespace])
    >>> print(script.completions()[0].name)
    upper
    c         K   s   y# g  | D] } t  |  ^ q
 } Wn t k
 rB t d   n X| j d d  } | d k rm t   } n t | t  s t d   n  t t |   j	 | d | d t
 t j    | | |  _ |  j |  j _ d S(   s  
        Parse `source` and mixin interpreted Python objects from `namespaces`.

        :type source: str
        :arg  source: Code to parse.
        :type namespaces: list of dict
        :arg  namespaces: a list of namespace dictionaries such as the one
                          returned by :func:`locals`.

        Other optional arguments are same as the ones for :class:`Script`.
        If `line` and `column` are None, they are assumed be at the end of
        `source`.
        s-   namespaces must be a non-empty list of dicts.R"   s?   The environment needs to be an InterpreterEnvironment subclass.RR   N(   t   dictt	   Exceptiont	   TypeErrort   getR0   R   R   t   superR   RW   R   R.   R9   t
   namespacest!   _allow_descriptor_getattr_defaultR;   t   allow_descriptor_getattr(   RM   RN   R   t   kwdsR   R"   (    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyRW     s    #	c         C   s:   t  j |  j |  j |  j d t |  j |  j  d |  j S(   NRf   R[   (	   R
   t   MixedModuleContextR;   RB   R   R   R&   RF   RE   (   RM   (    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyRi     s    	(   Rk   R   R   RD   R   RW   Ri   (    (    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyR     s   	s   utf-8c            s      f d   }  f d   } t  |  d d d d d | d | d	 | }	 |	 j    g  t |	 j |  D]! }
 t j |	 j | |
   ^ qj } t t | |  d
 d   S(   s  
    Returns a list of `Definition` objects, containing name parts.
    This means you can call ``Definition.goto_assignments()`` and get the
    reference of a name.
    The parameters are the same as in :py:class:`Script`, except or the
    following ones:

    :param all_scopes: If True lists the names of all scopes instead of only
        the module namespace.
    :param definitions: If True lists the names that have been defined by a
        class, function or a statement (``a = b`` returns ``a``).
    :param references: If True lists all the names that are not listed by
        ``definitions=True``. E.g. ``a = b`` returns ``b``.
    c            s)   |  j  j j   }   r | p(  o( | S(   N(   t   _nameR   t   is_definition(   t   _deft   is_def(   R   t
   references(    s0   lib/python2.7/site-packages/jedi/api/__init__.pyt   def_ref_filter  s    c            s7   |  j  j d k r t } n t } |   j |   |   S(   Nt   param(   t   parentR   R   R   Ry   (   R|   t   cls(   t   module_context(    s0   lib/python2.7/site-packages/jedi/api/__init__.pyt   create_name  s    	RO   i   RP   i    R&   R'   R"   R   c         S   s   |  j  |  j f S(   N(   RO   RP   (   R   (    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyR   	  R   (	   R    Ri   R   RB   R	   R{   R;   R   t   filter(   RN   R&   R'   t
   all_scopesR   R   R"   R   R   t   scriptR|   R   (    (   R   R   R   s0   lib/python2.7/site-packages/jedi/api/__init__.pyRc     s    *7c          G   s>   x7 |  D]/ } d | } t  | d t |  d  j   q Wd S(   s   
    Preloading modules tells Jedi to load a module now, instead of lazy parsing
    of modules. Usful for IDEs, to control which modules to load on startup.

    :param modules: different module names, list of string.
    s   import %s as x; x.i   N(   R    RH   R0   Rn   (   t   modulest   mt   s(    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyt   preload_module  s    
c         C   s(   |  t  _ | t  _ | t  _ | t  _ d S(   s   
    Define a callback debug function to get all the debug messages.

    If you don't specify any arguments, debug messages will be printed to stdout.

    :param func_cb: The callback function for debug messages, with n params.
    N(   R   t   debug_functiont   enable_warningt   enable_noticet   enable_speed(   t   func_cbR   t   noticesR<   (    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyt   set_debug_function  s    				(D   R   R.   t   sysR   R3   t   parso.pythonR    t   jedi._compatibilityR   R   R   t   jedi.parser_utilsR   t   jediR   R   R   t   jedi.file_ioR   t   jedi.apiR	   R
   R   t   jedi.api.completionR   t   jedi.api.environmentR   t   jedi.api.projectR   R   t   jedi.evaluateR   R   R   t   jedi.evaluate.argumentsR   t   jedi.evaluate.helpersR   R   t   jedi.evaluate.sys_pathR   t   jedi.evaluate.namesR   R   t   jedi.evaluate.syntax_treeR   t   jedi.evaluate.contextR   t   jedi.evaluate.base_contextR   t   jedi.evaluate.context.iterableR   t    jedi.evaluate.gradual.conversionR   R   t   jedi.evaluate.gradual.utilsR   t   setrecursionlimitt   objectR    R   R0   R?   RD   Rc   R   t   print_to_stdoutR   (    (    (    s0   lib/python2.7/site-packages/jedi/api/__init__.pyt   <module>   sN    w:)		