##// END OF EJS Templates
import completer: do not so too deeply nested packages before we get deeper
import completer: do not so too deeply nested packages before we get deeper

File last commit:

r410:c6fcfd1b
r410:c6fcfd1b
Show More
ipy_stock_completers.py
123 lines | 4.0 KiB | text/x-python | PythonLexer
/ IPython / Extensions / ipy_stock_completers.py
vivainio
First round of 'complete_command' hook, implements customizable command line ...
r394 """ Tab completion support for a couple of linux package managers
This is also an example of how to write custom completer plugins
or hooks.
Practical use:
[ipython]|1> import ipy_linux_package_managers
[ipython]|2> apt-get u<<< press tab here >>>
update upgrade
[ipython]|2> apt-get up
"""
import IPython.ipapi
vivainio
%cd and %run completers. try 'foo' and '%foo' completer if command line has 'foo'
r402 import glob,os,shlex
vivainio
First round of 'complete_command' hook, implements customizable command line ...
r394
ip = IPython.ipapi.get()
def apt_completers(self, event):
""" This should return a list of strings with possible completions.
Note that all the included strings that don't start with event.symbol
are removed, in order to not confuse readline.
"""
# print event # dbg
# commands are only suggested for the 'command' part of package manager
# invocation
cmd = (event.line + "<placeholder>").rsplit(None,1)[0]
# print cmd
if cmd.endswith('apt-get') or cmd.endswith('yum'):
return ['update', 'upgrade', 'install', 'remove']
# later on, add dpkg -l / whatever to get list of possible
# packages, add switches etc. for the rest of command line
# filling
raise IPython.ipapi.TryNext
# re_key specifies the regexp that triggers the specified completer
ip.set_hook('complete_command', apt_completers, re_key = '.*apt-get')
ip.set_hook('complete_command', apt_completers, re_key = '.*yum')
vivainio
allow str_key in custom completer hooks. import completer.
r395
vivainio
use package cache in import completer, for performance
r409 pkg_cache = None
vivainio
allow str_key in custom completer hooks. import completer.
r395 def module_completer(self,event):
""" Give completions after user has typed 'import' """
vivainio
module completer now uses pkgutil.iter_modules()
r406
vivainio
use package cache in import completer, for performance
r409 global pkg_cache
vivainio
use pkgutil.walk_packages for import completion
r407 import pkgutil,imp,time
vivainio
use package cache in import completer, for performance
r409 #current =
if pkg_cache is None:
print "\n\n[Standby while scanning modules, this can take a while]\n\n"
pkg_cache = list(pkgutil.walk_packages())
vivainio
import completer: do not so too deeply nested packages before we get deeper
r410 already = set()
vivainio
use package cache in import completer, for performance
r409 for ld, name, ispkg in pkg_cache:
vivainio
import completer: do not so too deeply nested packages before we get deeper
r410 if name.count('.') < event.symbol.count('.') + 1:
if name not in already:
already.add(name)
yield name + (ispkg and '.' or '')
vivainio
use pkgutil.walk_packages for import completion
r407 return
vivainio
allow str_key in custom completer hooks. import completer.
r395
vivainio
SVN completer
r397 ip.set_hook('complete_command', module_completer, str_key = 'import')
vivainio
use pkgutil.walk_packages for import completion
r407 ip.set_hook('complete_command', module_completer, str_key = 'from')
vivainio
SVN completer
r397
svn_commands = """\
add blame praise annotate ann cat checkout co cleanup commit ci copy
cp delete del remove rm diff di export help ? h import info list ls
lock log merge mkdir move mv rename ren propdel pdel pd propedit pedit
pe propget pget pg proplist plist pl propset pset ps resolved revert
vivainio
beef up svn completer to show file matches for post-command parameters
r401 status stat st switch sw unlock update
vivainio
SVN completer
r397 """
vivainio
beef up svn completer to show file matches for post-command parameters
r401 def svn_completer(self,event):
if len((event.line + 'placeholder').split()) > 2:
# the rest are probably file names
return ip.IP.Completer.file_matches(event.symbol)
vivainio
SVN completer
r397 return svn_commands.split()
vivainio
%cd and %run completers. try 'foo' and '%foo' completer if command line has 'foo'
r402 ip.set_hook('complete_command', svn_completer, str_key = 'svn')
def runlistpy(self, event):
comps = shlex.split(event.line)
relpath = (len(comps) > 1 and comps[-1] or '')
print "rp",relpath
if relpath.startswith('~'):
relpath = os.path.expanduser(relpath)
dirs = [f.replace('\\','/') + "/" for f in glob.glob(relpath+'*') if os.path.isdir(f)]
pys = [f.replace('\\','/') for f in glob.glob(relpath+'*.py')]
return dirs + pys
ip.set_hook('complete_command', runlistpy, str_key = '%run')
vivainio
use package cache in import completer, for performance
r409 def cd_completer(self, event):
vivainio
%cd and %run completers. try 'foo' and '%foo' completer if command line has 'foo'
r402 relpath = event.symbol
vivainio
%cd completer now shows bookmark completions on %cd -b
r404
if '-b' in event.line:
# return only bookmark completions
bkms = self.db.get('bookmarks',{})
return bkms.keys()
vivainio
cd - now has completions too (show directory history entries)
r405
if event.symbol == '-':
# jump in directory history by number
ents = ['-%d [%s]' % (i,s) for i,s in enumerate(ip.user_ns['_dh'])]
if len(ents) > 1:
return ents
return []
vivainio
%cd completer now shows bookmark completions on %cd -b
r404
vivainio
cd - now has completions too (show directory history entries)
r405
vivainio
%cd and %run completers. try 'foo' and '%foo' completer if command line has 'foo'
r402 if relpath.startswith('~'):
relpath = os.path.expanduser(relpath).replace('\\','/')
found = [f.replace('\\','/')+'/' for f in glob.glob(relpath+'*') if os.path.isdir(f)]
if not found:
return [relpath]
return found
vivainio
use package cache in import completer, for performance
r409 ip.set_hook('complete_command', cd_completer, str_key = '%cd')