home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_690 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  2.8 KB  |  119 lines

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