home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Lib / rlcompleter.py < prev    next >
Text File  |  2000-12-21  |  4KB  |  121 lines

  1. """Word completion for GNU readline 2.0.
  2.  
  3. This requires the latest extension to the readline module (the
  4. completes keywords, built-ins and globals in __main__; when completing
  5. NAME.NAME..., it evaluates (!) the expression up to the last dot and
  6. completes its attributes.
  7.  
  8. It's very cool to do "import string" type "string.", hit the
  9. completion key (twice), and see the list of names defined by the
  10. string module!
  11.  
  12. Tip: to use the tab key as the completion key, call
  13.  
  14.     readline.parse_and_bind("tab: complete")
  15.  
  16. Notes:
  17.  
  18. - Exceptions raised by the completer function are *ignored* (and
  19. generally cause the completion to fail).  This is a feature -- since
  20. readline sets the tty device in raw (or cbreak) mode, printing a
  21. traceback wouldn't work well without some complicated hoopla to save,
  22. reset and restore the tty state.
  23.  
  24. - The evaluation of the NAME.NAME... form may cause arbitrary
  25. application defined code to be executed if an object with a
  26. __getattr__ hook is found.  Since it is the responsibility of the
  27. application (or the user) to enable this feature, I consider this an
  28. acceptable risk.  More complicated expressions (e.g. function calls or
  29. indexing operations) are *not* evaluated.
  30.  
  31. - GNU readline is also used by the built-in functions input() and
  32. raw_input(), and thus these also benefit/suffer from the completer
  33. features.  Clearly an interactive application can benefit by
  34. specifying its own completer function and using raw_input() for all
  35. its input.
  36.  
  37. - When the original stdin is not a tty device, GNU readline is never
  38. used, and this module (and the readline module) are silently inactive.
  39.  
  40. """
  41.  
  42. import readline
  43. import __builtin__
  44. import __main__
  45.  
  46. class Completer:
  47.  
  48.     def complete(self, text, state):
  49.         """Return the next possible completion for 'text'.
  50.  
  51.         This is called successively with state == 0, 1, 2, ... until it
  52.         returns None.  The completion should begin with 'text'.
  53.  
  54.         """
  55.         if state == 0:
  56.             if "." in text:
  57.                 self.matches = self.attr_matches(text)
  58.             else:
  59.                 self.matches = self.global_matches(text)
  60.         try:
  61.             return self.matches[state]
  62.         except IndexError:
  63.             return None
  64.  
  65.     def global_matches(self, text):
  66.         """Compute matches when text is a simple name.
  67.  
  68.         Return a list of all keywords, built-in functions and names
  69.         currently defines in __main__ that match.
  70.  
  71.         """
  72.         import keyword
  73.         matches = []
  74.         n = len(text)
  75.         for list in [keyword.kwlist,
  76.                      __builtin__.__dict__.keys(),
  77.                      __main__.__dict__.keys()]:
  78.             for word in list:
  79.                 if word[:n] == text:
  80.                     matches.append(word)
  81.         return matches
  82.  
  83.     def attr_matches(self, text):
  84.         """Compute matches when text contains a dot.
  85.  
  86.         Assuming the text is of the form NAME.NAME....[NAME], and is
  87.         evaluabable in the globals of __main__, it will be evaluated
  88.         and its attributes (as revealed by dir()) are used as possible
  89.         completions.  (For class instances, class members are are also
  90.         considered.)
  91.  
  92.         WARNING: this can still invoke arbitrary C code, if an object
  93.         with a __getattr__ hook is evaluated.
  94.  
  95.         """
  96.         import re
  97.         m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text)
  98.         if not m:
  99.             return
  100.         expr, attr = m.group(1, 3)
  101.         object = eval(expr, __main__.__dict__)
  102.         words = dir(object)
  103.         if hasattr(object,'__class__'):
  104.             words.append('__class__')
  105.             words = words + get_class_members(object.__class__)
  106.         matches = []
  107.         n = len(attr)
  108.         for word in words:
  109.             if word[:n] == attr:
  110.                 matches.append("%s.%s" % (expr, word))
  111.         return matches
  112.  
  113. def get_class_members(klass):
  114.     ret = dir(klass)
  115.     if hasattr(klass,'__bases__'):
  116.         for base in klass.__bases__:
  117.             ret = ret + get_class_members(base)
  118.     return ret
  119.  
  120. readline.set_completer(Completer().complete)
  121.