ó
’›V]c           @   s}  d  Z  d d l Z d d l 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 m Z d d
 l m Z d d d g Z e j d e j ƒ e j d e j ƒ g Z e j d ƒ Z d a d „  Z d „  Z d „  Z d „  Z  d „  Z! d „  Z" d „  Z# d „  Z$ d „  Z% e ƒ  d „  ƒ Z& e ƒ  e d „  ƒ ƒ Z' d S(   s¡  
Docstrings are another source of information for functions and classes.
:mod:`jedi.evaluate.dynamic` tries to find all executions of functions, while
the docstring parsing is much easier. There are three different types of
docstrings that |jedi| understands:

- `Sphinx <http://sphinx-doc.org/markup/desc.html#info-field-lists>`_
- `Epydoc <http://epydoc.sourceforge.net/manual-fields.html>`_
- `Numpydoc <https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt>`_

For example, the sphinx annotation ``:type foo: str`` clearly states that the
type of ``foo`` is ``str``.

As an addition to parameter searching, this module also provides return
annotations.
iÿÿÿÿN(   t   dedent(   t   parset   ParserSyntaxError(   t   u(   t   debug(   t   indent_block(   t   evaluator_method_cache(   t   iterator_to_context_sett
   ContextSett   NO_CONTEXTS(   t   LazyKnownContextss   \s*:type\s+%s:\s*([^\n]+)s   \s*:param\s+(\w+)\s+%s:[^\n]*s   \s*@type\s+%s:\s*([^\n]+)s   \s*:rtype:\s*([^\n]+)s   \s*@rtype:\s*([^\n]+)s   :[^`]+:`([^`]+)`c          C   s8   t  t t t f ƒ r t ‚ n  d d l m }  |  a t S(   Niÿÿÿÿ(   t   NumpyDocString(   t
   isinstancet   _numpy_doc_string_cachet   ImportErrort   SyntaxErrort   numpydoc.docscrapeR   (   R   (    (    s7   lib/python2.7/site-packages/jedi/evaluate/docstrings.pyt   _get_numpy_doc_string_cls2   s
    	c      	   C   s³   t  j ƒ  A t  j d ƒ y t ƒ  |  ƒ j d } Wn t k
 rH g  SXWd QXx] | D]U \ } } } | | k rV t j d | ƒ } | r› | j d ƒ } n  t	 t
 | ƒ ƒ SqV Wg  S(   sA   Search `docstr` (in numpydoc format) for type(-s) of `param_str`.t   ignoret
   ParametersNs"   ([^,]+(,[^,]+)*?)(,[ ]*optional)?$i   (   t   warningst   catch_warningst   simplefilterR   t   _parsed_datat	   Exceptiont   ret   matcht   groupt   listt   _expand_typestr(   t   docstrt	   param_strt   paramst   p_namet   p_typet   p_descrt   m(    (    s7   lib/python2.7/site-packages/jedi/evaluate/docstrings.pyt   _search_param_in_numpydocstr;   s    c      	   c   sÈ   t  j ƒ  : t  j d ƒ y t ƒ  |  ƒ } Wn t k
 rA d SXWd QXy" | j d } | | j d 7} Wn t k
 r~ d SXxB | D]: \ } } } | s¤ | } n  x t | ƒ D] } | Vq± Wq† Wd S(   sP   
    Search `docstr` (in numpydoc format) for type(-s) of function returns.
    R   Nt   Returnst   Yields(   R   R   R   R   R   R   R   (   R   t   doct   returnst   r_namet   r_typet   r_descrt   type_(    (    s7   lib/python2.7/site-packages/jedi/evaluate/docstrings.pyt   _search_return_in_numpydocstrN   s     	c         c   s1  t  j d |  ƒ rG x|  j d ƒ D] } | j d ƒ d j ƒ  Vq" Wnæ t  j d |  ƒ rn |  j d ƒ d Vn¿ |  j d ƒ r(t |  d d ƒj d } | j d	 k r-x} | j d
 j D]h } | j d k rê d | j k râ d Vqd Vq¶ | j d k r¶ d | j	 j
 ƒ  k rd Vqd Vq¶ q¶ Wq-n |  Vd S(   s@   
    Attempts to interpret the possible types in `type_str`
    s   \bor\bt   ort   ofi    s   \bof\bt   {t   versions   3.7t   atomi   t   numbert   .t   floatt   intt   stringt   bt   bytest   strN(   R   t   searcht   splitt   stript
   startswithR   t   childrent   typet   valuet   string_prefixt   lower(   t   type_strt   tt   nodet   leaf(    (    s7   lib/python2.7/site-packages/jedi/evaluate/docstrings.pyR   g   s$    c         C   sx   g  t  D]" } t j | t j | ƒ ƒ ^ q } x9 | D]1 } | j |  ƒ } | r6 t | j d ƒ ƒ g Sq6 Wt |  | ƒ S(   sä  
    Search `docstr` for type(-s) of `param_str`.

    >>> _search_param_in_docstr(':type param: int', 'param')
    ['int']
    >>> _search_param_in_docstr('@type param: int', 'param')
    ['int']
    >>> _search_param_in_docstr(
    ...   ':type param: :class:`threading.Thread`', 'param')
    ['threading.Thread']
    >>> bool(_search_param_in_docstr('no document', 'param'))
    False
    >>> _search_param_in_docstr(':param int param: some description', 'param')
    ['int']

    i   (   t   DOCSTRING_PARAM_PATTERNSR   t   compilet   escapeR<   t   _strip_rst_roleR   R%   (   R   R   t   pt   patternst   patternR   (    (    s7   lib/python2.7/site-packages/jedi/evaluate/docstrings.pyt   _search_param_in_docstrˆ   s    ,c         C   s*   t  j |  ƒ } | r" | j d ƒ S|  Sd S(   s   
    Strip off the part looks like a ReST role in `type_str`.

    >>> _strip_rst_role(':class:`ClassName`')  # strip off :class:
    'ClassName'
    >>> _strip_rst_role(':py:obj:`module.Object`')  # works with domain
    'module.Object'
    >>> _strip_rst_role('ClassName')  # do nothing when not ReST role
    'ClassName'

    See also:
    http://sphinx-doc.org/domains.html#cross-referencing-python-objects

    i   N(   t   REST_ROLE_PATTERNR   R   (   RE   R   (    (    s7   lib/python2.7/site-packages/jedi/evaluate/docstrings.pyRL   ¤   s    c         C   sO  t  t d ƒ ƒ } | d  k r" g  Sx( t j d | ƒ D] } d | | } q5 Wt j d | d d ƒ|  j j } y( | j	 | j
 t | ƒ ƒ d t ƒ} Wn t k
 r« g  SXy1 t | j ƒ  ƒ } | j d j d j d	 } Wn t t f k
 r÷ g  SX| j d k rg  Sd d l m } | |  j |  | ƒ }	 |	 j ƒ  }
 t t |
 | ƒ ƒ S(   Nsí   
    def pseudo_docstring_stuff():
        '''
        Create a pseudo function for docstring statements.
        Need this docstring so that if the below part is not valid Python this
        is still a function.
        '''
    {}
    s   ((?:\w+\.)*\w+)\.s
   import %s
s   Parse docstring code %st   colort   BLUEt   error_recoveryiÿÿÿÿiþÿÿÿt   nameR3   t	   atom_expr(   t   FunctionContext(   RU   R3   RV   (   R    R   t   NoneR   t   findallR   t   dbgt	   evaluatort   latest_grammarR   t   formatR   t   FalseR   t   nextt   iter_funcdefsR@   t   AttributeErrort
   IndexErrorRA   t   jedi.evaluate.contextRW   t   get_function_executionR   t   _execute_types_in_stmt(   t   module_contextR8   t   codet   elementt   grammart   modulet   funcdeft   stmtRW   t   function_contextt   func_execution_context(    (    s7   lib/python2.7/site-packages/jedi/evaluate/docstrings.pyt   _evaluate_for_statement_stringº   s4    (	c            s,   ˆ  j  | ƒ } t j ‡  f d †  | Dƒ ƒ S(   sÒ   
    Executing all types or general elements that we find in a statement. This
    doesn't include tuple, list and dict literals, because the stuff they
    contain is executed. (Used as type information).
    c         3   s!   |  ] } t  ˆ  j | ƒ Vq d  S(   N(   t   _execute_array_valuesR[   (   t   .0t   d(   Rf   (    s7   lib/python2.7/site-packages/jedi/evaluate/docstrings.pys	   <genexpr>ö   s   (   t	   eval_nodeR   t	   from_sets(   Rf   Rl   t   definitions(    (   Rf   s7   lib/python2.7/site-packages/jedi/evaluate/docstrings.pyRe   î   s    c            sž   d d l  m } m } t | | ƒ r g  } xL | j ƒ  D]> } t j ‡  f d †  | j ƒ  Dƒ ƒ } | j t	 | ƒ ƒ q8 W| ˆ  | j
 | ƒ h S| j ƒ  Sd S(   s—   
    Tuples indicate that there's not just one return value, but the listed
    ones.  `(str, int)` means that it returns a tuple with both types.
    iÿÿÿÿ(   t   SequenceLiteralContextt   FakeSequencec         3   s   |  ] } t  ˆ  | ƒ Vq d  S(   N(   Rp   (   Rq   t   typ(   R[   (    s7   lib/python2.7/site-packages/jedi/evaluate/docstrings.pys	   <genexpr>  s   N(   t   jedi.evaluate.context.iterableRv   Rw   R   t
   py__iter__R   Rt   t   infert   appendR
   t
   array_typet   execute_annotation(   R[   t   arrayRv   Rw   t   valuest   lazy_contextt   objects(    (   R[   s7   lib/python2.7/site-packages/jedi/evaluate/docstrings.pyRp   û   s    c            sç   d d l  m } d d l m } ‡  ‡ f d †  } |  j ƒ  ‰  ˆ j ƒ  } | j d k r] t S| |  j ƒ  ƒ } t	 |  | ƒ rÍ t	 |  j
 | ƒ rÍ |  j j ƒ  d k rÍ |  j
 j j } | | | j ƒ  ƒ O} n  t j d | d d	 ƒ| S(
   Niÿÿÿÿ(   t   InstanceArguments(   t   FunctionExecutionContextc            s)   t  ‡  f d †  t |  ˆ j j ƒ Dƒ ƒ S(   Nc         3   s+   |  ]! } t  ˆ  | ƒ D] } | Vq q d  S(   N(   Ro   (   Rq   R   RM   (   Rf   (    s7   lib/python2.7/site-packages/jedi/evaluate/docstrings.pys	   <genexpr>  s   (   R   RP   RU   RB   (   t	   docstring(   Rf   t   param(    s7   lib/python2.7/site-packages/jedi/evaluate/docstrings.pyt   eval_docstring  s    t   lambdeft   __init__s#   Found param types for docstring: %sRR   RS   (   t   jedi.evaluate.context.instanceRƒ   Rc   R„   t   get_root_contextt   get_parent_functionRA   R	   t	   py__doc__R   t   var_argsRm   t
   py__name__t   instancet   class_contextR   RZ   (   t   execution_contextR†   Rƒ   R„   R‡   t   funct   typesR‘   (    (   Rf   R†   s7   lib/python2.7/site-packages/jedi/evaluate/docstrings.pyt   infer_param  s    c         c   sO   d „  } x? | |  j  ƒ  ƒ D]+ } x" t |  j ƒ  | ƒ D] } | Vq8 Wq Wd  S(   Nc         s   s]   x: t  D]2 } | j |  ƒ } | r t | j d ƒ ƒ Vq q Wx t |  ƒ D] } | VqJ Wd  S(   Ni   (   t   DOCSTRING_RETURN_PATTERNSR<   RL   R   R.   (   Rg   RM   R   R-   (    (    s7   lib/python2.7/site-packages/jedi/evaluate/docstrings.pyt   search_return_in_docstr,  s    (   R   Ro   R‹   (   Rm   R—   RE   t	   type_eval(    (    s7   lib/python2.7/site-packages/jedi/evaluate/docstrings.pyt   infer_return_types)  s    		((   t   __doc__R   R   t   textwrapR    t   parsoR   R   t   jedi._compatibilityR   t   jediR   t   jedi.evaluate.utilsR   t   jedi.evaluate.cacheR   t   jedi.evaluate.base_contextR   R   R	   t   jedi.evaluate.lazy_contextR
   RI   RJ   t   MR–   RQ   RX   R   R   R%   R.   R   RP   RL   Ro   Re   Rp   R•   R™   (    (    (    s7   lib/python2.7/site-packages/jedi/evaluate/docstrings.pyt   <module>   s:   						!			4		