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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. import inspect
  6. import pkgutil
  7. import pydoc
  8. _lookfor_caches = { }
  9. _function_signature_re = re.compile('[a-z_]+\\(.*[,=].*\\)', re.I)
  10.  
  11. def lookfor(what, modules = None, import_modules = True, regenerate = False):
  12.     cache = { }
  13.     for module in modules:
  14.         
  15.         try:
  16.             c = _lookfor_generate_cache(module, import_modules, regenerate)
  17.             cache.update(c)
  18.         continue
  19.         except ImportError:
  20.             continue
  21.         
  22.  
  23.     
  24.     found = []
  25.     whats = str(what).lower().split()
  26.     if not whats:
  27.         return None
  28.     for docstring, kind, index in cache.iteritems():
  29.         ok = True
  30.         doc = docstring.lower()
  31.         for w in whats:
  32.             if w not in doc:
  33.                 ok = False
  34.                 break
  35.                 continue
  36.             None<EXCEPTION MATCH>ImportError if kind in ('module', 'object') else whats
  37.         
  38.         if ok:
  39.             found.append(name)
  40.             continue
  41.     
  42.     kind_relevance = {
  43.         'func': 1000,
  44.         'class': 1000,
  45.         'module': -1000,
  46.         'object': -1000 }
  47.     
  48.     def relevance(name, docstr, kind, index):
  49.         r = 0
  50.         first_doc = '\n'.join(docstr.lower().strip().split('\n')[:3])
  51.         [] += [](_[1])
  52.         [] += [](_[2])
  53.         r += -len(name) * 5
  54.         r += kind_relevance.get(kind, -1000)
  55.         r += -name.count('.') * 10
  56.         r += max(-index / 100, -100)
  57.         return r
  58.  
  59.     
  60.     def relevance_sort(a, b):
  61.         dr = relevance(b, *cache[b]) - relevance(a, *cache[a])
  62.         if dr != 0:
  63.             return dr
  64.         return cmp(a, b)
  65.  
  66.     found.sort(relevance_sort)
  67.     s = "Search results for '%s'" % ' '.join(whats)
  68.     help_text = [
  69.         s,
  70.         '-' * len(s)]
  71.     for name in found:
  72.         (doc, kind, ix) = cache[name]
  73.         doclines = _[1]
  74.         
  75.         try:
  76.             first_doc = doclines[0].strip()
  77.             if _function_signature_re.search(first_doc):
  78.                 first_doc = doclines[1].strip()
  79.         except IndexError:
  80.             []
  81.             []
  82.             []
  83.             first_doc = ''
  84.         except:
  85.             (None, (None, None))
  86.  
  87.         help_text.append('%s\n    %s' % (name, first_doc))
  88.     
  89.     if len(help_text) > 10:
  90.         pager = pydoc.getpager()
  91.         pager('\n'.join(help_text))
  92.     else:
  93.         print '\n'.join(help_text)
  94.  
  95.  
  96. def _lookfor_generate_cache(module, import_modules, regenerate):
  97.     if module is None:
  98.         module = 'numpy'
  99.     
  100.     if isinstance(module, str):
  101.         module = __import__(module)
  102.     
  103.     if id(module) in _lookfor_caches and not regenerate:
  104.         return _lookfor_caches[id(module)]
  105.     cache = { }
  106.     _lookfor_caches[id(module)] = cache
  107.     seen = { }
  108.     index = 0
  109.     stack = [
  110.         (module.__name__, module)]
  111.     while stack:
  112.         (name, item) = stack.pop(0)
  113.         if id(item) in seen:
  114.             continue
  115.         
  116.         seen[id(item)] = True
  117.         index += 1
  118.         kind = 'object'
  119.         if inspect.ismodule(item):
  120.             kind = 'module'
  121.             
  122.             try:
  123.                 _all = item.__all__
  124.             except AttributeError:
  125.                 _all = None
  126.  
  127.             if import_modules and hasattr(item, '__path__'):
  128.                 for m in pkgutil.iter_modules(item.__path__):
  129.                     if _all is not None and m[1] not in _all:
  130.                         continue
  131.                     
  132.                     
  133.                     try:
  134.                         __import__('%s.%s' % (name, m[1]))
  135.                     continue
  136.                     except ImportError:
  137.                         continue
  138.                         continue
  139.                     
  140.  
  141.                 
  142.             
  143.             for n, v in inspect.getmembers(item):
  144.                 if _all is not None and n not in _all:
  145.                     continue
  146.                 
  147.                 stack.append(('%s.%s' % (name, n), v))
  148.             
  149.         elif inspect.isclass(item):
  150.             kind = 'class'
  151.             for n, v in inspect.getmembers(item):
  152.                 stack.append(('%s.%s' % (name, n), v))
  153.             
  154.         elif callable(item):
  155.             kind = 'func'
  156.         
  157.         doc = inspect.getdoc(item)
  158.         if doc is not None:
  159.             cache[name] = (doc, kind, index)
  160.             continue
  161.     return cache
  162.  
  163. import IPython.ipapi as IPython
  164. ip = IPython.ipapi.get()
  165. _lookfor_modules = [
  166.     'numpy',
  167.     'scipy']
  168.  
  169. def lookfor_f(self, arg = ''):
  170.     lookfor(arg, modules = _lookfor_modules)
  171.  
  172.  
  173. def lookfor_modules_f(self, arg = ''):
  174.     global _lookfor_modules
  175.     if not arg:
  176.         print 'Modules included in %lookfor search:', _lookfor_modules
  177.     else:
  178.         _lookfor_modules = arg.split()
  179.  
  180. ip.expose_magic('lookfor', lookfor_f)
  181. ip.expose_magic('lookfor_modules', lookfor_modules_f)
  182.