ó
‹²,]c           @  s:  d  Z  d d l m Z d d l 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 m Z d d l m Z m Z m Z d d l m Z m Z m Z m Z m Z d	 e f d
 „  ƒ  YZ e d d d „ ƒ Z d d „ Z d „  Z d „  Z  d „  Z! d „  Z" d d „ Z# d S(   s(   
Introspection utilities used by Spyder
iÿÿÿÿ(   t   print_functionN(   t   Token(   t   log_dt(   t
   sourcecodet   encoding(   t   DEBUG_EDITORt   LOG_FILENAMEt   IntrospectionPlugin(   t   default_info_responset   get_parent_untilt   memoizet   find_lexer_for_filenamet   get_keywordst   FallbackPluginc           B  s/   e  Z d  Z d Z d „  Z d „  Z d „  Z RS(   s%   Basic Introspection Plugin for Spydert   fallbackc           s'  | d s d Sg  } | d ‰  | d rt  | d ƒ } | j | d ƒ } x[ | D]S \ } } | j ƒ  } | | d k rR | j ˆ  ƒ rR ˆ  | k rR | j | ƒ qR qR W| t j j k rËy- t | ƒ } | j	 ‡  f d †  | Dƒ ƒ Wqÿ t
 k
 rû qÿ XqËnÉ t t j | d | d ƒ ƒ } g  | D]3 } | j ˆ  ƒ r)t | ƒ t ˆ  ƒ k r)| ^ q)} d ˆ  k r„ˆ  j d ƒ d	 }	 n d
 }	 g  | D]4 }
 |
 |	 t ˆ  ƒ !|
 t ˆ  ƒ j d ƒ d
 ^ q‘} t j d | d ƒ } | r| t | j ƒ  d
 ƒ 7} n  g  t | ƒ D] }
 |
 d f ^ qS(   s~   Return a list of (completion, type) tuples

        Simple completion based on python-like identifiers and whitespace
        t   objNt   contextt   filenamet   source_codec         3  s$   |  ] } | j  ˆ  ƒ r | Vq d  S(   N(   t
   startswith(   t   .0t   k(   R   (    sI   lib/python2.7/site-packages/spyder/utils/introspection/fallback_plugin.pys	   <genexpr>;   s    t   id_regext   .i   i    s   [ "\']([\w\.\\\\/]+)\Zt   linet    (   R   t
   get_tokenst   stripR   t   appendR   t   Literalt   StringR   t   extendt	   Exceptiont   sett   ret   findallt   lent   rfindt   splitt   searcht   _complete_patht   groupst   sorted(   t   selft   infot   itemst   lexert   tokensR   t   tokent   keywordst   itemt   startt   it   match(    (   R   sI   lib/python2.7/site-packages/spyder/utils/introspection/fallback_plugin.pyt   get_completions$   s>    


!
 3>c         C  sÞ  | d s d S| d } | d } | d } | d } d } | d k rL d Sd | k rn | j d ƒ d } n  t | | t | ƒ ƒ } | d k r– d S| d	 } t ƒ  } t j | ƒ d | k rÌ | | f S| j d
 ƒ sê | j d ƒ rÔt j | ƒ }	 t	 | d |	 d | ƒ}
 |
 s1t j |
 ƒ d | k rMt | | | ƒ } | | f St j
 |
 ƒ j d ƒ d } | | k s| d k r‹|
 d f St | d ƒ  } | j ƒ  } Wd QXt j | ƒ d } t | | ƒ } n  | | f S(   sÃ   
        Find the definition for an object within a set of source code

        This is used to find the path of python-like modules
        (e.g. cython and enaml) for a goto definition
        t   is_python_likeNR   t   linesR   R   R   iÿÿÿÿR   s   import s   from t   alt_patht
   stop_tokeni    t   __init__i   t   rb(   t   NoneR&   t   get_definition_with_regexR$   t   python_like_extst   ospt   splitextR   t   dirnamet   python_like_mod_findert   basenamet   opent   readR   t   decode(   R+   R,   R0   R8   R   R   t   line_nrR   t   extsR9   t   source_filet   mod_namet   fidt   code(    (    sI   lib/python2.7/site-packages/spyder/utils/introspection/fallback_plugin.pyt   get_definitionP   sH    




	
	
			

c         C  sƒ   | d rx | d rC t  j j | d ƒ } t  j j | ƒ d } n d } t d | d d | d d d d d	 d
 ƒ } | St ƒ  Sd
 S(   s3   Get a formatted calltip and docstring from Fallbackt	   docstringR   i    s   <module>t   namet   noteR   t   argspect   calltipN(   t   ost   pathRD   RA   t   dictR=   R   (   R+   R,   R   t   resp(    (    sI   lib/python2.7/site-packages/spyder/utils/introspection/fallback_plugin.pyt   get_info€   s    

	(   t   __name__t
   __module__t   __doc__RP   R6   RN   RX   (    (    (    sI   lib/python2.7/site-packages/spyder/utils/introspection/fallback_plugin.pyR      s
   	,	0c   	      C  sÿ  | r( d | k r( | j  d ƒ d } n  t j  d |  ƒ } | d d k rûy  t j | d ƒ \ } } } Wn6 t k
 r¢ | r™ t j | | d ƒ } q£ d } n X| rût j | ƒ } | d | k sxG | d D]8 } | d k ré Pn  t j | | ƒ } | | k rÓ PqÓ qÓ Wn  | rv| | k rvxO t	 ƒ  D]A } d | | f } t j
 t j | | ƒ ƒ r.t j | | ƒ Sq.Wn  x4 t	 ƒ  D]) } d | | f } t j
 | ƒ r€| Sq€Wt j
 | ƒ rÐt j | ƒ rÐ| St j | d ƒ } t j
 | ƒ rø| Sqûn  d S(   sp  
    Locate a module path based on an import line in an python-like file

    import_line is the line of source code containing the import
    alt_path specifies an alternate base path for the module
    stop_token specifies the desired name to stop on

    This is used to a find the path to python-like modules
    (e.g. cython and enaml) for a goto definition.
    R   iÿÿÿÿs   \Wi    t   fromt   importi   i   t   cimportt   ass   %s%ss   __init__.pyN(   R\   R]   (   R]   R^   R_   (   R&   R"   t   impt   find_modulet   ImportErrorR@   t   joinR=   t   realpathR?   t   existst   isdir(	   t   import_lineR9   R:   R/   t   _RU   t   partt   extt   fname(    (    sI   lib/python2.7/site-packages/spyder/utils/introspection/fallback_plugin.pyRC   ’   sB     
c   
   
   C  s?  | s
 d St r t j ƒ  } n  d d d d d d d d d	 d
 g
 } t | |  | | ƒ } | sˆ d d d d g } t | |  | | ƒ } n  | rû t |  j ƒ  ƒ } d } xR | D]G } t | | ƒ }	 | | k sÖ | r­ |	 | k  rô |	 } | } qô q­ q­ Wn  | rt rt t d | ƒ n  | St r7t t d | ƒ n  d Sd S(   sR   
    Find the definition of an object within a source closest to a given line
    s   ^c?import.*\W{0}{1}s   from.*\W{0}\W.*c?import s   from .* c?import.*\W{0}{1}s   class\s*{0}{1}s   c?p?def[^=]*\W{0}{1}s   cdef.*\[.*\].*\W{0}{1}s   enamldef.*\W{0}{1}s   attr.*\W{0}{1}s   event.*\W{0}{1}s   id\s*:.*\W{0}{1}s   .*\Wself.{0}{1}[^=!<>]*=[^=]s   .*\W{0}{1}[^=!<>]*=[^=]s   self.{0}{1}[^=!<>]*=[^=]s   {0}{1}[^=!<>]*=[^=]i    s   regex definition matchs   regex definition failed matchN(	   R=   R   t   timet   get_matchesR$   t
   splitlinest   absR   R   (
   t   sourceR0   t
   start_linet   t0t   patternst   matchest   min_distt   best_indR5   t   dist(    (    sI   lib/python2.7/site-packages/spyder/utils/introspection/fallback_plugin.pyR>   È   sJ    		c   	      C  sÎ   g  |  D] } | j  | d ƒ ^ q }  t j d j |  ƒ ƒ } g  | j ƒ  D] } | j ƒ  d ^ qJ } | d k r t | ƒ } n  g  } x@ t | ƒ D]2 \ } } t j | | ƒ r” | j	 | d ƒ q” q” W| S(   Ns   [^0-9a-zA-Z.[]s   |^t    iÿÿÿÿi   (
   t   formatR"   t   compileRc   Rn   R   R$   t	   enumerateR5   R   (	   Rs   Rp   R0   Rq   t   patternR   R8   Rt   t   index(    (    sI   lib/python2.7/site-packages/spyder/utils/introspection/fallback_plugin.pyRm   ù   s    ")c          C  sO   g  }  x+ t  j D]  } |  j t t  j | ƒ ƒ q Wg  |  D] } d | ^ q; S(   s+   Return a list of all python-like extensionsR   (   R   t   PYTHON_LIKE_LANGUAGESR   t   listt   ALL_LANGUAGES(   RI   t   langRj   (    (    sI   lib/python2.7/site-packages/spyder/utils/introspection/fallback_plugin.pyR?     s    c          C  sT   g  }  x0 t  j j ƒ  D] \ } } |  j t | ƒ ƒ q Wg  |  D] } d | ^ q@ S(   s(   Return a list of all editable extensionsR   (   R   R€   R-   R   R   (   RI   t   languaget
   extensionsRj   (    (    sI   lib/python2.7/site-packages/spyder/utils/introspection/fallback_plugin.pyt   all_editable_exts  s    c         C  sˆ   g  } t  j j |  ƒ }  yb x[ t  j |  ƒ D]J } t  j j |  | ƒ } t  j j | ƒ rh | t  j 7} n  | j | ƒ q+ WWn n X| S(   s>   List directory 'root' appending the path separator to subdirs.(   RT   RU   t
   expandusert   listdirRc   Rf   t   sepR   (   t   roott   resRP   RU   (    (    sI   lib/python2.7/site-packages/spyder/utils/introspection/fallback_plugin.pyt   _listdir  s    c         C  sË   |  s t  d ƒ St j j |  ƒ \ } } | r4 | n d } g  t  | ƒ D] } | j | ƒ rG | ^ qG } t | ƒ d k s t j j |  ƒ r‘ | St j j |  ƒ rÀ g  t  |  ƒ D] } | ^ q° S|  d g S(   s   Perform completion of filesystem path.
    https://stackoverflow.com/questions/5637124/tab-completion-in-pythons-raw-input
    R   i   Rx   (   RŠ   RT   RU   R&   R   R$   Re   Rf   (   RU   RB   t   restt   tmpt   pR‰   (    (    sI   lib/python2.7/site-packages/spyder/utils/introspection/fallback_plugin.pyR(   '  s    
.%($   R[   t
   __future__R    R`   RT   t   os.pathRU   R@   R"   Rl   t   pygments.tokenR   t   spyder.utils.debugR   t   spyder.utilsR   R   t"   spyder.utils.introspection.managerR   R   R   t    spyder.utils.introspection.utilsR   R	   R
   R   R   R   R=   RC   R>   Rm   R?   R„   RŠ   R(   (    (    (    sI   lib/python2.7/site-packages/spyder/utils/introspection/fallback_plugin.pyt   <module>	   s*   (t41				