Ńň
ÎŃAHc           @   s=  d  Z  d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k	 i
 Z d d k Z d d k l Z d d k l Z d d k l Z d d k Z y e   Wn# e j
 o d d k l Z n Xd d k l Z l Z d d	 g Z d d d
     YZ d	 e f d     YZ d S(   sę  Word completion for IPython.

This module is a fork of the rlcompleter module in the Python standard
library.  The original enhancements made to rlcompleter have been sent
upstream and were accepted as of Python 2.3, but we need a lot more
functionality specific to IPython, so this module will continue to live as an
IPython-specific utility.

---------------------------------------------------------------------------
Original rlcompleter documentation:

This requires the latest extension to the readline module (the
completes keywords, built-ins and globals in __main__; when completing
NAME.NAME..., it evaluates (!) the expression up to the last dot and
completes its attributes.

It's very cool to do "import string" type "string.", hit the
completion key (twice), and see the list of names defined by the
string module!

Tip: to use the tab key as the completion key, call

    readline.parse_and_bind("tab: complete")

Notes:

- Exceptions raised by the completer function are *ignored* (and
generally cause the completion to fail).  This is a feature -- since
readline sets the tty device in raw (or cbreak) mode, printing a
traceback wouldn't work well without some complicated hoopla to save,
reset and restore the tty state.

- The evaluation of the NAME.NAME... form may cause arbitrary
application defined code to be executed if an object with a
__getattr__ hook is found.  Since it is the responsibility of the
application (or the user) to enable this feature, I consider this an
acceptable risk.  More complicated expressions (e.g. function calls or
indexing operations) are *not* evaluated.

- GNU readline is also used by the built-in functions input() and
raw_input(), and thus these also benefit/suffer from the completer
features.  Clearly an interactive application can benefit by
specifying its own completer function and using raw_input() for all
its input.

- When the original stdin is not a tty device, GNU readline is never
used, and this module (and the readline module) are silently inactive.

i˙˙˙˙N(   t   Struct(   t   ipapi(   t   generics(   t   Set(   t   debugxt   dir2t	   Completert   IPCompleterc           B   s2   e  Z d d d   Z d   Z d   Z d   Z RS(   c         C   s   xD | | f D]6 } | d j	 o# t |  t i j o t d  q q W| d j o d |  _ n d |  _ | |  _ | d j o h  |  _ n
 | |  _ d S(   s˘  Create a new completer for the command line.

        Completer([namespace,global_namespace]) -> completer instance.

        If unspecified, the default namespace where completions are performed
        is __main__ (technically, __main__.__dict__). Namespaces should be
        given as dictionaries.

        An optional second namespace can be given.  This allows the completer
        to handle cases where both the local and global scopes need to be
        distinguished.

        Completer instances should be used as the completion mechanism of
        readline via the set_completer() call:

        readline.set_completer(Completer(my_namespace).complete)
        s   namespace must be a dictionaryi   i    N(   t   Nonet   typet   typest   DictTypet	   TypeErrort   use_main_nst	   namespacet   global_namespace(   t   selfR   R   t   ns(    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyt   __init__\   s     #		c         C   s   |  i  o t i |  _ n | d j o9 d | j o |  i |  |  _ q` |  i |  |  _ n y |  i | SWn t j
 o d SXd S(   sÂ   Return the next possible completion for 'text'.

        This is called successively with state == 0, 1, 2, ... until it
        returns None.  The completion should begin with 'text'.

        i    t   .N(	   R   t   __main__t   __dict__R   t   attr_matchest   matchest   global_matchest
   IndexErrorR   (   R   t   textt   state(    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyt   complete   s    
c         C   s   g  } | i  } t |  } xu t i t i i   |  i i   |  i i   g D]C } x: | D]2 } | |  | j o | d j o | |  qY qY WqL W| S(   sË   Compute matches when text is a simple name.

        Return a list of all keywords, built-in functions and names currently
        defined in self.namespace or self.global_namespace that match.

        t   __builtins__(	   t   appendt   lent   keywordt   kwlistt   __builtin__R   t   keysR   R   (   R   R   R   t   match_appendt   nt   lstt   word(    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyR      s    		  c         C   s  d d k  } | i d |  } | p g  S| i d d  \ } } y t | |  i  } Wn( y t | |  i  } Wq g  SXn Xt |  } y t i | |  } Wn t	 i
 j
 o n Xt |  } g  }	 | D], }
 |
 |  | j o |	 d | |
 f qŐ qŐ ~	 } | S(   sć  Compute matches when text contains a dot.

        Assuming the text is of the form NAME.NAME....[NAME], and is
        evaluatable in self.namespace or self.global_namespace, it will be
        evaluated and its attributes (as revealed by dir()) are used as
        possible completions.  (For class instances, class members are are
        also considered.)

        WARNING: this can still invoke arbitrary C code, if an object
        with a __getattr__ hook is evaluated.

        i˙˙˙˙Ns   (\S+(\.\w+)*)\.(\w*)$i   i   s   %s.%s(   t   ret   matcht   groupt   evalR   R   R   R   t   complete_objectR   t   TryNextR   (   R   R   R(   t   mt   exprt   attrt   objt   wordsR%   t   _[1]t   wt   res(    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyR   Ź   s(    	@N(   t   __name__t
   __module__R   R   R   R   R   (    (    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyR   [   s   )		c           B   s   e  Z d  Z d d d d d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z d d  Z RS(   s?   Extension of the completer class with IPython-specific featuresi    c         C   sU  t  i |  | |  | i d |  _ | i |  _ t |  _ |  i i   } | i |  i d  } |  i i	 |  |  i i
 |  _
 |  i i |  _ | |  _ | i i |  _ | d	 j o
 h  } n | |  _ t i d  |  _ t i |  _ t i i d d  } | d
 j |  _ t i d j o |  i |  _ n |  i |  _ |  i |  i  |  i! |  i" g |  _# d	 S(   sŁ  IPCompleter() -> completer

        Return a completer object suitable for use by the readline library
        via readline.set_completer().

        Inputs:

        - shell: a pointer to the ipython shell itself.  This is needed
        because this completer knows about magic functions, and those can
        only be accessed via the ipython instance.

        - namespace: an optional dict where completions are performed.

        - global_namespace: secondary optional dict for completions, to
        handle cases (such as IPython embedded inside functions) where
        both Python scopes are visible.

        - The optional omit__names parameter sets the completer to omit the
        'magic' names (__magicname__) for python objects unless the text
        to be completed explicitly starts with one or more underscores.

        - If alias_table is supplied, it should be a dictionary of aliases
        to complete. s   .magic_t    s   ([^\\] )t   TERMt   xtermt   dumbt   emacst   win32N(   s   dumbs   emacs($   R   R   t   namet   magic_prefixt	   ESC_MAGICt   magic_escapet   readlinet   get_completer_delimst   replacet   set_completer_delimst   get_line_buffert
   get_endidxt   omit__namest   rct   readline_merge_completionst   merge_completionsR   t   alias_tableR(   t   compilet   space_name_ret   globt   ost   environt   gett   dumb_terminalt   syst   platformt   _clean_glob_win32t
   clean_globt   _clean_globt   python_matchest   file_matchest   alias_matchest   python_func_kw_matchest   matchers(   R   t   shellR   R   RH   RL   t   delimst   term(    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyR   Ř   s2    		
	c         C   so   g  } | i  } yF x? t t i  D]. } |  i | |  } | p Pn | |  q" WWn t j
 o n X| S(   s9   Return all possible completions for the benefit of emacs.(   R   t   xrangeRT   t   maxintR   t	   NameError(   R   R   t   completionst   comp_appendt   iR5   (    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyt   all_completions  s    	  c         C   s   |  i  d |  S(   Ns   %s*(   RO   (   R   R   (    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyRX   (  s    c         C   s8   g  } |  i  d |  D] } | | i d d  q ~ S(   Ns   %s*s   \t   /(   RO   RD   (   R   R   R3   t   f(    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyRV   +  s    c            s9  d   | i  d  o | d } d } n d }   f d   }  f d    |  i } d } y t i |  d } Wn t j
 oq | i d	  d j o d } | i d	  d } q| i d
  d j o d } | i d
  d } qg  Sn t j
 o d } n X| | |  j o d } | | } } n d } t i i	 |  } | d j o3 g  }	 |  i
 d  D] }
 |	 | | |
  qo~	 S|  i | i d d   } | oC t |  } g  } | D] }
 | | | | |
 |  qÇ~ } n= | o
 | } n, g  } | D] }
 | | | |
  q~ }  |  S(   s  Match filenames, expanding ~USER type strings.

        Most of the seemingly convoluted logic in this completer is an
        attempt to handle filenames with spaces in them.  And yet it's not
        quite perfect, because Python's readline doesn't expose all of the
        GNU readline details needed for this to be done correctly.

        For a filename with a space in it, the printed completions will be
        only the parts after what's already been typed (instead of the
        full completions, as is normally done).  I don't think with the
        current (as of Python 2.3) Python readline it's possible to do
        better.t    t   !i   R8   c            s@   d i  g  } |  D]% } | |   j o d | p | q ~  S(   NR8   s   \(   t   join(   t   sR3   t   ch(   t   protectables(    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyt   protect_filenameL  s    c            s­   t  |   d j o t i i |  d  o{ |  d } | d d j o | d  } n t i |  } | o4 g  } | D] } | | d | qt ~ }    |   S|  Sn |  Sd S(   s7   Recursively expand match lists containing a single dir.i   i    i˙˙˙˙Rh   s   \N(   Rh   s   \(   R   RP   t   patht   isdirt   listdir(   R   t   dt   subdirsR3   t   p(   t   single_dir_expand(    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyRw   P  s    *
)i    i˙˙˙˙t   "t   't   *s   \(   t
   startswitht   lbuft   shlext   splitt
   ValueErrort   countR   RP   Rq   t
   expanduserRO   RW   RD   R   (   R   R   t   text_prefixRp   R|   t   open_quotest   lsplitt   has_protectablest   text0R3   Ri   t   m0t
   len_lsplitt   _[2]R   t   _[3](    (   Ro   Rw   sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyRZ   /  sL    

	30
$c         C   s   d |  i  i   j o |  i  i   i d  o g  St i i |  } |  i i   } | d j o | Sg  } | D]! } | i |  o | | qs qs ~ Sd S(   s   Match internal system aliasesRj   t   sudoR8   N(   R|   t   lstripR{   RP   Rq   R   RL   R#   (   R   R   t   aliasesR3   t   alias(    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyR[     s    0c         C   sń   d | j o yf |  i  |  } | i d  oC |  i o9 |  i d j o d   } n
 d   } t | |  } n Wqí t j
 o g  } qí Xn[ |  i |  } | g  j o> | i t i  o* d |  i	 j o |  i  |  i
 |  } n | S(   s'   Match attributes or global python namesR   i   c         S   s   t  i d  |   d j S(   s   .*\.__.*?__N(   R(   R)   R   (   t   txt(    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyt   <lambda>°  s    c         S   s   t  i d  |   d j S(   s   .*\._.*?N(   R(   R)   R   (   R   (    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyR   ´  s    Rj   (   R   t   endswithRH   t   filterRc   R   R{   RP   t   sepR|   R?   (   R   R   R   t   no__name(    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyRY   Ś  s     	c         C   sĘ   t  i |  p t  i |  pZ t  i |  o) t | d d  p t | d d  } qz t | d  o | i } qz n y6 t  i |  \ } } } } | o | t	 |  SWn t
 j
 o n Xg  S(   s_   Return the list of default arguments of obj if it is callable,
        or empty list otherwise.R   t   __new__t   __call__N(   t   inspectt
   isfunctiont   ismethodt   isclasst   getattrR   t   hasattrR   t
   getargspecR   R   (   R   R1   t   argst   _t   _1t   defaults(    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyt   _default_argumentsĂ  s      c         C   s?  d | j o g  Sy |  i  } Wn6 t j
 o* t i d t i t i B } |  _  n X| i |  i    } | i   t	 |  } d } xZ | D]N } | d j o | d 8} q | d j o  | d 7} | d j o PqŢ q q Wg  Sg  } t i d  i
 } xr t oj yO | i | i    | | d  p | i   Pn | i   d j p Pn Wqt j
 o PqXqWt |  d j o |  i | d  }	 n& |  i d i | d	 d	 d    }	 g  }
 xp |	 D]h } y |  i t | |  i   } Wn
 qĎn Xx3 | D]+ } | i |  o |
 i d
 |  qqWqĎW|
 S(   s9   Match named parameters (kwargs) of the last open functionR   sÓ   
                '.*?' |    # single quoted strings or
                ".*?" |    # double quoted strings or
                \w+   |    # identifier
                \S         # other characters
                i    t   )i   t   (s   \w+$i˙˙˙˙Ns   %s=(   t   _IPCompleter__funcParamsRegext   AttributeErrorR(   RM   t   VERBOSEt   DOTALLt   findallRF   t   reverset   iterR)   t   TrueR   t   nextt   popt   StopIterationR   R   R   Rl   R˘   R+   R   R{   (   R   R   t   regexpt   tokenst
   iterTokenst   openPart   tokent   idst   isIdt   callableMatchest
   argMatchest   callableMatcht	   namedArgst   namedArg(    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyR\   ×  sb     "
  
 
 	%    c      
   C   ss  |  i  } | i   p d  St   } | | _ | | _ | i d  d  d } | | _ | i |  i	  p |  i
 i |  i	 |  } n g  } xá t i |  i
 i |  | |  i
 i |  i   D]Ż } y | |  } g  } | D]! }	 |	 i |  o | |	 qÜ qÜ ~ }
 |
 o |
 Sg  } | D]- }	 |	 i   i | i    o | |	 qq~ SWqź t i j
 o qź Xqź Wd  S(   Ni   i    (   t	   full_lbuft   stripR   R    t   linet   symbolR~   t   commandR{   RA   t   custom_completerst	   s_matchest	   itertoolst   chaint   flat_matchesR|   t   lowerR   R-   (   R   R   Rž   t   eventt   cmdt	   try_magict   cR5   R3   t   rt   withcaseR   (    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyt   dispatch_custom_completer  s4    							 5C	c   
      C   sß  | d j o |  i   |  _ n
 | |  _ |  i p |  i i   p |  i i d  d S|  i } |  i } |  i |  i	    |  _
 yP| i |  o | i | |  } n' | i d  o t i i |  } n | d j oť |  i |  } | d j	 o | |  _ q|  i o7 g  |  _ x] |  i D] } |  i i | |   q"Wn3 x/ |  i D]$ } | |  |  _ |  i o PqPqPWd   } | |  i  |  _ n y! |  i | i | |  }	 |	 SWn t j
 o d SXWn d SXd S(   sô  Return the next possible completion for 'text'.

        This is called successively with state == 0, 1, 2, ... until it
        returns None.  The completion should begin with 'text'.

        :Keywords:
        - line_buffer: string
        If not given, the completer attempts to obtain the current line buffer
        via readline.  This keyword allows clients which are requesting for
        text completions in non-readline contexts to inform the completer of
        the entire text.
        s   	t   ~i    c         S   sB   h  } g  } |  D]* } | | j o | | i  | |  q q ~ S(   N(   t
   setdefault(   t   alistt   setR3   t   e(    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyt   uniqv  s    N(   R   RF   Rź   RS   R˝   RB   t   insert_textRA   R?   RG   R|   R{   RD   RP   Rq   R   RÍ   R   RK   R]   t   extendR   (
   R   R   R   t   line_bufferRA   R?   t
   custom_rest   matcherRÓ   t   ret(    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyR   6  sJ    			
	
 
 
		
N(   R6   R7   t   __doc__R   R   Rg   RX   RV   RZ   R[   RY   R˘   R\   RÍ   R   (    (    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyR   Ő   s   =				h				8	'(    (   RÚ   R"   R   RO   R    RP   R(   R}   RT   t   IPython.rlineimplt	   rlineimplRB   RĂ   t   IPython.ipstructR    t   IPythonR   R   R
   RŃ   Rc   t   setsR   t   IPython.genutilsR   R   t   __all__R   R   (    (    (    sI   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/completer.pyt   <module>1   s,   z