Show More
@@ -1,386 +1,386 b'' | |||||
1 |
|
1 | |||
2 | """ Implementations for various useful completers |
|
2 | """ Implementations for various useful completers | |
3 |
|
3 | |||
4 | See Extensions/ipy_stock_completers.py on examples of how to enable a completer, |
|
4 | See Extensions/ipy_stock_completers.py on examples of how to enable a completer, | |
5 | but the basic idea is to do: |
|
5 | but the basic idea is to do: | |
6 |
|
6 | |||
7 | ip.set_hook('complete_command', svn_completer, str_key = 'svn') |
|
7 | ip.set_hook('complete_command', svn_completer, str_key = 'svn') | |
8 |
|
8 | |||
9 | """ |
|
9 | """ | |
10 | import IPython.ipapi |
|
10 | import IPython.ipapi | |
11 | import glob,os,shlex,sys |
|
11 | import glob,os,shlex,sys | |
12 | import inspect |
|
12 | import inspect | |
13 | from time import time |
|
13 | from time import time | |
14 | from zipimport import zipimporter |
|
14 | from zipimport import zipimporter | |
15 | ip = IPython.ipapi.get() |
|
15 | ip = IPython.ipapi.get() | |
16 |
|
16 | |||
17 | try: |
|
17 | try: | |
18 | set |
|
18 | set | |
19 | except: |
|
19 | except: | |
20 | from sets import Set as set |
|
20 | from sets import Set as set | |
21 |
|
21 | |||
22 | TIMEOUT_STORAGE = 3 #Time in seconds after which the rootmodules will be stored |
|
22 | TIMEOUT_STORAGE = 3 #Time in seconds after which the rootmodules will be stored | |
23 | TIMEOUT_GIVEUP = 20 #Time in seconds after which we give up |
|
23 | TIMEOUT_GIVEUP = 20 #Time in seconds after which we give up | |
24 |
|
24 | |||
25 | def quick_completer(cmd, completions): |
|
25 | def quick_completer(cmd, completions): | |
26 | """ Easily create a trivial completer for a command. |
|
26 | """ Easily create a trivial completer for a command. | |
27 |
|
27 | |||
28 | Takes either a list of completions, or all completions in string |
|
28 | Takes either a list of completions, or all completions in string | |
29 | (that will be split on whitespace) |
|
29 | (that will be split on whitespace) | |
30 |
|
30 | |||
31 | Example:: |
|
31 | Example:: | |
32 |
|
32 | |||
33 | [d:\ipython]|1> import ipy_completers |
|
33 | [d:\ipython]|1> import ipy_completers | |
34 | [d:\ipython]|2> ipy_completers.quick_completer('foo', ['bar','baz']) |
|
34 | [d:\ipython]|2> ipy_completers.quick_completer('foo', ['bar','baz']) | |
35 | [d:\ipython]|3> foo b<TAB> |
|
35 | [d:\ipython]|3> foo b<TAB> | |
36 | bar baz |
|
36 | bar baz | |
37 | [d:\ipython]|3> foo ba |
|
37 | [d:\ipython]|3> foo ba | |
38 | """ |
|
38 | """ | |
39 | if isinstance(completions, basestring): |
|
39 | if isinstance(completions, basestring): | |
40 |
|
40 | |||
41 | completions = completions.split() |
|
41 | completions = completions.split() | |
42 | def do_complete(self,event): |
|
42 | def do_complete(self,event): | |
43 | return completions |
|
43 | return completions | |
44 |
|
44 | |||
45 | ip.set_hook('complete_command',do_complete, str_key = cmd) |
|
45 | ip.set_hook('complete_command',do_complete, str_key = cmd) | |
46 |
|
46 | |||
47 | def getRootModules(): |
|
47 | def getRootModules(): | |
48 | """ |
|
48 | """ | |
49 | Returns a list containing the names of all the modules available in the |
|
49 | Returns a list containing the names of all the modules available in the | |
50 | folders of the pythonpath. |
|
50 | folders of the pythonpath. | |
51 | """ |
|
51 | """ | |
52 | modules = [] |
|
52 | modules = [] | |
53 | if ip.db.has_key('rootmodules'): |
|
53 | if ip.db.has_key('rootmodules'): | |
54 | return ip.db['rootmodules'] |
|
54 | return ip.db['rootmodules'] | |
55 | t = time() |
|
55 | t = time() | |
56 | store = False |
|
56 | store = False | |
57 | for path in sys.path: |
|
57 | for path in sys.path: | |
58 | modules += moduleList(path) |
|
58 | modules += moduleList(path) | |
59 | if time() - t >= TIMEOUT_STORAGE and not store: |
|
59 | if time() - t >= TIMEOUT_STORAGE and not store: | |
60 | store = True |
|
60 | store = True | |
61 | print "\nCaching the list of root modules, please wait!" |
|
61 | print "\nCaching the list of root modules, please wait!" | |
62 | print "(This will only be done once - type '%rehashx' to " + \ |
|
62 | print "(This will only be done once - type '%rehashx' to " + \ | |
63 | "reset cache!)" |
|
63 | "reset cache!)" | |
64 |
|
64 | |||
65 | if time() - t > TIMEOUT_GIVEUP: |
|
65 | if time() - t > TIMEOUT_GIVEUP: | |
66 | print "This is taking too long, we give up." |
|
66 | print "This is taking too long, we give up." | |
67 |
|
67 | |||
68 | ip.db['rootmodules'] = [] |
|
68 | ip.db['rootmodules'] = [] | |
69 | return [] |
|
69 | return [] | |
70 |
|
70 | |||
71 | modules += sys.builtin_module_names |
|
71 | modules += sys.builtin_module_names | |
72 |
|
72 | |||
73 | modules = list(set(modules)) |
|
73 | modules = list(set(modules)) | |
74 | if '__init__' in modules: |
|
74 | if '__init__' in modules: | |
75 | modules.remove('__init__') |
|
75 | modules.remove('__init__') | |
76 | modules = list(set(modules)) |
|
76 | modules = list(set(modules)) | |
77 | if store: |
|
77 | if store: | |
78 | ip.db['rootmodules'] = modules |
|
78 | ip.db['rootmodules'] = modules | |
79 | return modules |
|
79 | return modules | |
80 |
|
80 | |||
81 | def moduleList(path): |
|
81 | def moduleList(path): | |
82 | """ |
|
82 | """ | |
83 | Return the list containing the names of the modules available in the given |
|
83 | Return the list containing the names of the modules available in the given | |
84 | folder. |
|
84 | folder. | |
85 | """ |
|
85 | """ | |
86 |
|
86 | |||
87 | if os.path.isdir(path): |
|
87 | if os.path.isdir(path): | |
88 | folder_list = os.listdir(path) |
|
88 | folder_list = os.listdir(path) | |
89 | elif path.endswith('.egg'): |
|
89 | elif path.endswith('.egg'): | |
90 | try: |
|
90 | try: | |
91 | folder_list = [f for f in zipimporter(path)._files] |
|
91 | folder_list = [f for f in zipimporter(path)._files] | |
92 | except: |
|
92 | except: | |
93 | folder_list = [] |
|
93 | folder_list = [] | |
94 | else: |
|
94 | else: | |
95 | folder_list = [] |
|
95 | folder_list = [] | |
96 | #folder_list = glob.glob(os.path.join(path,'*')) |
|
96 | #folder_list = glob.glob(os.path.join(path,'*')) | |
97 | folder_list = [p for p in folder_list \ |
|
97 | folder_list = [p for p in folder_list \ | |
98 | if os.path.exists(os.path.join(path, p,'__init__.py'))\ |
|
98 | if os.path.exists(os.path.join(path, p,'__init__.py'))\ | |
99 | or p[-3:] in ('.py','.so')\ |
|
99 | or p[-3:] in ('.py','.so')\ | |
100 | or p[-4:] in ('.pyc','.pyo','.pyd')] |
|
100 | or p[-4:] in ('.pyc','.pyo','.pyd')] | |
101 |
|
101 | |||
102 | folder_list = [os.path.basename(p).split('.')[0] for p in folder_list] |
|
102 | folder_list = [os.path.basename(p).split('.')[0] for p in folder_list] | |
103 | return folder_list |
|
103 | return folder_list | |
104 |
|
104 | |||
105 | def moduleCompletion(line): |
|
105 | def moduleCompletion(line): | |
106 | """ |
|
106 | """ | |
107 | Returns a list containing the completion possibilities for an import line. |
|
107 | Returns a list containing the completion possibilities for an import line. | |
108 | The line looks like this : |
|
108 | The line looks like this : | |
109 | 'import xml.d' |
|
109 | 'import xml.d' | |
110 | 'from xml.dom import' |
|
110 | 'from xml.dom import' | |
111 | """ |
|
111 | """ | |
112 | def tryImport(mod, only_modules=False): |
|
112 | def tryImport(mod, only_modules=False): | |
113 | def isImportable(module, attr): |
|
113 | def isImportable(module, attr): | |
114 | if only_modules: |
|
114 | if only_modules: | |
115 | return inspect.ismodule(getattr(module, attr)) |
|
115 | return inspect.ismodule(getattr(module, attr)) | |
116 | else: |
|
116 | else: | |
117 | return not(attr[:2] == '__' and attr[-2:] == '__') |
|
117 | return not(attr[:2] == '__' and attr[-2:] == '__') | |
118 | try: |
|
118 | try: | |
119 | m = __import__(mod) |
|
119 | m = __import__(mod) | |
120 | except: |
|
120 | except: | |
121 | return [] |
|
121 | return [] | |
122 | mods = mod.split('.') |
|
122 | mods = mod.split('.') | |
123 | for module in mods[1:]: |
|
123 | for module in mods[1:]: | |
124 | m = getattr(m,module) |
|
124 | m = getattr(m,module) | |
125 | if (not hasattr(m, '__file__')) or (not only_modules) or\ |
|
125 | if (not hasattr(m, '__file__')) or (not only_modules) or\ | |
126 | (hasattr(m, '__file__') and '__init__' in m.__file__): |
|
126 | (hasattr(m, '__file__') and '__init__' in m.__file__): | |
127 | completion_list = [attr for attr in dir(m) if isImportable(m, attr)] |
|
127 | completion_list = [attr for attr in dir(m) if isImportable(m, attr)] | |
128 | completion_list.extend(getattr(m,'__all__',[])) |
|
128 | completion_list.extend(getattr(m,'__all__',[])) | |
129 | if hasattr(m, '__file__') and '__init__' in m.__file__: |
|
129 | if hasattr(m, '__file__') and '__init__' in m.__file__: | |
130 | completion_list.extend(moduleList(os.path.dirname(m.__file__))) |
|
130 | completion_list.extend(moduleList(os.path.dirname(m.__file__))) | |
131 | completion_list = list(set(completion_list)) |
|
131 | completion_list = list(set(completion_list)) | |
132 | if '__init__' in completion_list: |
|
132 | if '__init__' in completion_list: | |
133 | completion_list.remove('__init__') |
|
133 | completion_list.remove('__init__') | |
134 | return completion_list |
|
134 | return completion_list | |
135 |
|
135 | |||
136 | words = line.split(' ') |
|
136 | words = line.split(' ') | |
137 | if len(words) == 3 and words[0] == 'from': |
|
137 | if len(words) == 3 and words[0] == 'from': | |
138 | return ['import '] |
|
138 | return ['import '] | |
139 | if len(words) < 3 and (words[0] in ['import','from']) : |
|
139 | if len(words) < 3 and (words[0] in ['import','from']) : | |
140 | if len(words) == 1: |
|
140 | if len(words) == 1: | |
141 | return getRootModules() |
|
141 | return getRootModules() | |
142 | mod = words[1].split('.') |
|
142 | mod = words[1].split('.') | |
143 | if len(mod) < 2: |
|
143 | if len(mod) < 2: | |
144 | return getRootModules() |
|
144 | return getRootModules() | |
145 | completion_list = tryImport('.'.join(mod[:-1]), True) |
|
145 | completion_list = tryImport('.'.join(mod[:-1]), True) | |
146 | completion_list = ['.'.join(mod[:-1] + [el]) for el in completion_list] |
|
146 | completion_list = ['.'.join(mod[:-1] + [el]) for el in completion_list] | |
147 | return completion_list |
|
147 | return completion_list | |
148 | if len(words) >= 3 and words[0] == 'from': |
|
148 | if len(words) >= 3 and words[0] == 'from': | |
149 | mod = words[1] |
|
149 | mod = words[1] | |
150 | return tryImport(mod) |
|
150 | return tryImport(mod) | |
151 |
|
151 | |||
152 | def vcs_completer(commands, event): |
|
152 | def vcs_completer(commands, event): | |
153 | """ utility to make writing typical version control app completers easier |
|
153 | """ utility to make writing typical version control app completers easier | |
154 |
|
154 | |||
155 | VCS command line apps typically have the format: |
|
155 | VCS command line apps typically have the format: | |
156 |
|
156 | |||
157 | [sudo ]PROGNAME [help] [command] file file... |
|
157 | [sudo ]PROGNAME [help] [command] file file... | |
158 |
|
158 | |||
159 | """ |
|
159 | """ | |
160 |
|
160 | |||
161 |
|
161 | |||
162 | cmd_param = event.line.split() |
|
162 | cmd_param = event.line.split() | |
163 | if event.line.endswith(' '): |
|
163 | if event.line.endswith(' '): | |
164 | cmd_param.append('') |
|
164 | cmd_param.append('') | |
165 |
|
165 | |||
166 | if cmd_param[0] == 'sudo': |
|
166 | if cmd_param[0] == 'sudo': | |
167 | cmd_param = cmd_param[1:] |
|
167 | cmd_param = cmd_param[1:] | |
168 |
|
168 | |||
169 | if len(cmd_param) == 2 or 'help' in cmd_param: |
|
169 | if len(cmd_param) == 2 or 'help' in cmd_param: | |
170 | return commands.split() |
|
170 | return commands.split() | |
171 |
|
171 | |||
172 | return ip.IP.Completer.file_matches(event.symbol) |
|
172 | return ip.IP.Completer.file_matches(event.symbol) | |
173 |
|
173 | |||
174 |
|
174 | |||
175 | pkg_cache = None |
|
175 | pkg_cache = None | |
176 |
|
176 | |||
177 | def module_completer(self,event): |
|
177 | def module_completer(self,event): | |
178 | """ Give completions after user has typed 'import ...' or 'from ...'""" |
|
178 | """ Give completions after user has typed 'import ...' or 'from ...'""" | |
179 |
|
179 | |||
180 | # This works in all versions of python. While 2.5 has |
|
180 | # This works in all versions of python. While 2.5 has | |
181 | # pkgutil.walk_packages(), that particular routine is fairly dangerous, |
|
181 | # pkgutil.walk_packages(), that particular routine is fairly dangerous, | |
182 | # since it imports *EVERYTHING* on sys.path. That is: a) very slow b) full |
|
182 | # since it imports *EVERYTHING* on sys.path. That is: a) very slow b) full | |
183 | # of possibly problematic side effects. |
|
183 | # of possibly problematic side effects. | |
184 | # This search the folders in the sys.path for available modules. |
|
184 | # This search the folders in the sys.path for available modules. | |
185 |
|
185 | |||
186 | return moduleCompletion(event.line) |
|
186 | return moduleCompletion(event.line) | |
187 |
|
187 | |||
188 |
|
188 | |||
189 | svn_commands = """\ |
|
189 | svn_commands = """\ | |
190 | add blame praise annotate ann cat checkout co cleanup commit ci copy |
|
190 | add blame praise annotate ann cat checkout co cleanup commit ci copy | |
191 | cp delete del remove rm diff di export help ? h import info list ls |
|
191 | cp delete del remove rm diff di export help ? h import info list ls | |
192 | lock log merge mkdir move mv rename ren propdel pdel pd propedit pedit |
|
192 | lock log merge mkdir move mv rename ren propdel pdel pd propedit pedit | |
193 | pe propget pget pg proplist plist pl propset pset ps resolved revert |
|
193 | pe propget pget pg proplist plist pl propset pset ps resolved revert | |
194 | status stat st switch sw unlock update |
|
194 | status stat st switch sw unlock update | |
195 | """ |
|
195 | """ | |
196 |
|
196 | |||
197 | def svn_completer(self,event): |
|
197 | def svn_completer(self,event): | |
198 | return vcs_completer(svn_commands, event) |
|
198 | return vcs_completer(svn_commands, event) | |
199 |
|
199 | |||
200 |
|
200 | |||
201 | hg_commands = """ |
|
201 | hg_commands = """ | |
202 | add addremove annotate archive backout branch branches bundle cat |
|
202 | add addremove annotate archive backout branch branches bundle cat | |
203 | clone commit copy diff export grep heads help identify import incoming |
|
203 | clone commit copy diff export grep heads help identify import incoming | |
204 | init locate log manifest merge outgoing parents paths pull push |
|
204 | init locate log manifest merge outgoing parents paths pull push | |
205 | qapplied qclone qcommit qdelete qdiff qfold qguard qheader qimport |
|
205 | qapplied qclone qcommit qdelete qdiff qfold qguard qheader qimport | |
206 | qinit qnew qnext qpop qprev qpush qrefresh qrename qrestore qsave |
|
206 | qinit qnew qnext qpop qprev qpush qrefresh qrename qrestore qsave | |
207 | qselect qseries qtop qunapplied recover remove rename revert rollback |
|
207 | qselect qseries qtop qunapplied recover remove rename revert rollback | |
208 | root serve showconfig status strip tag tags tip unbundle update verify |
|
208 | root serve showconfig status strip tag tags tip unbundle update verify | |
209 | version |
|
209 | version | |
210 | """ |
|
210 | """ | |
211 |
|
211 | |||
212 | def hg_completer(self,event): |
|
212 | def hg_completer(self,event): | |
213 | """ Completer for mercurial commands """ |
|
213 | """ Completer for mercurial commands """ | |
214 |
|
214 | |||
215 | return vcs_completer(hg_commands, event) |
|
215 | return vcs_completer(hg_commands, event) | |
216 |
|
216 | |||
217 |
|
217 | |||
218 |
|
218 | |||
219 | __bzr_commands = None |
|
219 | __bzr_commands = None | |
220 |
|
220 | |||
221 | def bzr_commands(): |
|
221 | def bzr_commands(): | |
222 | global __bzr_commands |
|
222 | global __bzr_commands | |
223 | if __bzr_commands is not None: |
|
223 | if __bzr_commands is not None: | |
224 | return __bzr_commands |
|
224 | return __bzr_commands | |
225 | out = os.popen('bzr help commands') |
|
225 | out = os.popen('bzr help commands') | |
226 | __bzr_commands = [l.split()[0] for l in out] |
|
226 | __bzr_commands = [l.split()[0] for l in out] | |
227 | return __bzr_commands |
|
227 | return __bzr_commands | |
228 |
|
228 | |||
229 | def bzr_completer(self,event): |
|
229 | def bzr_completer(self,event): | |
230 | """ Completer for bazaar commands """ |
|
230 | """ Completer for bazaar commands """ | |
231 | cmd_param = event.line.split() |
|
231 | cmd_param = event.line.split() | |
232 | if event.line.endswith(' '): |
|
232 | if event.line.endswith(' '): | |
233 | cmd_param.append('') |
|
233 | cmd_param.append('') | |
234 |
|
234 | |||
235 | if len(cmd_param) > 2: |
|
235 | if len(cmd_param) > 2: | |
236 | cmd = cmd_param[1] |
|
236 | cmd = cmd_param[1] | |
237 | param = cmd_param[-1] |
|
237 | param = cmd_param[-1] | |
238 | output_file = (param == '--output=') |
|
238 | output_file = (param == '--output=') | |
239 | if cmd == 'help': |
|
239 | if cmd == 'help': | |
240 | return bzr_commands() |
|
240 | return bzr_commands() | |
241 | elif cmd in ['bundle-revisions','conflicts', |
|
241 | elif cmd in ['bundle-revisions','conflicts', | |
242 | 'deleted','nick','register-branch', |
|
242 | 'deleted','nick','register-branch', | |
243 | 'serve','unbind','upgrade','version', |
|
243 | 'serve','unbind','upgrade','version', | |
244 | 'whoami'] and not output_file: |
|
244 | 'whoami'] and not output_file: | |
245 | return [] |
|
245 | return [] | |
246 | else: |
|
246 | else: | |
247 | # the rest are probably file names |
|
247 | # the rest are probably file names | |
248 | return ip.IP.Completer.file_matches(event.symbol) |
|
248 | return ip.IP.Completer.file_matches(event.symbol) | |
249 |
|
249 | |||
250 | return bzr_commands() |
|
250 | return bzr_commands() | |
251 |
|
251 | |||
252 |
|
252 | |||
253 | def shlex_split(x): |
|
253 | def shlex_split(x): | |
254 | """Helper function to split lines into segments.""" |
|
254 | """Helper function to split lines into segments.""" | |
255 | #shlex.split raise exception if syntax error in sh syntax |
|
255 | #shlex.split raise exception if syntax error in sh syntax | |
256 | #for example if no closing " is found. This function keeps dropping |
|
256 | #for example if no closing " is found. This function keeps dropping | |
257 | #the last character of the line until shlex.split does not raise |
|
257 | #the last character of the line until shlex.split does not raise | |
258 | #exception. Adds end of the line to the result of shlex.split |
|
258 | #exception. Adds end of the line to the result of shlex.split | |
259 | #example: %run "c:/python -> ['%run','"c:/python'] |
|
259 | #example: %run "c:/python -> ['%run','"c:/python'] | |
260 | endofline=[] |
|
260 | endofline=[] | |
261 | while x!="": |
|
261 | while x!="": | |
262 | try: |
|
262 | try: | |
263 | comps=shlex.split(x) |
|
263 | comps=shlex.split(x) | |
264 | if len(endofline)>=1: |
|
264 | if len(endofline)>=1: | |
265 | comps.append("".join(endofline)) |
|
265 | comps.append("".join(endofline)) | |
266 | return comps |
|
266 | return comps | |
267 | except ValueError: |
|
267 | except ValueError: | |
268 | endofline=[x[-1:]]+endofline |
|
268 | endofline=[x[-1:]]+endofline | |
269 | x=x[:-1] |
|
269 | x=x[:-1] | |
270 | return ["".join(endofline)] |
|
270 | return ["".join(endofline)] | |
271 |
|
271 | |||
272 | def runlistpy(self, event): |
|
272 | def runlistpy(self, event): | |
273 | comps = shlex_split(event.line) |
|
273 | comps = shlex_split(event.line) | |
274 | relpath = (len(comps) > 1 and comps[-1] or '').strip("'\"") |
|
274 | relpath = (len(comps) > 1 and comps[-1] or '').strip("'\"") | |
275 |
|
275 | |||
276 | #print "\nev=",event # dbg |
|
276 | #print "\nev=",event # dbg | |
277 | #print "rp=",relpath # dbg |
|
277 | #print "rp=",relpath # dbg | |
278 | #print 'comps=',comps # dbg |
|
278 | #print 'comps=',comps # dbg | |
279 |
|
279 | |||
280 | lglob = glob.glob |
|
280 | lglob = glob.glob | |
281 | isdir = os.path.isdir |
|
281 | isdir = os.path.isdir | |
282 | if relpath.startswith('~'): |
|
282 | if relpath.startswith('~'): | |
283 | relpath = os.path.expanduser(relpath) |
|
283 | relpath = os.path.expanduser(relpath) | |
284 | dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*') |
|
284 | dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*') | |
285 | if isdir(f)] |
|
285 | if isdir(f)] | |
286 |
|
286 | |||
287 | # Find if the user has already typed the first filename, after which we |
|
287 | # Find if the user has already typed the first filename, after which we | |
288 | # should complete on all files, since after the first one other files may |
|
288 | # should complete on all files, since after the first one other files may | |
289 | # be arguments to the input script. |
|
289 | # be arguments to the input script. | |
290 | #filter( |
|
290 | #filter( | |
291 | if filter(lambda f: f.endswith('.py') or f.endswith('.ipy') or |
|
291 | if filter(lambda f: f.endswith('.py') or f.endswith('.ipy') or | |
292 | f.endswith('.pyw'),comps): |
|
292 | f.endswith('.pyw'),comps): | |
293 | pys = [f.replace('\\','/') for f in lglob('*')] |
|
293 | pys = [f.replace('\\','/') for f in lglob('*')] | |
294 | else: |
|
294 | else: | |
295 | pys = [f.replace('\\','/') |
|
295 | pys = [f.replace('\\','/') | |
296 | for f in lglob(relpath+'*.py') + lglob(relpath+'*.ipy') + |
|
296 | for f in lglob(relpath+'*.py') + lglob(relpath+'*.ipy') + | |
297 | lglob(relpath + '*.pyw')] |
|
297 | lglob(relpath + '*.pyw')] | |
298 | return dirs + pys |
|
298 | return dirs + pys | |
299 |
|
299 | |||
300 |
|
300 | |||
301 | def cd_completer(self, event): |
|
301 | def cd_completer(self, event): | |
302 | relpath = event.symbol |
|
302 | relpath = event.symbol | |
303 | #print event # dbg |
|
303 | #print event # dbg | |
304 | if '-b' in event.line: |
|
304 | if '-b' in event.line: | |
305 | # return only bookmark completions |
|
305 | # return only bookmark completions | |
306 | bkms = self.db.get('bookmarks',{}) |
|
306 | bkms = self.db.get('bookmarks',{}) | |
307 | return bkms.keys() |
|
307 | return bkms.keys() | |
308 |
|
308 | |||
309 |
|
309 | |||
310 | if event.symbol == '-': |
|
310 | if event.symbol == '-': | |
311 | width_dh = str(len(str(len(ip.user_ns['_dh']) + 1))) |
|
311 | width_dh = str(len(str(len(ip.user_ns['_dh']) + 1))) | |
312 | # jump in directory history by number |
|
312 | # jump in directory history by number | |
313 | fmt = '-%0' + width_dh +'d [%s]' |
|
313 | fmt = '-%0' + width_dh +'d [%s]' | |
314 | ents = [ fmt % (i,s) for i,s in enumerate(ip.user_ns['_dh'])] |
|
314 | ents = [ fmt % (i,s) for i,s in enumerate(ip.user_ns['_dh'])] | |
315 | if len(ents) > 1: |
|
315 | if len(ents) > 1: | |
316 | return ents |
|
316 | return ents | |
317 | return [] |
|
317 | return [] | |
318 |
|
318 | |||
319 | if relpath.startswith('~'): |
|
319 | if relpath.startswith('~'): | |
320 | relpath = os.path.expanduser(relpath).replace('\\','/') |
|
320 | relpath = os.path.expanduser(relpath).replace('\\','/') | |
321 | found = [] |
|
321 | found = [] | |
322 | for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*') |
|
322 | for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*') | |
323 | if os.path.isdir(f)]: |
|
323 | if os.path.isdir(f)]: | |
324 | if ' ' in d: |
|
324 | if ' ' in d: | |
325 | # we don't want to deal with any of that, complex code |
|
325 | # we don't want to deal with any of that, complex code | |
326 | # for this is elsewhere |
|
326 | # for this is elsewhere | |
327 | raise IPython.ipapi.TryNext |
|
327 | raise IPython.ipapi.TryNext | |
328 | found.append( d ) |
|
328 | found.append( d ) | |
329 |
|
329 | |||
330 | if not found: |
|
330 | if not found: | |
331 | if os.path.isdir(relpath): |
|
331 | if os.path.isdir(relpath): | |
332 | return [relpath] |
|
332 | return [relpath] | |
333 | raise IPython.ipapi.TryNext |
|
333 | raise IPython.ipapi.TryNext | |
334 |
|
334 | |||
335 |
|
335 | |||
336 | def single_dir_expand(matches): |
|
336 | def single_dir_expand(matches): | |
337 | "Recursively expand match lists containing a single dir." |
|
337 | "Recursively expand match lists containing a single dir." | |
338 |
|
338 | |||
339 | if len(matches) == 1 and os.path.isdir(matches[0]): |
|
339 | if len(matches) == 1 and os.path.isdir(matches[0]): | |
340 | # Takes care of links to directories also. Use '/' |
|
340 | # Takes care of links to directories also. Use '/' | |
341 | # explicitly, even under Windows, so that name completions |
|
341 | # explicitly, even under Windows, so that name completions | |
342 | # don't end up escaped. |
|
342 | # don't end up escaped. | |
343 | d = matches[0] |
|
343 | d = matches[0] | |
344 | if d[-1] in ['/','\\']: |
|
344 | if d[-1] in ['/','\\']: | |
345 | d = d[:-1] |
|
345 | d = d[:-1] | |
346 |
|
346 | |||
347 | subdirs = [p for p in os.listdir(d) if os.path.isdir( d + '/' + p)] |
|
347 | subdirs = [p for p in os.listdir(d) if os.path.isdir( d + '/' + p) and not p.startswith('.')] | |
348 | if subdirs: |
|
348 | if subdirs: | |
349 | matches = [ (d + '/' + p) for p in subdirs ] |
|
349 | matches = [ (d + '/' + p) for p in subdirs ] | |
350 | return single_dir_expand(matches) |
|
350 | return single_dir_expand(matches) | |
351 | else: |
|
351 | else: | |
352 | return matches |
|
352 | return matches | |
353 | else: |
|
353 | else: | |
354 | return matches |
|
354 | return matches | |
355 |
|
355 | |||
356 | return single_dir_expand(found) |
|
356 | return single_dir_expand(found) | |
357 |
|
357 | |||
358 | def apt_get_packages(prefix): |
|
358 | def apt_get_packages(prefix): | |
359 | out = os.popen('apt-cache pkgnames') |
|
359 | out = os.popen('apt-cache pkgnames') | |
360 | for p in out: |
|
360 | for p in out: | |
361 | if p.startswith(prefix): |
|
361 | if p.startswith(prefix): | |
362 | yield p.rstrip() |
|
362 | yield p.rstrip() | |
363 |
|
363 | |||
364 |
|
364 | |||
365 | apt_commands = """\ |
|
365 | apt_commands = """\ | |
366 | update upgrade install remove purge source build-dep dist-upgrade |
|
366 | update upgrade install remove purge source build-dep dist-upgrade | |
367 | dselect-upgrade clean autoclean check""" |
|
367 | dselect-upgrade clean autoclean check""" | |
368 |
|
368 | |||
369 | def apt_completer(self, event): |
|
369 | def apt_completer(self, event): | |
370 | """ Completer for apt-get (uses apt-cache internally) |
|
370 | """ Completer for apt-get (uses apt-cache internally) | |
371 |
|
371 | |||
372 | """ |
|
372 | """ | |
373 |
|
373 | |||
374 |
|
374 | |||
375 | cmd_param = event.line.split() |
|
375 | cmd_param = event.line.split() | |
376 | if event.line.endswith(' '): |
|
376 | if event.line.endswith(' '): | |
377 | cmd_param.append('') |
|
377 | cmd_param.append('') | |
378 |
|
378 | |||
379 | if cmd_param[0] == 'sudo': |
|
379 | if cmd_param[0] == 'sudo': | |
380 | cmd_param = cmd_param[1:] |
|
380 | cmd_param = cmd_param[1:] | |
381 |
|
381 | |||
382 | if len(cmd_param) == 2 or 'help' in cmd_param: |
|
382 | if len(cmd_param) == 2 or 'help' in cmd_param: | |
383 | return apt_commands.split() |
|
383 | return apt_commands.split() | |
384 |
|
384 | |||
385 | return list(apt_get_packages(event.symbol)) |
|
385 | return list(apt_get_packages(event.symbol)) | |
386 |
|
386 |
General Comments 0
You need to be logged in to leave comments.
Login now