##// END OF EJS Templates
Skip scanning for module names with no global shell instance...
Thomas Kluyver -
Show More
@@ -1,342 +1,347 b''
1 1 # encoding: utf-8
2 2 """Implementations for various useful completers.
3 3
4 4 These are all loaded by default by IPython.
5 5 """
6 6 #-----------------------------------------------------------------------------
7 7 # Copyright (C) 2010-2011 The IPython Development Team.
8 8 #
9 9 # Distributed under the terms of the BSD License.
10 10 #
11 11 # The full license is in the file COPYING.txt, distributed with this software.
12 12 #-----------------------------------------------------------------------------
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Imports
16 16 #-----------------------------------------------------------------------------
17 17
18 18 # Stdlib imports
19 19 import glob
20 20 import inspect
21 21 import os
22 22 import re
23 23 import sys
24 24 from importlib import import_module
25 25 from importlib.machinery import all_suffixes
26 26
27 27
28 28 # Third-party imports
29 29 from time import time
30 30 from zipimport import zipimporter
31 31
32 32 # Our own imports
33 33 from IPython.core.completer import expand_user, compress_user
34 34 from IPython.core.error import TryNext
35 35 from IPython.utils._process_common import arg_split
36 36
37 37 # FIXME: this should be pulled in with the right call via the component system
38 38 from IPython import get_ipython
39 39
40 40 #-----------------------------------------------------------------------------
41 41 # Globals and constants
42 42 #-----------------------------------------------------------------------------
43 43 _suffixes = all_suffixes()
44 44
45 45 # Time in seconds after which the rootmodules will be stored permanently in the
46 46 # ipython ip.db database (kept in the user's .ipython dir).
47 47 TIMEOUT_STORAGE = 2
48 48
49 49 # Time in seconds after which we give up
50 50 TIMEOUT_GIVEUP = 20
51 51
52 52 # Regular expression for the python import statement
53 53 import_re = re.compile(r'(?P<name>[a-zA-Z_][a-zA-Z0-9_]*?)'
54 54 r'(?P<package>[/\\]__init__)?'
55 55 r'(?P<suffix>%s)$' %
56 56 r'|'.join(re.escape(s) for s in _suffixes))
57 57
58 58 # RE for the ipython %run command (python + ipython scripts)
59 59 magic_run_re = re.compile(r'.*(\.ipy|\.ipynb|\.py[w]?)$')
60 60
61 61 #-----------------------------------------------------------------------------
62 62 # Local utilities
63 63 #-----------------------------------------------------------------------------
64 64
65 65 def module_list(path):
66 66 """
67 67 Return the list containing the names of the modules available in the given
68 68 folder.
69 69 """
70 70 # sys.path has the cwd as an empty string, but isdir/listdir need it as '.'
71 71 if path == '':
72 72 path = '.'
73 73
74 74 # A few local constants to be used in loops below
75 75 pjoin = os.path.join
76 76
77 77 if os.path.isdir(path):
78 78 # Build a list of all files in the directory and all files
79 79 # in its subdirectories. For performance reasons, do not
80 80 # recurse more than one level into subdirectories.
81 81 files = []
82 82 for root, dirs, nondirs in os.walk(path, followlinks=True):
83 83 subdir = root[len(path)+1:]
84 84 if subdir:
85 85 files.extend(pjoin(subdir, f) for f in nondirs)
86 86 dirs[:] = [] # Do not recurse into additional subdirectories.
87 87 else:
88 88 files.extend(nondirs)
89 89
90 90 else:
91 91 try:
92 92 files = list(zipimporter(path)._files.keys())
93 93 except:
94 94 files = []
95 95
96 96 # Build a list of modules which match the import_re regex.
97 97 modules = []
98 98 for f in files:
99 99 m = import_re.match(f)
100 100 if m:
101 101 modules.append(m.group('name'))
102 102 return list(set(modules))
103 103
104 104
105 105 def get_root_modules():
106 106 """
107 107 Returns a list containing the names of all the modules available in the
108 108 folders of the pythonpath.
109 109
110 110 ip.db['rootmodules_cache'] maps sys.path entries to list of modules.
111 111 """
112 112 ip = get_ipython()
113 if ip is None:
114 # No global shell instance to store cached list of modules.
115 # Don't try to scan for modules every time.
116 return list(sys.builtin_module_names)
117
113 118 rootmodules_cache = ip.db.get('rootmodules_cache', {})
114 119 rootmodules = list(sys.builtin_module_names)
115 120 start_time = time()
116 121 store = False
117 122 for path in sys.path:
118 123 try:
119 124 modules = rootmodules_cache[path]
120 125 except KeyError:
121 126 modules = module_list(path)
122 127 try:
123 128 modules.remove('__init__')
124 129 except ValueError:
125 130 pass
126 131 if path not in ('', '.'): # cwd modules should not be cached
127 132 rootmodules_cache[path] = modules
128 133 if time() - start_time > TIMEOUT_STORAGE and not store:
129 134 store = True
130 135 print("\nCaching the list of root modules, please wait!")
131 136 print("(This will only be done once - type '%rehashx' to "
132 137 "reset cache!)\n")
133 138 sys.stdout.flush()
134 139 if time() - start_time > TIMEOUT_GIVEUP:
135 140 print("This is taking too long, we give up.\n")
136 141 return []
137 142 rootmodules.extend(modules)
138 143 if store:
139 144 ip.db['rootmodules_cache'] = rootmodules_cache
140 145 rootmodules = list(set(rootmodules))
141 146 return rootmodules
142 147
143 148
144 149 def is_importable(module, attr, only_modules):
145 150 if only_modules:
146 151 return inspect.ismodule(getattr(module, attr))
147 152 else:
148 153 return not(attr[:2] == '__' and attr[-2:] == '__')
149 154
150 155 def try_import(mod, only_modules=False):
151 156 try:
152 157 m = import_module(mod)
153 158 except:
154 159 return []
155 160
156 161 m_is_init = hasattr(m, '__file__') and '__init__' in m.__file__
157 162
158 163 completions = []
159 164 if (not hasattr(m, '__file__')) or (not only_modules) or m_is_init:
160 165 completions.extend( [attr for attr in dir(m) if
161 166 is_importable(m, attr, only_modules)])
162 167
163 168 completions.extend(getattr(m, '__all__', []))
164 169 if m_is_init:
165 170 completions.extend(module_list(os.path.dirname(m.__file__)))
166 171 completions = {c for c in completions if isinstance(c, str)}
167 172 completions.discard('__init__')
168 173 return list(completions)
169 174
170 175
171 176 #-----------------------------------------------------------------------------
172 177 # Completion-related functions.
173 178 #-----------------------------------------------------------------------------
174 179
175 180 def quick_completer(cmd, completions):
176 181 """ Easily create a trivial completer for a command.
177 182
178 183 Takes either a list of completions, or all completions in string (that will
179 184 be split on whitespace).
180 185
181 186 Example::
182 187
183 188 [d:\ipython]|1> import ipy_completers
184 189 [d:\ipython]|2> ipy_completers.quick_completer('foo', ['bar','baz'])
185 190 [d:\ipython]|3> foo b<TAB>
186 191 bar baz
187 192 [d:\ipython]|3> foo ba
188 193 """
189 194
190 195 if isinstance(completions, str):
191 196 completions = completions.split()
192 197
193 198 def do_complete(self, event):
194 199 return completions
195 200
196 201 get_ipython().set_hook('complete_command',do_complete, str_key = cmd)
197 202
198 203 def module_completion(line):
199 204 """
200 205 Returns a list containing the completion possibilities for an import line.
201 206
202 207 The line looks like this :
203 208 'import xml.d'
204 209 'from xml.dom import'
205 210 """
206 211
207 212 words = line.split(' ')
208 213 nwords = len(words)
209 214
210 215 # from whatever <tab> -> 'import '
211 216 if nwords == 3 and words[0] == 'from':
212 217 return ['import ']
213 218
214 219 # 'from xy<tab>' or 'import xy<tab>'
215 220 if nwords < 3 and (words[0] in {'%aimport', 'import', 'from'}) :
216 221 if nwords == 1:
217 222 return get_root_modules()
218 223 mod = words[1].split('.')
219 224 if len(mod) < 2:
220 225 return get_root_modules()
221 226 completion_list = try_import('.'.join(mod[:-1]), True)
222 227 return ['.'.join(mod[:-1] + [el]) for el in completion_list]
223 228
224 229 # 'from xyz import abc<tab>'
225 230 if nwords >= 3 and words[0] == 'from':
226 231 mod = words[1]
227 232 return try_import(mod)
228 233
229 234 #-----------------------------------------------------------------------------
230 235 # Completers
231 236 #-----------------------------------------------------------------------------
232 237 # These all have the func(self, event) signature to be used as custom
233 238 # completers
234 239
235 240 def module_completer(self,event):
236 241 """Give completions after user has typed 'import ...' or 'from ...'"""
237 242
238 243 # This works in all versions of python. While 2.5 has
239 244 # pkgutil.walk_packages(), that particular routine is fairly dangerous,
240 245 # since it imports *EVERYTHING* on sys.path. That is: a) very slow b) full
241 246 # of possibly problematic side effects.
242 247 # This search the folders in the sys.path for available modules.
243 248
244 249 return module_completion(event.line)
245 250
246 251 # FIXME: there's a lot of logic common to the run, cd and builtin file
247 252 # completers, that is currently reimplemented in each.
248 253
249 254 def magic_run_completer(self, event):
250 255 """Complete files that end in .py or .ipy or .ipynb for the %run command.
251 256 """
252 257 comps = arg_split(event.line, strict=False)
253 258 # relpath should be the current token that we need to complete.
254 259 if (len(comps) > 1) and (not event.line.endswith(' ')):
255 260 relpath = comps[-1].strip("'\"")
256 261 else:
257 262 relpath = ''
258 263
259 264 #print("\nev=", event) # dbg
260 265 #print("rp=", relpath) # dbg
261 266 #print('comps=', comps) # dbg
262 267
263 268 lglob = glob.glob
264 269 isdir = os.path.isdir
265 270 relpath, tilde_expand, tilde_val = expand_user(relpath)
266 271
267 272 # Find if the user has already typed the first filename, after which we
268 273 # should complete on all files, since after the first one other files may
269 274 # be arguments to the input script.
270 275
271 276 if any(magic_run_re.match(c) for c in comps):
272 277 matches = [f.replace('\\','/') + ('/' if isdir(f) else '')
273 278 for f in lglob(relpath+'*')]
274 279 else:
275 280 dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*') if isdir(f)]
276 281 pys = [f.replace('\\','/')
277 282 for f in lglob(relpath+'*.py') + lglob(relpath+'*.ipy') +
278 283 lglob(relpath+'*.ipynb') + lglob(relpath + '*.pyw')]
279 284
280 285 matches = dirs + pys
281 286
282 287 #print('run comp:', dirs+pys) # dbg
283 288 return [compress_user(p, tilde_expand, tilde_val) for p in matches]
284 289
285 290
286 291 def cd_completer(self, event):
287 292 """Completer function for cd, which only returns directories."""
288 293 ip = get_ipython()
289 294 relpath = event.symbol
290 295
291 296 #print(event) # dbg
292 297 if event.line.endswith('-b') or ' -b ' in event.line:
293 298 # return only bookmark completions
294 299 bkms = self.db.get('bookmarks', None)
295 300 if bkms:
296 301 return bkms.keys()
297 302 else:
298 303 return []
299 304
300 305 if event.symbol == '-':
301 306 width_dh = str(len(str(len(ip.user_ns['_dh']) + 1)))
302 307 # jump in directory history by number
303 308 fmt = '-%0' + width_dh +'d [%s]'
304 309 ents = [ fmt % (i,s) for i,s in enumerate(ip.user_ns['_dh'])]
305 310 if len(ents) > 1:
306 311 return ents
307 312 return []
308 313
309 314 if event.symbol.startswith('--'):
310 315 return ["--" + os.path.basename(d) for d in ip.user_ns['_dh']]
311 316
312 317 # Expand ~ in path and normalize directory separators.
313 318 relpath, tilde_expand, tilde_val = expand_user(relpath)
314 319 relpath = relpath.replace('\\','/')
315 320
316 321 found = []
317 322 for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*')
318 323 if os.path.isdir(f)]:
319 324 if ' ' in d:
320 325 # we don't want to deal with any of that, complex code
321 326 # for this is elsewhere
322 327 raise TryNext
323 328
324 329 found.append(d)
325 330
326 331 if not found:
327 332 if os.path.isdir(relpath):
328 333 return [compress_user(relpath, tilde_expand, tilde_val)]
329 334
330 335 # if no completions so far, try bookmarks
331 336 bks = self.db.get('bookmarks',{})
332 337 bkmatches = [s for s in bks if s.startswith(event.symbol)]
333 338 if bkmatches:
334 339 return bkmatches
335 340
336 341 raise TryNext
337 342
338 343 return [compress_user(p, tilde_expand, tilde_val) for p in found]
339 344
340 345 def reset_completer(self, event):
341 346 "A completer for %reset magic"
342 347 return '-f -s in out array dhist'.split()
General Comments 0
You need to be logged in to leave comments. Login now