ó
’›V]c           @   s=  d  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 m Z d d l m Z d Z d e f d „  ƒ  YZ e j d „  ƒ Z e d d ƒ e d „  ƒ ƒ Z d „  Z  d „  Z! d „  Z" d S(   sß  
One of the really important features of |jedi| is to have an option to
understand code like this::

    def foo(bar):
        bar. # completion here
    foo(1)

There's no doubt wheter bar is an ``int`` or not, but if there's also a call
like ``foo('str')``, what would happen? Well, we'll just show both. Because
that's what a human would expect.

It works as follows:

- |Jedi| sees a param
- search for function calls named ``foo``
- execute these calls and check the input.
iÿÿÿÿ(   t   settings(   t   debug(   t   evaluator_function_cache(   t   imports(   t   TreeArguments(   t   create_default_params(   t   is_stdlib_path(   t   to_list(   t   get_parent_scope(   t   ModuleContextt   instance(   t
   ContextSett   NO_CONTEXTS(   t	   recursioni   t   DynamicExecutedParamsc           B   s    e  Z d  Z d „  Z d „  Z RS(   sP   
    Simulates being a parameter while actually just being multiple params.
    c         C   s   | |  _  | |  _ d  S(   N(   t	   evaluatort   _executed_params(   t   selfR   t   executed_params(    (    s4   lib/python2.7/site-packages/jedi/evaluate/dynamic.pyt   __init__*   s    	c         C   sF   t  j |  j |  ƒ + } | r8 t j d „  |  j Dƒ ƒ St SWd  QXd  S(   Nc         s   s   |  ] } | j  ƒ  Vq d  S(   N(   t   infer(   t   .0t   p(    (    s4   lib/python2.7/site-packages/jedi/evaluate/dynamic.pys	   <genexpr>4   s    (   R   t   execution_allowedR   R   t	   from_setsR   R   (   R   t   allowed(    (    s4   lib/python2.7/site-packages/jedi/evaluate/dynamic.pyR   .   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    s4   lib/python2.7/site-packages/jedi/evaluate/dynamic.pyR   %   s   	c   
      C   sg  t  j s t | | ƒ S|  j d 7_ z+| j ƒ  j ƒ  } | d k	 r_ t | ƒ r_ t | | ƒ S| j d k r– t	 | ƒ } | d k r¢ t | | ƒ Sn | j
 j } t j d | d d ƒz| | j ƒ  } t |  | | d | ƒ} | r&t t d „  | Dƒ ƒ Œ  } g  | D] } t |  | ƒ ^ q}	 n t | | ƒ SWd t j d	 d d ƒX|	 SWd |  j d 8_ Xd S(
   s:  
    A dynamic search for param values. If you try to complete a type:

    >>> def func(foo):
    ...     foo
    >>> func(1)
    >>> func("")

    It is not known what the type ``foo`` without analysing the whole code. You
    have to look for all calls to ``func`` to find out what ``foo`` possibly
    is.
    i   t   lambdefs   Dynamic param search in %s.t   colort   MAGENTAt   string_namec         s   s   |  ] } | j  ƒ  d  Vq d S(   i    N(   t   get_executed_params_and_issues(   R   t   function_execution(    (    s4   lib/python2.7/site-packages/jedi/evaluate/dynamic.pys	   <genexpr>e   s   Ns   Dynamic param result finished(   R    t   dynamic_paramsR   t   dynamic_params_deptht   get_root_contextt
   py__file__t   NoneR   t   typet   _get_lambda_namet   namet   valueR   t   dbgt   _search_function_executionst   zipt   listR   (
   R   t   execution_contextt   funcdeft   pathR    t   module_contextt   function_executionst   zipped_paramsR   t   params(    (    s4   lib/python2.7/site-packages/jedi/evaluate/dynamic.pyt   search_params8   s<    		"t   defaultc         c   s  | } | d k rE t  | ƒ } | j d k rE | j j } | } qE n  t } d } x¾ t j |  | g | ƒ D]¤ } t | t ƒ sƒ d Sx~ t	 | | ƒ D]m \ }	 }
 | d 7} | |  j
 t k rÀ d S|  j | |	 ƒ } x+ t |  | | |	 |
 ƒ D] } t } | Vqë Wq“ W| rj d Sqj Wd S(   s(   
    Returns a list of param names.
    R   t   classdefi    Ni   (   R   R(   R*   R+   t   FalseR   t   get_modules_containing_namet
   isinstanceR	   t   _get_possible_nodesR$   t   MAX_PARAM_SEARCHESt   create_contextt   _check_name_for_executiont   True(   R   R3   R1   R    t   compare_nodet   clst   found_executionst   it   for_mod_contextR*   t   trailert   random_contextR"   (    (    s4   lib/python2.7/site-packages/jedi/evaluate/dynamic.pyR-   t   s.    	
c         C   sf   |  j  } | j d k rb t | j ƒ  d  ƒ } | d k rb | j d } | j d k r_ | j Sqb n  d  S(   Nt	   expr_stmtt   =i    R*   (   t   parentR(   t   nextt   yield_operatorsR'   t   childrenR+   (   t   nodet   stmtt   first_operatort   first(    (    s4   lib/python2.7/site-packages/jedi/evaluate/dynamic.pyR)   œ   s    	c         c   s   y |  j  j ƒ  | } Wn t k
 r+ d  SXxL | D]D } | j ƒ  } | j } | j d k r3 | d k r3 | | f Vq3 q3 Wd  S(   NRG   t   ((   t	   tree_nodet   get_used_namest   KeyErrort   get_next_leafRK   R(   (   R3   t   func_string_namet   namesR*   t   bracketRG   (    (    s4   lib/python2.7/site-packages/jedi/evaluate/dynamic.pyR=   ¨   s    	c         #   s¶  d d l  m } ‡  ‡ ‡ ‡ ‡ f d †  } x„ˆ j ˆ  | ƒ D]p‰ ˆ j ‰ | ˆ k ru xR| ƒ  D] } | Vqc Wq> t ˆ j | ƒ r> | j d k r> ˆ j j ƒ  \ } }	 t | ƒ d k rÃ q> n  | d j	 ƒ  }
 g  |
 D] } | j ^ qÚ } | | g k r®ˆ  j
 ƒ  } t | ƒ  ƒ } x t | | d j ƒ D]t \ } ‰ ˆ j | j k  o\ˆ j k  n r0ˆ j | | ƒ } t ˆ | | | ˆ ƒ } x | D] } | Vq’Wq0q0Wq®q> q> Wd  S(   Niÿÿÿÿ(   t   FunctionExecutionContextc          3   s’   ˆ j  d }  |  d k r" d  }  n  t ˆ ˆ  |  ˆ ƒ } ˆ j d k r€ t j ˆ ˆ j ˆ | ƒ } x* | j ƒ  D] } | Vqn Wn ˆ j | ƒ Vd  S(   Ni   t   )R9   (	   RN   R'   R   R(   R
   t   TreeInstancet   parent_contextt   create_init_executionst   get_function_execution(   t   arglistt   argst   created_instancet	   execution(   t   contextR   RG   R+   t
   value_node(    s4   lib/python2.7/site-packages/jedi/evaluate/dynamic.pyt   create_func_excs¸   s    		R1   i   i    (   t   jedi.evaluate.context.functionR[   t   goto_definitionsRT   R<   R^   R(   R!   t   lenR   R%   RL   R=   R    t	   start_post   end_posR?   R@   (   R   Re   RB   R*   RG   R[   Rg   t   func_executionR6   t   _t   valuest   vt   nodesR3   R0   RH   t   iteratorR"   (    (   Re   R   RG   R+   Rf   s4   lib/python2.7/site-packages/jedi/evaluate/dynamic.pyR@   µ   s8    	#%	N(#   R   t   jediR    R   t   jedi.evaluate.cacheR   t   jedi.evaluateR   t   jedi.evaluate.argumentsR   t   jedi.evaluate.paramR   t   jedi.evaluate.helpersR   t   jedi.evaluate.utilsR   t   jedi.parser_utilsR   t   jedi.evaluate.contextR	   R
   t   jedi.evaluate.base_contextR   R   R   R>   t   objectR   t   increase_indentR7   R'   R-   R)   R=   R@   (    (    (    s4   lib/python2.7/site-packages/jedi/evaluate/dynamic.pyt   <module>   s(   <'		