ó
&9_[c           @  sË  d  Z  d d l m Z d d l Z d d l Z d d l Z d d l Z d d l Z y d d l m	 Z	 e	 ƒ  Z
 WnA e k
 r² d d l m Z g  e ƒ  D] Z e d ^ q™ Z
 n Xd 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 Z d Z e j d d j  d „  e
 Dƒ ƒ ƒ Z! e j d ƒ Z" d „  Z# d „  Z$ d „  Z% e& d „ Z' d „  Z( d „  Z) d „  Z* d „  Z+ d „  Z, d „  Z- d S(   s\   Implementations for various useful completers.

These are all loaded by default by IPython.
iÿÿÿÿ(   t   print_functionN(   t   all_suffixes(   t   get_suffixesi    (   t   time(   t   zipimporter(   t   expand_usert   compress_user(   t   TryNext(   t	   arg_split(   t   string_types(   t   get_ipythoni   i   sK   (?P<name>[a-zA-Z_][a-zA-Z0-9_]*?)(?P<package>[/\\]__init__)?(?P<suffix>%s)$t   |c         c  s   |  ] } t  j | ƒ Vq d  S(   N(   t   ret   escape(   t   .0t   s(    (    s8   lib/python2.7/site-packages/IPython/core/completerlib.pys	   <genexpr>>   s    s   .*(\.ipy|\.ipynb|\.py[w]?)$c           s:  |  d k r d }  n  t  j j ‰  t  j j |  ƒ r¶ g  } x© t  j |  d t ƒD]` \ } } } | t |  ƒ d ‰ ˆ r¢ | j ‡  ‡ f d †  | Dƒ ƒ g  | (qO | j | ƒ qO Wn/ y t t	 |  ƒ j
 j ƒ  ƒ } Wn g  } n Xg  } x< | D]4 } t j | ƒ } | rò | j | j d ƒ ƒ qò qò Wt t | ƒ ƒ S(   s`   
    Return the list containing the names of the modules available in the given
    folder.
    t    t   .t   followlinksi   c         3  s   |  ] } ˆ  ˆ | ƒ Vq d  S(   N(    (   R   t   f(   t   pjoint   subdir(    s8   lib/python2.7/site-packages/IPython/core/completerlib.pys	   <genexpr>[   s    t   name(   t   ost   patht   joint   isdirt   walkt   Truet   lent   extendt   listR   t   _filest   keyst	   import_ret   matcht   appendt   groupt   set(   R   t   filest   roott   dirst   nondirst   modulesR   t   m(    (   R   R   s8   lib/python2.7/site-packages/IPython/core/completerlib.pyt   module_listG   s*    	% 

c          C  ss  t  ƒ  }  |  d k r" t t j ƒ S|  j j d i  ƒ } t t j ƒ } t ƒ  } t } xï t j	 D]ä } y | | } WnÀ t
 k
 r5t | ƒ } y | j d ƒ Wn t k
 r³ n X| d	 k rÍ | | | <n  t ƒ  | t k r| rt } t d ƒ t d ƒ t j j ƒ  n  t ƒ  | t k r6t d ƒ g  Sn X| j | ƒ q_ W| r]| |  j d <n  t t | ƒ ƒ } | S(
   sº   
    Returns a list containing the names of all the modules available in the
    folders of the pythonpath.

    ip.db['rootmodules_cache'] maps sys.path entries to list of modules.
    t   rootmodules_cachet   __init__R   R   s/   
Caching the list of root modules, please wait!s@   (This will only be done once - type '%rehashx' to reset cache!)
s%   This is taking too long, we give up.
N(   R   R   (   R
   t   NoneR   t   syst   builtin_module_namest   dbt   getR   t   FalseR   t   KeyErrorR-   t   removet
   ValueErrort   TIMEOUT_STORAGER   t   printt   stdoutt   flusht   TIMEOUT_GIVEUPR   R&   (   t   ipR.   t   rootmodulest
   start_timet   storeR   R+   (    (    s8   lib/python2.7/site-packages/IPython/core/completerlib.pyt   get_root_moduleso   s>    		


c         C  s?   | r t  j t |  | ƒ ƒ S| d  d k o9 | d d k Sd  S(   Ni   t   __iþÿÿÿ(   t   inspectt   ismodulet   getattr(   t   modulet   attrt   only_modules(    (    s8   lib/python2.7/site-packages/IPython/core/completerlib.pyt   is_importable›   s    c         C  sD  |  j  d ƒ }  y t |  ƒ } Wn g  SX|  j d ƒ } x! | d D] } t | | ƒ } qD Wt | d ƒ ox d | j k } g  } t | d ƒ sž | sž | rÙ | j g  t | ƒ D] } t | | | ƒ r± | ^ q± ƒ n  | j t | d g  ƒ ƒ | r| j t	 t
 j j | j ƒ ƒ ƒ n  d „  | Dƒ } | j d ƒ t | ƒ S(   NR   i   t   __file__R/   t   __all__c         S  s%   h  |  ] } t  | t ƒ r | ’ q S(    (   t
   isinstanceR	   (   R   t   c(    (    s8   lib/python2.7/site-packages/IPython/core/completerlib.pys	   <setcomp>µ   s   	 (   t   rstript
   __import__t   splitRF   t   hasattrRK   R   t   dirRJ   R-   R   R   t   dirnamet   discardR   (   t   modRI   R,   t   modsRG   t	   m_is_initt   completionsRH   (    (    s8   lib/python2.7/site-packages/IPython/core/completerlib.pyt
   try_import¡   s&    "%c           sJ   t  ˆ  t ƒ r ˆ  j ƒ  ‰  n  ‡  f d †  } t ƒ  j d | d |  ƒd S(   s   Easily create a trivial completer for a command.

    Takes either a list of completions, or all completions in string (that will
    be split on whitespace).

    Example::

        [d:\ipython]|1> import ipy_completers
        [d:\ipython]|2> ipy_completers.quick_completer('foo', ['bar','baz'])
        [d:\ipython]|3> foo b<TAB>
        bar baz
        [d:\ipython]|3> foo ba
    c           s   ˆ  S(   N(    (   t   selft   event(   RY   (    s8   lib/python2.7/site-packages/IPython/core/completerlib.pyt   do_completeÐ   s    t   complete_commandt   str_keyN(   RM   R	   RQ   R
   t   set_hook(   t   cmdRY   R]   (    (   RY   s8   lib/python2.7/site-packages/IPython/core/completerlib.pyt   quick_completer¾   s    c         C  s  |  j  d ƒ } t | ƒ } | d k r> | d d k r> d g S| d k  ré | d d d d h k ré | d k rv t ƒ  S| d j  d	 ƒ } t | ƒ d
 k  r¢ t ƒ  St d	 j | d  ƒ t ƒ } g  | D]  } d	 j | d  | g ƒ ^ qÅ S| d k r| d d k r| d } t | ƒ Sd S(   s¡   
    Returns a list containing the completion possibilities for an import line.

    The line looks like this :
    'import xml.d'
    'from xml.dom import'
    t    i   i    t   froms   import s   %aimportt   importi   R   i   iÿÿÿÿN(   RQ   R   RB   RZ   R   R   (   t   linet   wordst   nwordsRV   t   completion_listt   el(    (    s8   lib/python2.7/site-packages/IPython/core/completerlib.pyt   module_completionÕ   s    	%+
c         C  s   t  | j ƒ S(   s@   Give completions after user has typed 'import ...' or 'from ...'(   Rk   Rf   (   R[   R\   (    (    s8   lib/python2.7/site-packages/IPython/core/completerlib.pyt   module_completerú   s    	c         C  s£  t  | j d t ƒ} t | ƒ d k rP | j j d ƒ rP | d j d ƒ } n d } t j } t j j	 } t
 | ƒ \ } } } t d „  | Dƒ ƒ rÞ g  | | d ƒ D]. } | j d	 d
 ƒ | | ƒ rÎ d
 n d ^ q§ }	 n¢ g  | | d ƒ D]( } | | ƒ rï | j d	 d
 ƒ d
 ^ qï }
 g  | | d ƒ | | d ƒ | | d ƒ | | d ƒ D] } | j d	 d
 ƒ ^ qX} |
 | }	 g  |	 D] } t | | | ƒ ^ q‡S(   sK   Complete files that end in .py or .ipy or .ipynb for the %run command.
    t   stricti   Rc   iÿÿÿÿs   '"R   c         s  s   |  ] } t  j | ƒ Vq d  S(   N(   t   magic_run_reR#   (   R   RN   (    (    s8   lib/python2.7/site-packages/IPython/core/completerlib.pys	   <genexpr>  s    t   *s   \t   /s   *.pys   *.ipys   *.ipynbs   *.pyw(   R   Rf   R5   R   t   endswitht   stript   globR   R   R   R   t   anyt   replaceR   (   R[   R\   t   compst   relpatht   lglobR   t   tilde_expandt	   tilde_valR   t   matchesR)   t   pyst   p(    (    s8   lib/python2.7/site-packages/IPython/core/completerlib.pyt   magic_run_completer  s    %	E?V
c         C  sx  t  ƒ  } | j } | j j d ƒ s3 d | j k r_ |  j j d d ƒ } | rX | j ƒ  Sg  Sn  | j d k rõ t t	 t t	 | j
 d ƒ d ƒ ƒ ƒ } d | d } g  t | j
 d ƒ D] \ } } | | | f ^ q¹ }	 t	 |	 ƒ d k rñ |	 Sg  S| j j d	 ƒ r5g  | j
 d D] }
 d	 t j j |
 ƒ ^ qSt | ƒ \ } } } | j d
 d ƒ } g  } xr g  t j | d ƒ D]. } t j j | ƒ ry| j d
 d ƒ d ^ qyD]( }
 d |
 k rÆt ‚ n  | j |
 ƒ q«W| sUt j j | ƒ rt | | | ƒ g S|  j j d i  ƒ } g  | D] } | j | j ƒ r| ^ q} | rL| St ‚ n  g  | D] } t | | | ƒ ^ q\S(   s:   Completer function for cd, which only returns directories.s   -bs    -b t	   bookmarkst   -t   _dhi   s   -%0s   d [%s]s   --s   \Rp   Ro   Rc   N(   R
   t   symbolRf   Rq   R3   R4   R0   R!   t   strR   t   user_nst	   enumeratet
   startswithR   R   t   basenameR   Ru   Rs   R   R   R$   R   (   R[   R\   R>   Rw   t   bkmst   width_dht   fmtt   iR   t   entst   dRy   Rz   t   foundR   t   bkst	   bkmatchesR}   (    (    s8   lib/python2.7/site-packages/IPython/core/completerlib.pyt   cd_completer-  sB    		!
)6.2	+	c         C  s
   d j  ƒ  S(   s   A completer for %reset magics   -f -s in out array dhist(   RQ   (   R[   R\   (    (    s8   lib/python2.7/site-packages/IPython/core/completerlib.pyt   reset_completerc  s    (.   t   __doc__t
   __future__R    Rs   RD   R   R   R1   t   importlib.machineryR   t	   _suffixest   ImportErrort   impR   R   R   t	   zipimportR   t   IPython.core.completerR   R   t   IPython.core.errorR   t   IPython.utils._process_commonR   t   IPython.utils.py3compatR	   t   IPythonR
   R9   R=   t   compileR   R"   Rn   R-   RB   RJ   R5   RZ   Rb   Rk   Rl   R~   R‘   R’   (    (    (    s8   lib/python2.7/site-packages/IPython/core/completerlib.pyt   <module>   sD   $		(	,			%		%	6