home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyc (Python 2.6) import IPython.ipapi as IPython import glob import os import shlex import sys import inspect from time import time from zipimport import zipimporter ip = IPython.ipapi.get() try: set except: from sets import Set as set TIMEOUT_STORAGE = 3 TIMEOUT_GIVEUP = 20 def quick_completer(cmd, completions): if isinstance(completions, basestring): completions = completions.split() def do_complete(self, event): return completions ip.set_hook('complete_command', do_complete, str_key = cmd) def getRootModules(): modules = [] if ip.db.has_key('rootmodules'): return ip.db['rootmodules'] t = time() store = False for path in sys.path: modules += moduleList(path) if time() - t > TIMEOUT_GIVEUP: print 'This is taking too long, we give up.' print ip.db['rootmodules'] = [] return [] modules += sys.builtin_module_names modules = list(set(modules)) modules = list(set(modules)) if store: ip.db['rootmodules'] = modules return modules def moduleList(path): if os.path.isdir(path): folder_list = os.listdir(path) elif path.endswith('.egg'): try: folder_list = [ f for f in zipimporter(path)._files ] folder_list = [] else: folder_list = [] folder_list = _[2] folder_list = [ os.path.basename(p).split('.')[0] for p in folder_list ] return folder_list def moduleCompletion(line): def tryImport(mod, only_modules = False): def isImportable(module, attr): if only_modules: return inspect.ismodule(getattr(module, attr)) if attr[:2] == '__': pass return not (attr[-2:] == '__') try: m = __import__(mod) except: (None,) return [] mods = mod.split('.') for module in mods[1:]: m = getattr(m, module) completion_list.extend(getattr(m, '__all__', [])) if hasattr(m, '__file__') and '__init__' in m.__file__: completion_list.extend(moduleList(os.path.dirname(m.__file__))) completion_list = list(set(completion_list)) if '__init__' in completion_list: completion_list.remove('__init__') return completion_list words = line.split(' ') if len(words) == 3 and words[0] == 'from': return [ 'import '] if len(words) < 3 and words[0] in ('import', 'from'): if len(words) == 1: return getRootModules() mod = words[1].split('.') if len(mod) < 2: return getRootModules() completion_list = tryImport('.'.join(mod[:-1]), True) completion_list = [ '.'.join(mod[:-1] + [ el]) for el in completion_list ] return completion_list if len(words) >= 3 and words[0] == 'from': mod = words[1] return tryImport(mod) def vcs_completer(commands, event): cmd_param = event.line.split() if event.line.endswith(' '): cmd_param.append('') if cmd_param[0] == 'sudo': cmd_param = cmd_param[1:] if len(cmd_param) == 2 or 'help' in cmd_param: return commands.split() return ip.IP.Completer.file_matches(event.symbol) pkg_cache = None def module_completer(self, event): return moduleCompletion(event.line) 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' def svn_completer(self, event): return vcs_completer(svn_commands, event) 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' def hg_completer(self, event): return vcs_completer(hg_commands, event) __bzr_commands = None def bzr_commands(): global __bzr_commands if __bzr_commands is not None: return __bzr_commands out = os.popen('bzr help commands') __bzr_commands = [ l.split()[0] for l in out ] return __bzr_commands def bzr_completer(self, event): cmd_param = event.line.split() if event.line.endswith(' '): cmd_param.append('') if len(cmd_param) > 2: cmd = cmd_param[1] param = cmd_param[-1] output_file = param == '--output=' if cmd == 'help': return bzr_commands() if cmd in ('bundle-revisions', 'conflicts', 'deleted', 'nick', 'register-branch', 'serve', 'unbind', 'upgrade', 'version', 'whoami') and not output_file: return [] return ip.IP.Completer.file_matches(event.symbol) len(cmd_param) > 2 return bzr_commands() def shlex_split(x): endofline = [] while x != '': try: comps = shlex.split(x) if len(endofline) >= 1: comps.append(''.join(endofline)) return comps continue except ValueError: endofline = [ x[-1:]] + endofline x = x[:-1] continue None<EXCEPTION MATCH>ValueError return [ ''.join(endofline)] def runlistpy(self, event): comps = shlex_split(event.line) if not len(comps) > 1 or comps[-1]: pass relpath = ''.strip('\'"') lglob = glob.glob isdir = os.path.isdir if relpath.startswith('~'): relpath = os.path.expanduser(relpath) dirs = _[1] return dirs + pys greedy_cd_completer = False def cd_completer(self, event): relpath = event.symbol if '-b' in event.line: bkms = self.db.get('bookmarks', { }) return bkms.keys() if event.symbol == '-': width_dh = str(len(str(len(ip.user_ns['_dh']) + 1))) fmt = '-%0' + width_dh + 'd [%s]' ents = [ fmt % (i, s) for i, s in enumerate(ip.user_ns['_dh']) ] if len(ents) > 1: return ents return [] if event.symbol.startswith('--'): return [ '--' + os.path.basename(d) for d in ip.user_ns['_dh'] ] found = [] for f in glob.glob(relpath + '*'): if os.path.isdir(f): continue _[3][f.replace('\\', '/') + '/'] if not found: if os.path.isdir(relpath): return [ relpath] bks = self.db.get('bookmarks', { }).keys() bkmatches = _[4] if bkmatches: return bkmatches raise IPython.ipapi.TryNext found def single_dir_expand(matches): if len(matches) == 1 and os.path.isdir(matches[0]): d = matches[0] if d[-1] in ('/', '\\'): d = d[:-1] subdirs = _[1] if subdirs: matches = [ d + '/' + p for p in subdirs ] return single_dir_expand(matches) return matches os.path.isdir(matches[0]) return matches if greedy_cd_completer: return single_dir_expand(found) return found def apt_get_packages(prefix): out = os.popen('apt-cache pkgnames') for p in out: if p.startswith(prefix): yield p.rstrip() continue apt_commands = 'update upgrade install remove purge source build-dep dist-upgrade\ndselect-upgrade clean autoclean check' def apt_completer(self, event): cmd_param = event.line.split() if event.line.endswith(' '): cmd_param.append('') if cmd_param[0] == 'sudo': cmd_param = cmd_param[1:] if len(cmd_param) == 2 or 'help' in cmd_param: return apt_commands.split() return list(apt_get_packages(event.symbol))