ó
‹²,]c           @   sK  d  Z  d d l Z d d l j Z d d l 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 d d l m Z m Z m Z d d l m Z m Z d d	 l m Z yD y  d d
 l m Z e j ƒ  Z Wn e k
 rd d l Z n XWn e k
 rd Z n Xe j d d ƒ Z d e f d „  ƒ  YZ  d S(   s   
Jedi Introspection Plugin
iÿÿÿÿN(   t   getsignaturefromtext(   t   debug_print(   t   programs(   t   log_last_errort   log_dt(   t   DEBUG_EDITORt   LOG_FILENAMEt   IntrospectionPlugin(   t   default_info_responset   get_parent_until(   t   JEDI_REQVER(   t
   jedi_patcht   jedis   >=0.10.0t
   JediPluginc           B   s\   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z e d „ Z	 e
 d „  ƒ Z d „  Z RS(	   sx   
    Jedi based introspection plugin for jedi

    Experimental Editor's code completion, go-to-definition and help
    R   c         C   sY   t  j d t ƒ s% t d t ƒ ‚ n  t t j _ x! d d g D] } t j | ƒ q> Wd S(   s"   Load the Jedi introspection pluginR   s   Requires Jedi %st   numpyt
   matplotlibN(	   R   t   is_module_installedR
   t   ImportErrort   FalseR   t   settingst   case_insensitive_completiont   preload_module(   t   selft   lib(    (    sE   lib/python2.7/site-packages/spyder/utils/introspection/jedi_plugin.pyt   load_plugin0   s
    c         C   sŽ   |  j  d | ƒ } t r< t t t d t | ƒ d  ƒ ƒ n  | d k	 rp g  | D] } | j | j f ^ qO } n g  } t t | ƒ d  ƒ | S(   s*   Return a list of (completion, type) tuplest   completionss   comp: id   N(	   t   get_jedi_objectR   R   R   t   strt   Nonet   namet   typeR   (   R   t   infoR   t   c(    (    sE   lib/python2.7/site-packages/spyder/utils/introspection/jedi_plugin.pyt   get_completions8   s    $(c         C   sÛ  |  j  d | ƒ } x‡ | D]Y } y | j ƒ  | _ Wn t k
 rE n X| j r | j j ƒ  j d ƒ r | } Pq q Wy | d } Wn t k
 r› t ƒ  SX| j } | d k r¸ t ƒ  S| j
 rÓ t | j
 ƒ } n d } | së | j } n  | j j | d ƒ rFt | j | ƒ } | | j d ƒ } | j | j j d ƒ d } n— | j rÄd | j j ƒ  d k rÄ| j j ƒ  d } | j j d ƒ d } | j | j j d ƒ d } | | j d ƒ } n | d } d } | j } | j d k rd	 | }	 d
 } | } nz | j d k r!d | }	 n^ | j j d | ƒ rf| j j d ƒ d }
 d |
 j ƒ  | f }	 n d | j j ƒ  | f }	 | j d d ƒ } | j d d ƒ } t | j ƒ t d | d | d |	 d | d | ƒ } | S(   sX  
        Find the calltip and docs

        Returns a dict like the following:
           {'note': 'Function of numpy.core.numeric...',
            'argspec': "(shape, dtype=None, order='C')'
            'docstring': 'Return an array of given...'
            'name': 'ones',
            'calltip': 'ones(shape, dtype=None, order='C')'}
        t   goto_definitionst   )i    t   (i   s   (...)s   ()t   modules	   Module %st    t   classs   Class in %s modules   %s(selft   .iþÿÿÿs   Method of %s class in %s modules   %s in %s modules    = t   =R   t   argspect   notet	   docstringt   calltipN(   R   R,   t   doct   AttributeErrort   rstript   endswitht
   IndexErrorR   R   R   t   module_pathR	   t   module_namet
   startswithR    t   findt
   splitlinest   splitR   t	   full_namet
   capitalizet   replaceR   t   dict(   R   R   t   call_deft   cdR   t   mod_nameR-   R*   R,   R+   t
   class_namet   doc_info(    (    sE   lib/python2.7/site-packages/spyder/utils/introspection/jedi_plugin.pyt   get_infoD   sf    "		 "
	
	
c   
      C   s8  | d | d } } d \ } } } |  j d | ƒ } | rR |  j | d ƒ } n  | r³ | d r³ |  j d | ƒ } d }	 | r– |  j | d ƒ }	 n  |	 r½ |	 d r½ |	 } q½ n
 | s½ d S| rå | d rå |  j | ƒ \ } } n | r| d	 } | d
 } n  | | k r| | k rd S| d k r.d S| | f S(   sê   
        Find a definition location using Jedi

        Follows gotos until a definition is found, or it reaches a builtin
        module.  Falls back on token lookup if it is in an enaml file or does
        not find a match
        t   line_numt   filenamet   goto_assignmentsi    t	   goto_nextR"   t
   in_builtinNR3   t   line_nr(   NNN(   R   R   t   get_definition_infot   find_in_builtin(
   R   R   t   lineRD   t   def_infoR3   RH   t   gotost   defnst   new_info(    (    sE   lib/python2.7/site-packages/spyder/utils/introspection/jedi_plugin.pyt   get_definition‘   s0    
c         C   sš  t  s
 d St r t j ƒ  } n  t j } xB | D]: } | j j d k r/ t | d ƒ r/ t j j | ƒ q/ q/ W| r€ | d } n d } y} t
 rÃ t  j j | d | d | d | d | d ƒ} n' t  j j | d | d | d | ƒ } t | | ƒ }	 |	 ƒ  }
 WnS t k
 rX} d }
 t d	 | ƒ t t | ƒ ƒ t rYt t t | ƒ ƒ qYn Xt rrt t | | ƒ n  |
 r’| r’|  j | | t ƒ S|
 Sd S(
   s>   Call a desired function on a Jedi Script and return the resultNt   ImportDeniert   forbidRD   t   source_codeRC   t   columnt   sys_paths   Jedi error (%s)(   R   R   t   timet   syst	   meta_patht	   __class__t   __name__t   hasattrt   removeR   t   JEDI_010t   apit   Scriptt   getattrt	   ExceptionR   R   R   R   R   R   R   (   R   t	   func_nameR   t   use_filenamet   t0t   metast   metaRD   t   scriptt   funct   valt   e(    (    sE   lib/python2.7/site-packages/spyder/utils/introspection/jedi_plugin.pyR   º   s@    	c   	      C   sÛ   yO |  j  } |  j } t |  d ƒ r0 |  j } n	 |  j } |  j } |  j ƒ  } Wn. t k
 r } t r{ t	 t
 d | ƒ n  d	 SXd j | ƒ } t j | | ƒ sª t } n t } t d | d | d | d | d | d | ƒ S(
   s>   Extract definition information from the Jedi definition objectRH   s   Get Defintion: %ss*   class\s+{0}|def\s+{0}|self.{0}\s*=|{0}\s*=R3   t   descriptionR   RG   RF   N(   R3   R   R[   RH   RK   Rk   t   in_builtin_moduleRa   R   R   R   R   t   formatt   ret   matcht   TrueR   R<   (	   t   defnR3   R   RH   Rk   RG   Rj   t   patternRF   (    (    sE   lib/python2.7/site-packages/spyder/utils/introspection/jedi_plugin.pyRI   ã   s&    					c   	      C   sK  | d } | d } | d	 k r$ d
 St j | d ƒ d } | d } | d } | |  j ƒ  k rÅ | j d ƒ s | j d ƒ rÅ |  j | t j | ƒ | ƒ } | rÅ | | d <} d | d <} qÅ n  | |  j ƒ  k r&d } t j	 | j
 | d ƒ | ƒ s&|  j | | | ƒ } | s#d	 } q#q&n  | |  j ƒ  k rAd	 } n  | | f S(   s#   Find a definition in a builtin fileR3   RH   i   Rk   R   s   import s   from s&   from.*\W{0}\W?.*c?import|import.*\W{0}N(   NN(   R   t   ospt   splitextt   python_like_extsR5   t   python_like_mod_findert   dirnamet   all_editable_extsRn   Ro   Rm   t   get_definition_from_file(	   R   R   R3   RH   t   extt   descR   t   pathRr   (    (    sE   lib/python2.7/site-packages/spyder/utils/introspection/jedi_plugin.pyRJ   ü   s0    



			(   RZ   t
   __module__t   __doc__R   R   R!   RB   RP   Rp   R   t   staticmethodRI   RJ   (    (    (    sE   lib/python2.7/site-packages/spyder/utils/introspection/jedi_plugin.pyR   &   s   			M	))(!   R~   Rn   t   os.pathR|   Rs   RW   RV   t   spyder_kernels.utils.dochelpersR    t   spyder.config.baseR   t   spyder.utilsR   t   spyder.utils.debugR   R   t"   spyder.utils.introspection.managerR   R   R   t    spyder.utils.introspection.utilsR   R	   R
   t   spyder.utils.introspectionR   t   applyR   R   R   R   R]   R   (    (    (    sE   lib/python2.7/site-packages/spyder/utils/introspection/jedi_plugin.pyt   <module>	   s*   
