From 1c1dc96948f67bbb09a28d007bdbdd30d1ced62e 2007-11-08 21:07:01 From: vivainio Date: 2007-11-08 21:07:01 Subject: [PATCH] Add proper apt-get completer --- diff --git a/IPython/Extensions/ipy_app_completers.py b/IPython/Extensions/ipy_app_completers.py index 0277623..4d2ca5b 100644 --- a/IPython/Extensions/ipy_app_completers.py +++ b/IPython/Extensions/ipy_app_completers.py @@ -11,11 +11,7 @@ ip = IPython.ipapi.get() from ipy_completers import * -ip.set_hook('complete_command', apt_completers, re_key = '.*apt-get') -ip.set_hook('complete_command', apt_completers, re_key = '.*yum') - +ip.set_hook('complete_command', apt_completer, re_key = '.*apt-get') ip.set_hook('complete_command', svn_completer, str_key = 'svn') - ip.set_hook('complete_command', hg_completer, str_key = 'hg') - ip.set_hook('complete_command', bzr_completer, str_key = 'bzr') diff --git a/IPython/Extensions/ipy_completers.py b/IPython/Extensions/ipy_completers.py index 080bb40..1eed962 100644 --- a/IPython/Extensions/ipy_completers.py +++ b/IPython/Extensions/ipy_completers.py @@ -165,32 +165,6 @@ def vcs_completer(commands, event): return ip.IP.Completer.file_matches(event.symbol) - -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 + "").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 - - - pkg_cache = None def module_completer(self,event): @@ -350,3 +324,32 @@ def cd_completer(self, event): raise IPython.ipapi.TryNext return found +def apt_get_packages(prefix): + out = os.popen('apt-cache pkgnames') + for p in out: + if p.startswith(prefix): + yield p.rstrip() + + +apt_commands = """\ +update upgrade install remove purge source build-dep dist-upgrade +dselect-upgrade clean autoclean check""" + +def apt_completer(self, event): + """ Completer for apt-get (uses apt-cache internally) + + """ + + + 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)) + diff --git a/IPython/completer.py b/IPython/completer.py index a60d974..f45e02a 100644 --- a/IPython/completer.py +++ b/IPython/completer.py @@ -620,8 +620,8 @@ class IPCompleter(Completer): except IndexError: return None except: - #from IPython.ultraTB import AutoFormattedTB; # dbg - #tb=AutoFormattedTB('Verbose');tb() #dbg + from IPython.ultraTB import AutoFormattedTB; # dbg + tb=AutoFormattedTB('Verbose');tb() #dbg # If completion fails, don't annoy the user. return None diff --git a/doc/ChangeLog b/doc/ChangeLog index 9492985..1d95cd4 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,6 +1,9 @@ 2007-11-08 Ville Vainio - * ipy_completer.py (import completer): assume 'xml' module exists. + * ipy_completers.py (import completer): assume 'xml' module exists. Do not add every module twice anymore. Closes #196. + + * ipy_completers.py, ipy_app_completers.py: Add proper apt-get + completer that uses apt-cache to search for existing packages. 2007-11-06 Ville Vainio