##// END OF EJS Templates
- Commit local changelog info, remove duplicate entry.
- Commit local changelog info, remove duplicate entry.

File last commit:

r497:5c476723
r502:d3ea7cd7
Show More
ipy_stock_completers.py
218 lines | 6.9 KiB | text/x-python | PythonLexer
/ IPython / Extensions / ipy_stock_completers.py
vivainio
reindent ipy_stock_completers
r497 """ Tab completion support for a couple of linux package managers
vivainio
First round of 'complete_command' hook, implements customizable command line ...
r394
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
import completer: only crippled 'local' version for py 2.4
r411 import glob,os,shlex,sys
vivainio
First round of 'complete_command' hook, implements customizable command line ...
r394
ip = IPython.ipapi.get()
vivainio
vcs_completer() makes version control system completers simpler
r458 def vcs_completer(commands, event):
vivainio
reindent ipy_stock_completers
r497 """ utility to make writing typical version control app completers easier
vivainio
vcs_completer() makes version control system completers simpler
r458 VCS command line apps typically have the format:
vivainio
reindent ipy_stock_completers
r497
vivainio
vcs_completer() makes version control system completers simpler
r458 [sudo ]PROGNAME [help] [command] file file...
vivainio
reindent ipy_stock_completers
r497
vivainio
vcs_completer() makes version control system completers simpler
r458 """
vivainio
reindent ipy_stock_completers
r497
vivainio
vcs_completer() makes version control system completers simpler
r458 cmd_param = event.line.split()
if event.line.endswith(' '):
cmd_param.append('')
if cmd_param[0] == 'sudo':
cmd_param = cmd_param[1:]
vivainio
reindent ipy_stock_completers
r497
vivainio
vcs_completer() makes version control system completers simpler
r458 if len(cmd_param) == 2 or 'help' in cmd_param:
return commands.split()
vivainio
reindent ipy_stock_completers
r497
vivainio
vcs_completer() makes version control system completers simpler
r458 return ip.IP.Completer.file_matches(event.symbol)
vivainio
reindent ipy_stock_completers
r497
vivainio
vcs_completer() makes version control system completers simpler
r458
vivainio
First round of 'complete_command' hook, implements customizable command line ...
r394 def apt_completers(self, event):
""" This should return a list of strings with possible completions.
vivainio
reindent ipy_stock_completers
r497
vivainio
First round of 'complete_command' hook, implements customizable command line ...
r394 Note that all the included strings that don't start with event.symbol
are removed, in order to not confuse readline.
vivainio
reindent ipy_stock_completers
r497
vivainio
First round of 'complete_command' hook, implements customizable command line ...
r394 """
# print event # dbg
vivainio
reindent ipy_stock_completers
r497
vivainio
First round of 'complete_command' hook, implements customizable command line ...
r394 # commands are only suggested for the 'command' part of package manager
# invocation
vivainio
reindent ipy_stock_completers
r497
vivainio
First round of 'complete_command' hook, implements customizable command line ...
r394 cmd = (event.line + "<placeholder>").rsplit(None,1)[0]
# print cmd
if cmd.endswith('apt-get') or cmd.endswith('yum'):
return ['update', 'upgrade', 'install', 'remove']
vivainio
reindent ipy_stock_completers
r497
# later on, add dpkg -l / whatever to get list of possible
vivainio
First round of 'complete_command' hook, implements customizable command line ...
r394 # packages, add switches etc. for the rest of command line
# filling
vivainio
reindent ipy_stock_completers
r497
raise IPython.ipapi.TryNext
vivainio
First round of 'complete_command' hook, implements customizable command line ...
r394
# 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
reindent ipy_stock_completers
r497 def module_completer(self,event):
vivainio
allow str_key in custom completer hooks. import completer.
r395 """ Give completions after user has typed 'import' """
vivainio
reindent ipy_stock_completers
r497
vivainio
import completer: only crippled 'local' version for py 2.4
r411 # only a local version for py 2.4, pkgutil has no walk_packages() there
fptest
- Fix tab-completion so that quotes are not closed unless the completion is...
r412 if sys.version_info < (2,5):
for el in [f[:-3] for f in glob.glob("*.py")]:
yield el
vivainio
import completer: only crippled 'local' version for py 2.4
r411 return
fptest
- Fix tab-completion so that quotes are not closed unless the completion is...
r412
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
reindent ipy_stock_completers
r497 #current =
vivainio
use package cache in import completer, for performance
r409 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
reindent ipy_stock_completers
r497
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):
vivainio
vcs_completer() makes version control system completers simpler
r458 return vcs_completer(svn_commands, event)
vivainio
SVN completer
r397
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')
vivainio
stock completer for hg (mercurial)
r445 hg_commands = """
add addremove annotate archive backout branch branches bundle cat
clone commit copy diff export grep heads help identify import incoming
init locate log manifest merge outgoing parents paths pull push
qapplied qclone qcommit qdelete qdiff qfold qguard qheader qimport
qinit qnew qnext qpop qprev qpush qrefresh qrename qrestore qsave
qselect qseries qtop qunapplied recover remove rename revert rollback
root serve showconfig status strip tag tags tip unbundle update verify
version
"""
def hg_completer(self,event):
""" Completer for mercurial commands """
vivainio
reindent ipy_stock_completers
r497
vivainio
vcs_completer() makes version control system completers simpler
r458 return vcs_completer(hg_commands, event)
vivainio
stock completer for hg (mercurial)
r445
ip.set_hook('complete_command', hg_completer, str_key = 'hg')
vivainio
bzr completer submitted by Stefan van der Walt
r457 bzr_commands = """
add annotate bind branch break-lock bundle-revisions cat check
checkout commit conflicts deleted diff export gannotate gbranch
gcommit gdiff help ignore ignored info init init-repository inventory
log merge missing mkdir mv nick pull push reconcile register-branch
remerge remove renames resolve revert revno root serve sign-my-commits
status testament unbind uncommit unknowns update upgrade version
version-info visualise whoami
"""
def bzr_completer(self,event):
""" Completer for bazaar commands """
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.split()
elif cmd in ['bundle-revisions','conflicts',
'deleted','nick','register-branch',
'serve','unbind','upgrade','version',
'whoami'] and not output_file:
return []
else:
# the rest are probably file names
return ip.IP.Completer.file_matches(event.symbol)
return bzr_commands.split()
ip.set_hook('complete_command', bzr_completer, str_key = 'bzr')
vivainio
%cd and %run completers. try 'foo' and '%foo' completer if command line has 'foo'
r402 def runlistpy(self, event):
comps = shlex.split(event.line)
relpath = (len(comps) > 1 and comps[-1] or '')
vivainio
reindent ipy_stock_completers
r497
fptest
- Fix tab-completion so that quotes are not closed unless the completion is...
r412 #print "rp",relpath # dbg
fptest
Fix local variable error.
r413 lglob = glob.glob
fptest
- Fix tab-completion so that quotes are not closed unless the completion is...
r412 isdir = os.path.isdir
vivainio
%cd and %run completers. try 'foo' and '%foo' completer if command line has 'foo'
r402 if relpath.startswith('~'):
relpath = os.path.expanduser(relpath)
fptest
Fix local variable error.
r413 dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*')
fptest
- Fix tab-completion so that quotes are not closed unless the completion is...
r412 if isdir(f)]
vivainio
%run completer also allows *.ipy files
r496 pys = [f.replace('\\','/') for f in lglob(relpath+'*.py') + lglob(relpath+'*.ipy')]
vivainio
%cd and %run completers. try 'foo' and '%foo' completer if command line has 'foo'
r402 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
yet again resort to complex file name completer more often
r484 #print event # dbg
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
reindent ipy_stock_completers
r497
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
reindent ipy_stock_completers
r497
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('\\','/')
vivainio
yet again resort to complex file name completer more often
r484 found = []
vivainio
reindent ipy_stock_completers
r497 for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*')
if os.path.isdir(f)]:
vivainio
yet again resort to complex file name completer more often
r484 if ' ' in d:
# we don't want to deal with any of that, complex code
# for this is elsewhere
raise IPython.ipapi.TryNext
found.append( d )
vivainio
%cd and %run completers. try 'foo' and '%foo' completer if command line has 'foo'
r402 if not found:
vivainio
cd completer fixed to deal (partially) with dir names that have spaces in them (broken on win32)
r481 if os.path.isdir(relpath):
return [relpath]
vivainio
reindent ipy_stock_completers
r497 raise IPython.ipapi.TryNext
vivainio
%cd and %run completers. try 'foo' and '%foo' completer if command line has 'foo'
r402 return found
fptest
- Fix tab-completion so that quotes are not closed unless the completion is...
r412 ip.set_hook('complete_command', cd_completer, str_key = '%cd')