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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import IPython.ipapi as IPython
  5. import glob
  6. import os
  7. import shlex
  8. import sys
  9. import inspect
  10. from time import time
  11. from zipimport import zipimporter
  12. ip = IPython.ipapi.get()
  13.  
  14. try:
  15.     set
  16. except:
  17.     from sets import Set as set
  18.  
  19. TIMEOUT_STORAGE = 3
  20. TIMEOUT_GIVEUP = 20
  21.  
  22. def quick_completer(cmd, completions):
  23.     if isinstance(completions, basestring):
  24.         completions = completions.split()
  25.     
  26.     
  27.     def do_complete(self, event):
  28.         return completions
  29.  
  30.     ip.set_hook('complete_command', do_complete, str_key = cmd)
  31.  
  32.  
  33. def getRootModules():
  34.     modules = []
  35.     if ip.db.has_key('rootmodules'):
  36.         return ip.db['rootmodules']
  37.     t = time()
  38.     store = False
  39.     for path in sys.path:
  40.         modules += moduleList(path)
  41.         if time() - t > TIMEOUT_GIVEUP:
  42.             print 'This is taking too long, we give up.'
  43.             print 
  44.             ip.db['rootmodules'] = []
  45.             return []
  46.     
  47.     modules += sys.builtin_module_names
  48.     modules = list(set(modules))
  49.     modules = list(set(modules))
  50.     if store:
  51.         ip.db['rootmodules'] = modules
  52.     
  53.     return modules
  54.  
  55.  
  56. def moduleList(path):
  57.     if os.path.isdir(path):
  58.         folder_list = os.listdir(path)
  59.     elif path.endswith('.egg'):
  60.         
  61.         try:
  62.             folder_list = [ f for f in zipimporter(path)._files ]
  63.         folder_list = []
  64.  
  65.     else:
  66.         folder_list = []
  67.     folder_list = _[2]
  68.     folder_list = [ os.path.basename(p).split('.')[0] for p in folder_list ]
  69.     return folder_list
  70.  
  71.  
  72. def moduleCompletion(line):
  73.     
  74.     def tryImport(mod, only_modules = False):
  75.         
  76.         def isImportable(module, attr):
  77.             if only_modules:
  78.                 return inspect.ismodule(getattr(module, attr))
  79.             if attr[:2] == '__':
  80.                 pass
  81.             return not (attr[-2:] == '__')
  82.  
  83.         
  84.         try:
  85.             m = __import__(mod)
  86.         except:
  87.             (None,)
  88.             return []
  89.  
  90.         mods = mod.split('.')
  91.         for module in mods[1:]:
  92.             m = getattr(m, module)
  93.         
  94.         completion_list.extend(getattr(m, '__all__', []))
  95.         if hasattr(m, '__file__') and '__init__' in m.__file__:
  96.             completion_list.extend(moduleList(os.path.dirname(m.__file__)))
  97.         
  98.         completion_list = list(set(completion_list))
  99.         if '__init__' in completion_list:
  100.             completion_list.remove('__init__')
  101.         
  102.         return completion_list
  103.  
  104.     words = line.split(' ')
  105.     if len(words) == 3 and words[0] == 'from':
  106.         return [
  107.             'import ']
  108.     if len(words) < 3 and words[0] in ('import', 'from'):
  109.         if len(words) == 1:
  110.             return getRootModules()
  111.         mod = words[1].split('.')
  112.         if len(mod) < 2:
  113.             return getRootModules()
  114.         completion_list = tryImport('.'.join(mod[:-1]), True)
  115.         completion_list = [ '.'.join(mod[:-1] + [
  116.             el]) for el in completion_list ]
  117.         return completion_list
  118.     if len(words) >= 3 and words[0] == 'from':
  119.         mod = words[1]
  120.         return tryImport(mod)
  121.  
  122.  
  123. def vcs_completer(commands, event):
  124.     cmd_param = event.line.split()
  125.     if event.line.endswith(' '):
  126.         cmd_param.append('')
  127.     
  128.     if cmd_param[0] == 'sudo':
  129.         cmd_param = cmd_param[1:]
  130.     
  131.     if len(cmd_param) == 2 or 'help' in cmd_param:
  132.         return commands.split()
  133.     return ip.IP.Completer.file_matches(event.symbol)
  134.  
  135. pkg_cache = None
  136.  
  137. def module_completer(self, event):
  138.     return moduleCompletion(event.line)
  139.  
  140. svn_commands = 'add blame praise annotate ann cat checkout co cleanup commit ci copy\ncp delete del remove rm diff di export help ? h import info list ls\nlock log merge mkdir move mv rename ren propdel pdel pd propedit pedit\npe propget pget pg proplist plist pl propset pset ps resolved revert\nstatus stat st switch sw unlock update\n'
  141.  
  142. def svn_completer(self, event):
  143.     return vcs_completer(svn_commands, event)
  144.  
  145. hg_commands = '\nadd addremove annotate archive backout branch branches bundle cat\nclone commit copy diff export grep heads help identify import incoming\ninit locate log manifest merge outgoing parents paths pull push\nqapplied qclone qcommit qdelete qdiff qfold qguard qheader qimport\nqinit qnew qnext qpop qprev qpush qrefresh qrename qrestore qsave\nqselect qseries qtop qunapplied recover remove rename revert rollback\nroot serve showconfig status strip tag tags tip unbundle update verify\nversion\n'
  146.  
  147. def hg_completer(self, event):
  148.     return vcs_completer(hg_commands, event)
  149.  
  150. __bzr_commands = None
  151.  
  152. def bzr_commands():
  153.     global __bzr_commands
  154.     if __bzr_commands is not None:
  155.         return __bzr_commands
  156.     out = os.popen('bzr help commands')
  157.     __bzr_commands = [ l.split()[0] for l in out ]
  158.     return __bzr_commands
  159.  
  160.  
  161. def bzr_completer(self, event):
  162.     cmd_param = event.line.split()
  163.     if event.line.endswith(' '):
  164.         cmd_param.append('')
  165.     
  166.     if len(cmd_param) > 2:
  167.         cmd = cmd_param[1]
  168.         param = cmd_param[-1]
  169.         output_file = param == '--output='
  170.         if cmd == 'help':
  171.             return bzr_commands()
  172.         if cmd in ('bundle-revisions', 'conflicts', 'deleted', 'nick', 'register-branch', 'serve', 'unbind', 'upgrade', 'version', 'whoami') and not output_file:
  173.             return []
  174.         return ip.IP.Completer.file_matches(event.symbol)
  175.     len(cmd_param) > 2
  176.     return bzr_commands()
  177.  
  178.  
  179. def shlex_split(x):
  180.     endofline = []
  181.     while x != '':
  182.         
  183.         try:
  184.             comps = shlex.split(x)
  185.             if len(endofline) >= 1:
  186.                 comps.append(''.join(endofline))
  187.             
  188.             return comps
  189.         continue
  190.         except ValueError:
  191.             endofline = [
  192.                 x[-1:]] + endofline
  193.             x = x[:-1]
  194.             continue
  195.         
  196.  
  197.         None<EXCEPTION MATCH>ValueError
  198.     return [
  199.         ''.join(endofline)]
  200.  
  201.  
  202. def runlistpy(self, event):
  203.     comps = shlex_split(event.line)
  204.     if not len(comps) > 1 or comps[-1]:
  205.         pass
  206.     relpath = ''.strip('\'"')
  207.     lglob = glob.glob
  208.     isdir = os.path.isdir
  209.     if relpath.startswith('~'):
  210.         relpath = os.path.expanduser(relpath)
  211.     
  212.     dirs = _[1]
  213.     return dirs + pys
  214.  
  215. greedy_cd_completer = False
  216.  
  217. def cd_completer(self, event):
  218.     relpath = event.symbol
  219.     if '-b' in event.line:
  220.         bkms = self.db.get('bookmarks', { })
  221.         return bkms.keys()
  222.     if event.symbol == '-':
  223.         width_dh = str(len(str(len(ip.user_ns['_dh']) + 1)))
  224.         fmt = '-%0' + width_dh + 'd [%s]'
  225.         ents = [ fmt % (i, s) for i, s in enumerate(ip.user_ns['_dh']) ]
  226.         if len(ents) > 1:
  227.             return ents
  228.         return []
  229.     if event.symbol.startswith('--'):
  230.         return [ '--' + os.path.basename(d) for d in ip.user_ns['_dh'] ]
  231.     found = []
  232.     for f in glob.glob(relpath + '*'):
  233.         if os.path.isdir(f):
  234.             continue
  235.         _[3][f.replace('\\', '/') + '/']
  236.     
  237.     if not found:
  238.         if os.path.isdir(relpath):
  239.             return [
  240.                 relpath]
  241.         bks = self.db.get('bookmarks', { }).keys()
  242.         bkmatches = _[4]
  243.         if bkmatches:
  244.             return bkmatches
  245.         raise IPython.ipapi.TryNext
  246.     found
  247.     
  248.     def single_dir_expand(matches):
  249.         if len(matches) == 1 and os.path.isdir(matches[0]):
  250.             d = matches[0]
  251.             if d[-1] in ('/', '\\'):
  252.                 d = d[:-1]
  253.             
  254.             subdirs = _[1]
  255.             if subdirs:
  256.                 matches = [ d + '/' + p for p in subdirs ]
  257.                 return single_dir_expand(matches)
  258.             return matches
  259.         os.path.isdir(matches[0])
  260.         return matches
  261.  
  262.     if greedy_cd_completer:
  263.         return single_dir_expand(found)
  264.     return found
  265.  
  266.  
  267. def apt_get_packages(prefix):
  268.     out = os.popen('apt-cache pkgnames')
  269.     for p in out:
  270.         if p.startswith(prefix):
  271.             yield p.rstrip()
  272.             continue
  273.     
  274.  
  275. apt_commands = 'update upgrade install remove purge source build-dep dist-upgrade\ndselect-upgrade clean autoclean check'
  276.  
  277. def apt_completer(self, event):
  278.     cmd_param = event.line.split()
  279.     if event.line.endswith(' '):
  280.         cmd_param.append('')
  281.     
  282.     if cmd_param[0] == 'sudo':
  283.         cmd_param = cmd_param[1:]
  284.     
  285.     if len(cmd_param) == 2 or 'help' in cmd_param:
  286.         return apt_commands.split()
  287.     return list(apt_get_packages(event.symbol))
  288.  
  289.