home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / rlcompleter.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  2.8 KB  |  107 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. import __builtin__
  5. import __main__
  6. __all__ = [
  7.     'Completer']
  8.  
  9. class Completer:
  10.     
  11.     def __init__(self, namespace = None):
  12.         if namespace and not isinstance(namespace, dict):
  13.             raise TypeError, 'namespace must be a dictionary'
  14.         if namespace is None:
  15.             self.use_main_ns = 1
  16.         else:
  17.             self.use_main_ns = 0
  18.             self.namespace = namespace
  19.  
  20.     
  21.     def complete(self, text, state):
  22.         if self.use_main_ns:
  23.             self.namespace = __main__.__dict__
  24.         if state == 0:
  25.             if '.' in text:
  26.                 self.matches = self.attr_matches(text)
  27.             else:
  28.                 self.matches = self.global_matches(text)
  29.         
  30.         try:
  31.             return self.matches[state]
  32.         except IndexError:
  33.             return None
  34.  
  35.  
  36.     
  37.     def _callable_postfix(self, val, word):
  38.         if hasattr(val, '__call__'):
  39.             word = word + '('
  40.         return word
  41.  
  42.     
  43.     def global_matches(self, text):
  44.         import keyword
  45.         matches = []
  46.         n = len(text)
  47.         for word in keyword.kwlist:
  48.             if word[:n] == text:
  49.                 matches.append(word)
  50.                 continue
  51.         for nspace in [
  52.             __builtin__.__dict__,
  53.             self.namespace]:
  54.             for word, val in nspace.items():
  55.                 if word[:n] == text and word != '__builtins__':
  56.                     matches.append(self._callable_postfix(val, word))
  57.                     continue
  58.         
  59.         return matches
  60.  
  61.     
  62.     def attr_matches(self, text):
  63.         import re
  64.         m = re.match('(\\w+(\\.\\w+)*)\\.(\\w*)', text)
  65.         if not m:
  66.             return []
  67.         (expr, attr) = None.group(1, 3)
  68.         
  69.         try:
  70.             thisobject = eval(expr, self.namespace)
  71.         except Exception:
  72.             return []
  73.  
  74.         words = dir(thisobject)
  75.         if '__builtins__' in words:
  76.             words.remove('__builtins__')
  77.         if hasattr(thisobject, '__class__'):
  78.             words.append('__class__')
  79.             words.extend(get_class_members(thisobject.__class__))
  80.         matches = []
  81.         n = len(attr)
  82.         for word in words:
  83.             if word[:n] == attr and hasattr(thisobject, word):
  84.                 val = getattr(thisobject, word)
  85.                 word = self._callable_postfix(val, '%s.%s' % (expr, word))
  86.                 matches.append(word)
  87.                 continue
  88.         return matches
  89.  
  90.  
  91.  
  92. def get_class_members(klass):
  93.     ret = dir(klass)
  94.     if hasattr(klass, '__bases__'):
  95.         for base in klass.__bases__:
  96.             ret = ret + get_class_members(base)
  97.         
  98.     return ret
  99.  
  100.  
  101. try:
  102.     import readline
  103. except ImportError:
  104.     pass
  105.  
  106. readline.set_completer(Completer().complete)
  107.