Show More
@@ -1,4 +1,4 b'' | |||||
1 |
""" Tab completion support for a couple of linux package managers |
|
1 | """ Tab completion support for a couple of linux package managers | |
2 |
|
2 | |||
3 | This is also an example of how to write custom completer plugins |
|
3 | This is also an example of how to write custom completer plugins | |
4 | or hooks. |
|
4 | or hooks. | |
@@ -17,51 +17,51 b' import glob,os,shlex,sys' | |||||
17 | ip = IPython.ipapi.get() |
|
17 | ip = IPython.ipapi.get() | |
18 |
|
18 | |||
19 | def vcs_completer(commands, event): |
|
19 | def vcs_completer(commands, event): | |
20 |
""" utility to make writing typical version control app completers easier |
|
20 | """ utility to make writing typical version control app completers easier | |
21 |
|
21 | |||
22 | VCS command line apps typically have the format: |
|
22 | VCS command line apps typically have the format: | |
23 |
|
23 | |||
24 | [sudo ]PROGNAME [help] [command] file file... |
|
24 | [sudo ]PROGNAME [help] [command] file file... | |
25 |
|
25 | |||
26 | """ |
|
26 | """ | |
27 |
|
27 | |||
28 |
|
28 | |||
29 | cmd_param = event.line.split() |
|
29 | cmd_param = event.line.split() | |
30 | if event.line.endswith(' '): |
|
30 | if event.line.endswith(' '): | |
31 | cmd_param.append('') |
|
31 | cmd_param.append('') | |
32 |
|
32 | |||
33 | if cmd_param[0] == 'sudo': |
|
33 | if cmd_param[0] == 'sudo': | |
34 | cmd_param = cmd_param[1:] |
|
34 | cmd_param = cmd_param[1:] | |
35 |
|
35 | |||
36 | if len(cmd_param) == 2 or 'help' in cmd_param: |
|
36 | if len(cmd_param) == 2 or 'help' in cmd_param: | |
37 | return commands.split() |
|
37 | return commands.split() | |
38 |
|
38 | |||
39 | return ip.IP.Completer.file_matches(event.symbol) |
|
39 | return ip.IP.Completer.file_matches(event.symbol) | |
40 |
|
40 | |||
41 |
|
41 | |||
42 |
|
42 | |||
43 | def apt_completers(self, event): |
|
43 | def apt_completers(self, event): | |
44 | """ This should return a list of strings with possible completions. |
|
44 | """ This should return a list of strings with possible completions. | |
45 |
|
45 | |||
46 | Note that all the included strings that don't start with event.symbol |
|
46 | Note that all the included strings that don't start with event.symbol | |
47 | are removed, in order to not confuse readline. |
|
47 | are removed, in order to not confuse readline. | |
48 |
|
48 | |||
49 | """ |
|
49 | """ | |
50 | # print event # dbg |
|
50 | # print event # dbg | |
51 |
|
51 | |||
52 | # commands are only suggested for the 'command' part of package manager |
|
52 | # commands are only suggested for the 'command' part of package manager | |
53 | # invocation |
|
53 | # invocation | |
54 |
|
54 | |||
55 | cmd = (event.line + "<placeholder>").rsplit(None,1)[0] |
|
55 | cmd = (event.line + "<placeholder>").rsplit(None,1)[0] | |
56 | # print cmd |
|
56 | # print cmd | |
57 | if cmd.endswith('apt-get') or cmd.endswith('yum'): |
|
57 | if cmd.endswith('apt-get') or cmd.endswith('yum'): | |
58 | return ['update', 'upgrade', 'install', 'remove'] |
|
58 | return ['update', 'upgrade', 'install', 'remove'] | |
59 |
|
59 | |||
60 |
# later on, add dpkg -l / whatever to get list of possible |
|
60 | # later on, add dpkg -l / whatever to get list of possible | |
61 | # packages, add switches etc. for the rest of command line |
|
61 | # packages, add switches etc. for the rest of command line | |
62 | # filling |
|
62 | # filling | |
63 |
|
63 | |||
64 |
raise IPython.ipapi.TryNext |
|
64 | raise IPython.ipapi.TryNext | |
65 |
|
65 | |||
66 |
|
66 | |||
67 | # re_key specifies the regexp that triggers the specified completer |
|
67 | # re_key specifies the regexp that triggers the specified completer | |
@@ -71,9 +71,9 b" ip.set_hook('complete_command', apt_completers, re_key = '.*yum')" | |||||
71 |
|
71 | |||
72 | pkg_cache = None |
|
72 | pkg_cache = None | |
73 |
|
73 | |||
74 |
def module_completer(self,event): |
|
74 | def module_completer(self,event): | |
75 | """ Give completions after user has typed 'import' """ |
|
75 | """ Give completions after user has typed 'import' """ | |
76 |
|
76 | |||
77 | # only a local version for py 2.4, pkgutil has no walk_packages() there |
|
77 | # only a local version for py 2.4, pkgutil has no walk_packages() there | |
78 | if sys.version_info < (2,5): |
|
78 | if sys.version_info < (2,5): | |
79 | for el in [f[:-3] for f in glob.glob("*.py")]: |
|
79 | for el in [f[:-3] for f in glob.glob("*.py")]: | |
@@ -82,11 +82,11 b' def module_completer(self,event):' | |||||
82 |
|
82 | |||
83 | global pkg_cache |
|
83 | global pkg_cache | |
84 | import pkgutil,imp,time |
|
84 | import pkgutil,imp,time | |
85 |
#current = |
|
85 | #current = | |
86 | if pkg_cache is None: |
|
86 | if pkg_cache is None: | |
87 | print "\n\n[Standby while scanning modules, this can take a while]\n\n" |
|
87 | print "\n\n[Standby while scanning modules, this can take a while]\n\n" | |
88 | pkg_cache = list(pkgutil.walk_packages()) |
|
88 | pkg_cache = list(pkgutil.walk_packages()) | |
89 |
|
89 | |||
90 | already = set() |
|
90 | already = set() | |
91 | for ld, name, ispkg in pkg_cache: |
|
91 | for ld, name, ispkg in pkg_cache: | |
92 | if name.count('.') < event.symbol.count('.') + 1: |
|
92 | if name.count('.') < event.symbol.count('.') + 1: | |
@@ -124,7 +124,7 b' version' | |||||
124 |
|
124 | |||
125 | def hg_completer(self,event): |
|
125 | def hg_completer(self,event): | |
126 | """ Completer for mercurial commands """ |
|
126 | """ Completer for mercurial commands """ | |
127 |
|
127 | |||
128 | return vcs_completer(hg_commands, event) |
|
128 | return vcs_completer(hg_commands, event) | |
129 |
|
129 | |||
130 | ip.set_hook('complete_command', hg_completer, str_key = 'hg') |
|
130 | ip.set_hook('complete_command', hg_completer, str_key = 'hg') | |
@@ -169,7 +169,7 b" ip.set_hook('complete_command', bzr_completer, str_key = 'bzr')" | |||||
169 | def runlistpy(self, event): |
|
169 | def runlistpy(self, event): | |
170 | comps = shlex.split(event.line) |
|
170 | comps = shlex.split(event.line) | |
171 | relpath = (len(comps) > 1 and comps[-1] or '') |
|
171 | relpath = (len(comps) > 1 and comps[-1] or '') | |
172 |
|
172 | |||
173 | #print "rp",relpath # dbg |
|
173 | #print "rp",relpath # dbg | |
174 | lglob = glob.glob |
|
174 | lglob = glob.glob | |
175 | isdir = os.path.isdir |
|
175 | isdir = os.path.isdir | |
@@ -189,20 +189,20 b' def cd_completer(self, event):' | |||||
189 | # return only bookmark completions |
|
189 | # return only bookmark completions | |
190 | bkms = self.db.get('bookmarks',{}) |
|
190 | bkms = self.db.get('bookmarks',{}) | |
191 | return bkms.keys() |
|
191 | return bkms.keys() | |
192 |
|
192 | |||
193 |
|
193 | |||
194 | if event.symbol == '-': |
|
194 | if event.symbol == '-': | |
195 | # jump in directory history by number |
|
195 | # jump in directory history by number | |
196 | ents = ['-%d [%s]' % (i,s) for i,s in enumerate(ip.user_ns['_dh'])] |
|
196 | ents = ['-%d [%s]' % (i,s) for i,s in enumerate(ip.user_ns['_dh'])] | |
197 | if len(ents) > 1: |
|
197 | if len(ents) > 1: | |
198 | return ents |
|
198 | return ents | |
199 | return [] |
|
199 | return [] | |
200 |
|
200 | |||
201 | if relpath.startswith('~'): |
|
201 | if relpath.startswith('~'): | |
202 | relpath = os.path.expanduser(relpath).replace('\\','/') |
|
202 | relpath = os.path.expanduser(relpath).replace('\\','/') | |
203 | found = [] |
|
203 | found = [] | |
204 |
for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*') |
|
204 | for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*') | |
205 |
if os.path.isdir(f)]: |
|
205 | if os.path.isdir(f)]: | |
206 | if ' ' in d: |
|
206 | if ' ' in d: | |
207 | # we don't want to deal with any of that, complex code |
|
207 | # we don't want to deal with any of that, complex code | |
208 | # for this is elsewhere |
|
208 | # for this is elsewhere | |
@@ -212,7 +212,7 b' def cd_completer(self, event):' | |||||
212 | if not found: |
|
212 | if not found: | |
213 | if os.path.isdir(relpath): |
|
213 | if os.path.isdir(relpath): | |
214 | return [relpath] |
|
214 | return [relpath] | |
215 |
|
|
215 | raise IPython.ipapi.TryNext | |
216 | return found |
|
216 | return found | |
217 |
|
217 | |||
218 | ip.set_hook('complete_command', cd_completer, str_key = '%cd') |
|
218 | ip.set_hook('complete_command', cd_completer, str_key = '%cd') |
General Comments 0
You need to be logged in to leave comments.
Login now