##// END OF EJS Templates
Tweak wording of warning message
Thomas Kluyver -
Show More
@@ -1,1236 +1,1236 b''
1 1 # encoding: utf-8
2 2 """Word completion for IPython.
3 3
4 4 This module started as fork of the rlcompleter module in the Python standard
5 5 library. The original enhancements made to rlcompleter have been sent
6 6 upstream and were accepted as of Python 2.3,
7 7
8 8 """
9 9
10 10 # Copyright (c) IPython Development Team.
11 11 # Distributed under the terms of the Modified BSD License.
12 12 #
13 13 # Some of this code originated from rlcompleter in the Python standard library
14 14 # Copyright (C) 2001 Python Software Foundation, www.python.org
15 15
16 16 from __future__ import print_function
17 17
18 18 import __main__
19 19 import glob
20 20 import inspect
21 21 import itertools
22 22 import keyword
23 23 import os
24 24 import re
25 25 import sys
26 26 import unicodedata
27 27 import string
28 28 import warnings
29 29
30 30 from traitlets.config.configurable import Configurable
31 31 from IPython.core.error import TryNext
32 32 from IPython.core.inputsplitter import ESC_MAGIC
33 33 from IPython.core.latex_symbols import latex_symbols, reverse_latex_symbol
34 34 from IPython.utils import generics
35 35 from IPython.utils.decorators import undoc
36 36 from IPython.utils.dir2 import dir2, get_real_method
37 37 from IPython.utils.process import arg_split
38 38 from IPython.utils.py3compat import builtin_mod, string_types, PY3, cast_unicode_py2
39 39 from traitlets import Bool, Enum, observe
40 40
41 41 from functools import wraps
42 42
43 43 #-----------------------------------------------------------------------------
44 44 # Globals
45 45 #-----------------------------------------------------------------------------
46 46
47 47 # Public API
48 48 __all__ = ['Completer','IPCompleter']
49 49
50 50 if sys.platform == 'win32':
51 51 PROTECTABLES = ' '
52 52 else:
53 53 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
54 54
55 55
56 56 #-----------------------------------------------------------------------------
57 57 # Work around BUG decorators.
58 58 #-----------------------------------------------------------------------------
59 59
60 60 def _strip_single_trailing_space(complete):
61 61 """
62 62 This is a workaround for a weird IPython/Prompt_toolkit behavior,
63 63 that can be removed once we rely on a slightly more recent prompt_toolkit
64 64 version (likely > 1.0.3). So this can likely be removed in IPython 6.0
65 65
66 66 cf https://github.com/ipython/ipython/issues/9658
67 67 and https://github.com/jonathanslenders/python-prompt-toolkit/pull/328
68 68
69 69 The bug is due to the fact that in PTK the completer will reinvoke itself
70 70 after trying to completer to the longuest common prefix of all the
71 71 completions, unless only one completion is available.
72 72
73 73 This logic is faulty if the completion ends with space, which can happen in
74 74 case like::
75 75
76 76 from foo import im<ta>
77 77
78 78 which only matching completion is `import `. Note the leading space at the
79 79 end. So leaving a space at the end is a reasonable request, but for now
80 80 we'll strip it.
81 81 """
82 82
83 83 @wraps(complete)
84 84 def comp(*args, **kwargs):
85 85 text, matches = complete(*args, **kwargs)
86 86 if len(matches) == 1:
87 87 return text, [matches[0].rstrip()]
88 88 return text, matches
89 89
90 90 return comp
91 91
92 92
93 93
94 94 #-----------------------------------------------------------------------------
95 95 # Main functions and classes
96 96 #-----------------------------------------------------------------------------
97 97
98 98 def has_open_quotes(s):
99 99 """Return whether a string has open quotes.
100 100
101 101 This simply counts whether the number of quote characters of either type in
102 102 the string is odd.
103 103
104 104 Returns
105 105 -------
106 106 If there is an open quote, the quote character is returned. Else, return
107 107 False.
108 108 """
109 109 # We check " first, then ', so complex cases with nested quotes will get
110 110 # the " to take precedence.
111 111 if s.count('"') % 2:
112 112 return '"'
113 113 elif s.count("'") % 2:
114 114 return "'"
115 115 else:
116 116 return False
117 117
118 118
119 119 def protect_filename(s):
120 120 """Escape a string to protect certain characters."""
121 121 if set(s) & set(PROTECTABLES):
122 122 if sys.platform == "win32":
123 123 return '"' + s + '"'
124 124 else:
125 125 return "".join(("\\" + c if c in PROTECTABLES else c) for c in s)
126 126 else:
127 127 return s
128 128
129 129
130 130 def expand_user(path):
131 131 """Expand '~'-style usernames in strings.
132 132
133 133 This is similar to :func:`os.path.expanduser`, but it computes and returns
134 134 extra information that will be useful if the input was being used in
135 135 computing completions, and you wish to return the completions with the
136 136 original '~' instead of its expanded value.
137 137
138 138 Parameters
139 139 ----------
140 140 path : str
141 141 String to be expanded. If no ~ is present, the output is the same as the
142 142 input.
143 143
144 144 Returns
145 145 -------
146 146 newpath : str
147 147 Result of ~ expansion in the input path.
148 148 tilde_expand : bool
149 149 Whether any expansion was performed or not.
150 150 tilde_val : str
151 151 The value that ~ was replaced with.
152 152 """
153 153 # Default values
154 154 tilde_expand = False
155 155 tilde_val = ''
156 156 newpath = path
157 157
158 158 if path.startswith('~'):
159 159 tilde_expand = True
160 160 rest = len(path)-1
161 161 newpath = os.path.expanduser(path)
162 162 if rest:
163 163 tilde_val = newpath[:-rest]
164 164 else:
165 165 tilde_val = newpath
166 166
167 167 return newpath, tilde_expand, tilde_val
168 168
169 169
170 170 def compress_user(path, tilde_expand, tilde_val):
171 171 """Does the opposite of expand_user, with its outputs.
172 172 """
173 173 if tilde_expand:
174 174 return path.replace(tilde_val, '~')
175 175 else:
176 176 return path
177 177
178 178
179 179 def completions_sorting_key(word):
180 180 """key for sorting completions
181 181
182 182 This does several things:
183 183
184 184 - Lowercase all completions, so they are sorted alphabetically with
185 185 upper and lower case words mingled
186 186 - Demote any completions starting with underscores to the end
187 187 - Insert any %magic and %%cellmagic completions in the alphabetical order
188 188 by their name
189 189 """
190 190 # Case insensitive sort
191 191 word = word.lower()
192 192
193 193 prio1, prio2 = 0, 0
194 194
195 195 if word.startswith('__'):
196 196 prio1 = 2
197 197 elif word.startswith('_'):
198 198 prio1 = 1
199 199
200 200 if word.endswith('='):
201 201 prio1 = -1
202 202
203 203 if word.startswith('%%'):
204 204 # If there's another % in there, this is something else, so leave it alone
205 205 if not "%" in word[2:]:
206 206 word = word[2:]
207 207 prio2 = 2
208 208 elif word.startswith('%'):
209 209 if not "%" in word[1:]:
210 210 word = word[1:]
211 211 prio2 = 1
212 212
213 213 return prio1, word, prio2
214 214
215 215
216 216 @undoc
217 217 class Bunch(object): pass
218 218
219 219
220 220 if sys.platform == 'win32':
221 221 DELIMS = ' \t\n`!@#$^&*()=+[{]}|;\'",<>?'
222 222 else:
223 223 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
224 224
225 225 GREEDY_DELIMS = ' =\r\n'
226 226
227 227
228 228 class CompletionSplitter(object):
229 229 """An object to split an input line in a manner similar to readline.
230 230
231 231 By having our own implementation, we can expose readline-like completion in
232 232 a uniform manner to all frontends. This object only needs to be given the
233 233 line of text to be split and the cursor position on said line, and it
234 234 returns the 'word' to be completed on at the cursor after splitting the
235 235 entire line.
236 236
237 237 What characters are used as splitting delimiters can be controlled by
238 238 setting the `delims` attribute (this is a property that internally
239 239 automatically builds the necessary regular expression)"""
240 240
241 241 # Private interface
242 242
243 243 # A string of delimiter characters. The default value makes sense for
244 244 # IPython's most typical usage patterns.
245 245 _delims = DELIMS
246 246
247 247 # The expression (a normal string) to be compiled into a regular expression
248 248 # for actual splitting. We store it as an attribute mostly for ease of
249 249 # debugging, since this type of code can be so tricky to debug.
250 250 _delim_expr = None
251 251
252 252 # The regular expression that does the actual splitting
253 253 _delim_re = None
254 254
255 255 def __init__(self, delims=None):
256 256 delims = CompletionSplitter._delims if delims is None else delims
257 257 self.delims = delims
258 258
259 259 @property
260 260 def delims(self):
261 261 """Return the string of delimiter characters."""
262 262 return self._delims
263 263
264 264 @delims.setter
265 265 def delims(self, delims):
266 266 """Set the delimiters for line splitting."""
267 267 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
268 268 self._delim_re = re.compile(expr)
269 269 self._delims = delims
270 270 self._delim_expr = expr
271 271
272 272 def split_line(self, line, cursor_pos=None):
273 273 """Split a line of text with a cursor at the given position.
274 274 """
275 275 l = line if cursor_pos is None else line[:cursor_pos]
276 276 return self._delim_re.split(l)[-1]
277 277
278 278
279 279 class Completer(Configurable):
280 280
281 281 greedy = Bool(False,
282 282 help="""Activate greedy completion
283 283 PENDING DEPRECTION. this is now mostly taken care of with Jedi.
284 284
285 285 This will enable completion on elements of lists, results of function calls, etc.,
286 286 but can be unsafe because the code is actually evaluated on TAB.
287 287 """
288 288 ).tag(config=True)
289 289
290 290
291 291 def __init__(self, namespace=None, global_namespace=None, **kwargs):
292 292 """Create a new completer for the command line.
293 293
294 294 Completer(namespace=ns, global_namespace=ns2) -> completer instance.
295 295
296 296 If unspecified, the default namespace where completions are performed
297 297 is __main__ (technically, __main__.__dict__). Namespaces should be
298 298 given as dictionaries.
299 299
300 300 An optional second namespace can be given. This allows the completer
301 301 to handle cases where both the local and global scopes need to be
302 302 distinguished.
303 303
304 304 Completer instances should be used as the completion mechanism of
305 305 readline via the set_completer() call:
306 306
307 307 readline.set_completer(Completer(my_namespace).complete)
308 308 """
309 309
310 310 # Don't bind to namespace quite yet, but flag whether the user wants a
311 311 # specific namespace or to use __main__.__dict__. This will allow us
312 312 # to bind to __main__.__dict__ at completion time, not now.
313 313 if namespace is None:
314 314 self.use_main_ns = 1
315 315 else:
316 316 self.use_main_ns = 0
317 317 self.namespace = namespace
318 318
319 319 # The global namespace, if given, can be bound directly
320 320 if global_namespace is None:
321 321 self.global_namespace = {}
322 322 else:
323 323 self.global_namespace = global_namespace
324 324
325 325 super(Completer, self).__init__(**kwargs)
326 326
327 327 def complete(self, text, state):
328 328 """Return the next possible completion for 'text'.
329 329
330 330 This is called successively with state == 0, 1, 2, ... until it
331 331 returns None. The completion should begin with 'text'.
332 332
333 333 """
334 334 if self.use_main_ns:
335 335 self.namespace = __main__.__dict__
336 336
337 337 if state == 0:
338 338 if "." in text:
339 339 self.matches = self.attr_matches(text)
340 340 else:
341 341 self.matches = self.global_matches(text)
342 342 try:
343 343 return self.matches[state]
344 344 except IndexError:
345 345 return None
346 346
347 347 def global_matches(self, text):
348 348 """Compute matches when text is a simple name.
349 349
350 350 Return a list of all keywords, built-in functions and names currently
351 351 defined in self.namespace or self.global_namespace that match.
352 352
353 353 """
354 354 matches = []
355 355 match_append = matches.append
356 356 n = len(text)
357 357 for lst in [keyword.kwlist,
358 358 builtin_mod.__dict__.keys(),
359 359 self.namespace.keys(),
360 360 self.global_namespace.keys()]:
361 361 for word in lst:
362 362 if word[:n] == text and word != "__builtins__":
363 363 match_append(word)
364 364 return [cast_unicode_py2(m) for m in matches]
365 365
366 366 def attr_matches(self, text):
367 367 """Compute matches when text contains a dot.
368 368
369 369 Assuming the text is of the form NAME.NAME....[NAME], and is
370 370 evaluatable in self.namespace or self.global_namespace, it will be
371 371 evaluated and its attributes (as revealed by dir()) are used as
372 372 possible completions. (For class instances, class members are are
373 373 also considered.)
374 374
375 375 WARNING: this can still invoke arbitrary C code, if an object
376 376 with a __getattr__ hook is evaluated.
377 377
378 378 """
379 379
380 380 # Another option, seems to work great. Catches things like ''.<tab>
381 381 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
382 382
383 383 if m:
384 384 expr, attr = m.group(1, 3)
385 385 elif self.greedy:
386 386 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
387 387 if not m2:
388 388 return []
389 389 expr, attr = m2.group(1,2)
390 390 else:
391 391 return []
392 392
393 393 try:
394 394 obj = eval(expr, self.namespace)
395 395 except:
396 396 try:
397 397 obj = eval(expr, self.global_namespace)
398 398 except:
399 399 return []
400 400
401 401 if self.limit_to__all__ and hasattr(obj, '__all__'):
402 402 words = get__all__entries(obj)
403 403 else:
404 404 words = dir2(obj)
405 405
406 406 try:
407 407 words = generics.complete_object(obj, words)
408 408 except TryNext:
409 409 pass
410 410 except Exception:
411 411 # Silence errors from completion function
412 412 #raise # dbg
413 413 pass
414 414 # Build match list to return
415 415 n = len(attr)
416 416 return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ]
417 417
418 418
419 419 def get__all__entries(obj):
420 420 """returns the strings in the __all__ attribute"""
421 421 try:
422 422 words = getattr(obj, '__all__')
423 423 except:
424 424 return []
425 425
426 426 return [cast_unicode_py2(w) for w in words if isinstance(w, string_types)]
427 427
428 428
429 429 def match_dict_keys(keys, prefix, delims):
430 430 """Used by dict_key_matches, matching the prefix to a list of keys"""
431 431 if not prefix:
432 432 return None, 0, [repr(k) for k in keys
433 433 if isinstance(k, (string_types, bytes))]
434 434 quote_match = re.search('["\']', prefix)
435 435 quote = quote_match.group()
436 436 try:
437 437 prefix_str = eval(prefix + quote, {})
438 438 except Exception:
439 439 return None, 0, []
440 440
441 441 pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$'
442 442 token_match = re.search(pattern, prefix, re.UNICODE)
443 443 token_start = token_match.start()
444 444 token_prefix = token_match.group()
445 445
446 446 # TODO: support bytes in Py3k
447 447 matched = []
448 448 for key in keys:
449 449 try:
450 450 if not key.startswith(prefix_str):
451 451 continue
452 452 except (AttributeError, TypeError, UnicodeError):
453 453 # Python 3+ TypeError on b'a'.startswith('a') or vice-versa
454 454 continue
455 455
456 456 # reformat remainder of key to begin with prefix
457 457 rem = key[len(prefix_str):]
458 458 # force repr wrapped in '
459 459 rem_repr = repr(rem + '"')
460 460 if rem_repr.startswith('u') and prefix[0] not in 'uU':
461 461 # Found key is unicode, but prefix is Py2 string.
462 462 # Therefore attempt to interpret key as string.
463 463 try:
464 464 rem_repr = repr(rem.encode('ascii') + '"')
465 465 except UnicodeEncodeError:
466 466 continue
467 467
468 468 rem_repr = rem_repr[1 + rem_repr.index("'"):-2]
469 469 if quote == '"':
470 470 # The entered prefix is quoted with ",
471 471 # but the match is quoted with '.
472 472 # A contained " hence needs escaping for comparison:
473 473 rem_repr = rem_repr.replace('"', '\\"')
474 474
475 475 # then reinsert prefix from start of token
476 476 matched.append('%s%s' % (token_prefix, rem_repr))
477 477 return quote, token_start, matched
478 478
479 479
480 480 def _safe_isinstance(obj, module, class_name):
481 481 """Checks if obj is an instance of module.class_name if loaded
482 482 """
483 483 return (module in sys.modules and
484 484 isinstance(obj, getattr(__import__(module), class_name)))
485 485
486 486
487 487 def back_unicode_name_matches(text):
488 488 u"""Match unicode characters back to unicode name
489 489
490 490 This does β˜ƒ -> \\snowman
491 491
492 492 Note that snowman is not a valid python3 combining character but will be expanded.
493 493 Though it will not recombine back to the snowman character by the completion machinery.
494 494
495 495 This will not either back-complete standard sequences like \\n, \\b ...
496 496
497 497 Used on Python 3 only.
498 498 """
499 499 if len(text)<2:
500 500 return u'', ()
501 501 maybe_slash = text[-2]
502 502 if maybe_slash != '\\':
503 503 return u'', ()
504 504
505 505 char = text[-1]
506 506 # no expand on quote for completion in strings.
507 507 # nor backcomplete standard ascii keys
508 508 if char in string.ascii_letters or char in ['"',"'"]:
509 509 return u'', ()
510 510 try :
511 511 unic = unicodedata.name(char)
512 512 return '\\'+char,['\\'+unic]
513 513 except KeyError:
514 514 pass
515 515 return u'', ()
516 516
517 517 def back_latex_name_matches(text):
518 518 u"""Match latex characters back to unicode name
519 519
520 520 This does ->\\sqrt
521 521
522 522 Used on Python 3 only.
523 523 """
524 524 if len(text)<2:
525 525 return u'', ()
526 526 maybe_slash = text[-2]
527 527 if maybe_slash != '\\':
528 528 return u'', ()
529 529
530 530
531 531 char = text[-1]
532 532 # no expand on quote for completion in strings.
533 533 # nor backcomplete standard ascii keys
534 534 if char in string.ascii_letters or char in ['"',"'"]:
535 535 return u'', ()
536 536 try :
537 537 latex = reverse_latex_symbol[char]
538 538 # '\\' replace the \ as well
539 539 return '\\'+char,[latex]
540 540 except KeyError:
541 541 pass
542 542 return u'', ()
543 543
544 544
545 545 class IPCompleter(Completer):
546 546 """Extension of the completer class with IPython-specific features"""
547 547
548 548 @observe('greedy')
549 549 def _greedy_changed(self, change):
550 550 """update the splitter and readline delims when greedy is changed"""
551 551 if change['new']:
552 552 self.splitter.delims = GREEDY_DELIMS
553 553 else:
554 554 self.splitter.delims = DELIMS
555 555
556 556 merge_completions = Bool(True,
557 557 help="""Whether to merge completion results into a single list
558 558
559 559 If False, only the completion results from the first non-empty
560 560 completer will be returned.
561 561 """
562 562 ).tag(config=True)
563 563 omit__names = Enum((0,1,2), default_value=2,
564 564 help="""Instruct the completer to omit private method names
565 565
566 566 Specifically, when completing on ``object.<tab>``.
567 567
568 568 When 2 [default]: all names that start with '_' will be excluded.
569 569
570 570 When 1: all 'magic' names (``__foo__``) will be excluded.
571 571
572 572 When 0: nothing will be excluded.
573 573 """
574 574 ).tag(config=True)
575 575 limit_to__all__ = Bool(False,
576 576 help="""
577 577 DEPRECATED as of version 5.0.
578 578
579 579 Instruct the completer to use __all__ for the completion
580 580
581 581 Specifically, when completing on ``object.<tab>``.
582 582
583 583 When True: only those names in obj.__all__ will be included.
584 584
585 585 When False [default]: the __all__ attribute is ignored
586 586 """,
587 587 ).tag(config=True)
588 588
589 589 def __init__(self, shell=None, namespace=None, global_namespace=None,
590 590 use_readline=False, config=None, **kwargs):
591 591 """IPCompleter() -> completer
592 592
593 593 Return a completer object suitable for use by the readline library
594 594 via readline.set_completer().
595 595
596 596 Inputs:
597 597
598 598 - shell: a pointer to the ipython shell itself. This is needed
599 599 because this completer knows about magic functions, and those can
600 600 only be accessed via the ipython instance.
601 601
602 602 - namespace: an optional dict where completions are performed.
603 603
604 604 - global_namespace: secondary optional dict for completions, to
605 605 handle cases (such as IPython embedded inside functions) where
606 606 both Python scopes are visible.
607 607
608 608 use_readline : bool, optional
609 609 DEPRECATED, ignored.
610 610 """
611 611
612 612 self.magic_escape = ESC_MAGIC
613 613 self.splitter = CompletionSplitter()
614 614
615 615 if use_readline:
616 warnings.warn('The use_readline parameter is deprecated since IPython 6.0.',
616 warnings.warn('The use_readline parameter is deprecated and ignored since IPython 6.0.',
617 617 DeprecationWarning, stacklevel=2)
618 618
619 619 # _greedy_changed() depends on splitter and readline being defined:
620 620 Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
621 621 config=config, **kwargs)
622 622
623 623 # List where completion matches will be stored
624 624 self.matches = []
625 625 self.shell = shell
626 626 # Regexp to split filenames with spaces in them
627 627 self.space_name_re = re.compile(r'([^\\] )')
628 628 # Hold a local ref. to glob.glob for speed
629 629 self.glob = glob.glob
630 630
631 631 # Determine if we are running on 'dumb' terminals, like (X)Emacs
632 632 # buffers, to avoid completion problems.
633 633 term = os.environ.get('TERM','xterm')
634 634 self.dumb_terminal = term in ['dumb','emacs']
635 635
636 636 # Special handling of backslashes needed in win32 platforms
637 637 if sys.platform == "win32":
638 638 self.clean_glob = self._clean_glob_win32
639 639 else:
640 640 self.clean_glob = self._clean_glob
641 641
642 642 #regexp to parse docstring for function signature
643 643 self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
644 644 self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
645 645 #use this if positional argument name is also needed
646 646 #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)')
647 647
648 648 # All active matcher routines for completion
649 649 self.matchers = [
650 650 self.python_matches,
651 651 self.file_matches,
652 652 self.magic_matches,
653 653 self.python_func_kw_matches,
654 654 self.dict_key_matches,
655 655 ]
656 656
657 657 # This is set externally by InteractiveShell
658 658 self.custom_completers = None
659 659
660 660 def all_completions(self, text):
661 661 """
662 662 Wrapper around the complete method for the benefit of emacs.
663 663 """
664 664 return self.complete(text)[1]
665 665
666 666 def _clean_glob(self, text):
667 667 return self.glob("%s*" % text)
668 668
669 669 def _clean_glob_win32(self,text):
670 670 return [f.replace("\\","/")
671 671 for f in self.glob("%s*" % text)]
672 672
673 673 def file_matches(self, text):
674 674 """Match filenames, expanding ~USER type strings.
675 675
676 676 Most of the seemingly convoluted logic in this completer is an
677 677 attempt to handle filenames with spaces in them. And yet it's not
678 678 quite perfect, because Python's readline doesn't expose all of the
679 679 GNU readline details needed for this to be done correctly.
680 680
681 681 For a filename with a space in it, the printed completions will be
682 682 only the parts after what's already been typed (instead of the
683 683 full completions, as is normally done). I don't think with the
684 684 current (as of Python 2.3) Python readline it's possible to do
685 685 better."""
686 686
687 687 # chars that require escaping with backslash - i.e. chars
688 688 # that readline treats incorrectly as delimiters, but we
689 689 # don't want to treat as delimiters in filename matching
690 690 # when escaped with backslash
691 691 if text.startswith('!'):
692 692 text = text[1:]
693 693 text_prefix = u'!'
694 694 else:
695 695 text_prefix = u''
696 696
697 697 text_until_cursor = self.text_until_cursor
698 698 # track strings with open quotes
699 699 open_quotes = has_open_quotes(text_until_cursor)
700 700
701 701 if '(' in text_until_cursor or '[' in text_until_cursor:
702 702 lsplit = text
703 703 else:
704 704 try:
705 705 # arg_split ~ shlex.split, but with unicode bugs fixed by us
706 706 lsplit = arg_split(text_until_cursor)[-1]
707 707 except ValueError:
708 708 # typically an unmatched ", or backslash without escaped char.
709 709 if open_quotes:
710 710 lsplit = text_until_cursor.split(open_quotes)[-1]
711 711 else:
712 712 return []
713 713 except IndexError:
714 714 # tab pressed on empty line
715 715 lsplit = ""
716 716
717 717 if not open_quotes and lsplit != protect_filename(lsplit):
718 718 # if protectables are found, do matching on the whole escaped name
719 719 has_protectables = True
720 720 text0,text = text,lsplit
721 721 else:
722 722 has_protectables = False
723 723 text = os.path.expanduser(text)
724 724
725 725 if text == "":
726 726 return [text_prefix + cast_unicode_py2(protect_filename(f)) for f in self.glob("*")]
727 727
728 728 # Compute the matches from the filesystem
729 729 if sys.platform == 'win32':
730 730 m0 = self.clean_glob(text)
731 731 else:
732 732 m0 = self.clean_glob(text.replace('\\', ''))
733 733
734 734 if has_protectables:
735 735 # If we had protectables, we need to revert our changes to the
736 736 # beginning of filename so that we don't double-write the part
737 737 # of the filename we have so far
738 738 len_lsplit = len(lsplit)
739 739 matches = [text_prefix + text0 +
740 740 protect_filename(f[len_lsplit:]) for f in m0]
741 741 else:
742 742 if open_quotes:
743 743 # if we have a string with an open quote, we don't need to
744 744 # protect the names at all (and we _shouldn't_, as it
745 745 # would cause bugs when the filesystem call is made).
746 746 matches = m0
747 747 else:
748 748 matches = [text_prefix +
749 749 protect_filename(f) for f in m0]
750 750
751 751 # Mark directories in input list by appending '/' to their names.
752 752 return [cast_unicode_py2(x+'/') if os.path.isdir(x) else x for x in matches]
753 753
754 754 def magic_matches(self, text):
755 755 """Match magics"""
756 756 # Get all shell magics now rather than statically, so magics loaded at
757 757 # runtime show up too.
758 758 lsm = self.shell.magics_manager.lsmagic()
759 759 line_magics = lsm['line']
760 760 cell_magics = lsm['cell']
761 761 pre = self.magic_escape
762 762 pre2 = pre+pre
763 763
764 764 # Completion logic:
765 765 # - user gives %%: only do cell magics
766 766 # - user gives %: do both line and cell magics
767 767 # - no prefix: do both
768 768 # In other words, line magics are skipped if the user gives %% explicitly
769 769 bare_text = text.lstrip(pre)
770 770 comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)]
771 771 if not text.startswith(pre2):
772 772 comp += [ pre+m for m in line_magics if m.startswith(bare_text)]
773 773 return [cast_unicode_py2(c) for c in comp]
774 774
775 775
776 776 def python_matches(self, text):
777 777 """Match attributes or global python names"""
778 778 if "." in text:
779 779 try:
780 780 matches = self.attr_matches(text)
781 781 if text.endswith('.') and self.omit__names:
782 782 if self.omit__names == 1:
783 783 # true if txt is _not_ a __ name, false otherwise:
784 784 no__name = (lambda txt:
785 785 re.match(r'.*\.__.*?__',txt) is None)
786 786 else:
787 787 # true if txt is _not_ a _ name, false otherwise:
788 788 no__name = (lambda txt:
789 789 re.match(r'\._.*?',txt[txt.rindex('.'):]) is None)
790 790 matches = filter(no__name, matches)
791 791 except NameError:
792 792 # catches <undefined attributes>.<tab>
793 793 matches = []
794 794 else:
795 795 matches = self.global_matches(text)
796 796 return matches
797 797
798 798 def _default_arguments_from_docstring(self, doc):
799 799 """Parse the first line of docstring for call signature.
800 800
801 801 Docstring should be of the form 'min(iterable[, key=func])\n'.
802 802 It can also parse cython docstring of the form
803 803 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'.
804 804 """
805 805 if doc is None:
806 806 return []
807 807
808 808 #care only the firstline
809 809 line = doc.lstrip().splitlines()[0]
810 810
811 811 #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
812 812 #'min(iterable[, key=func])\n' -> 'iterable[, key=func]'
813 813 sig = self.docstring_sig_re.search(line)
814 814 if sig is None:
815 815 return []
816 816 # iterable[, key=func]' -> ['iterable[' ,' key=func]']
817 817 sig = sig.groups()[0].split(',')
818 818 ret = []
819 819 for s in sig:
820 820 #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
821 821 ret += self.docstring_kwd_re.findall(s)
822 822 return ret
823 823
824 824 def _default_arguments(self, obj):
825 825 """Return the list of default arguments of obj if it is callable,
826 826 or empty list otherwise."""
827 827 call_obj = obj
828 828 ret = []
829 829 if inspect.isbuiltin(obj):
830 830 pass
831 831 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
832 832 if inspect.isclass(obj):
833 833 #for cython embededsignature=True the constructor docstring
834 834 #belongs to the object itself not __init__
835 835 ret += self._default_arguments_from_docstring(
836 836 getattr(obj, '__doc__', ''))
837 837 # for classes, check for __init__,__new__
838 838 call_obj = (getattr(obj, '__init__', None) or
839 839 getattr(obj, '__new__', None))
840 840 # for all others, check if they are __call__able
841 841 elif hasattr(obj, '__call__'):
842 842 call_obj = obj.__call__
843 843 ret += self._default_arguments_from_docstring(
844 844 getattr(call_obj, '__doc__', ''))
845 845
846 846 if PY3:
847 847 _keeps = (inspect.Parameter.KEYWORD_ONLY,
848 848 inspect.Parameter.POSITIONAL_OR_KEYWORD)
849 849 signature = inspect.signature
850 850 else:
851 851 import IPython.utils.signatures
852 852 _keeps = (IPython.utils.signatures.Parameter.KEYWORD_ONLY,
853 853 IPython.utils.signatures.Parameter.POSITIONAL_OR_KEYWORD)
854 854 signature = IPython.utils.signatures.signature
855 855
856 856 try:
857 857 sig = signature(call_obj)
858 858 ret.extend(k for k, v in sig.parameters.items() if
859 859 v.kind in _keeps)
860 860 except ValueError:
861 861 pass
862 862
863 863 return list(set(ret))
864 864
865 865 def python_func_kw_matches(self,text):
866 866 """Match named parameters (kwargs) of the last open function"""
867 867
868 868 if "." in text: # a parameter cannot be dotted
869 869 return []
870 870 try: regexp = self.__funcParamsRegex
871 871 except AttributeError:
872 872 regexp = self.__funcParamsRegex = re.compile(r'''
873 873 '.*?(?<!\\)' | # single quoted strings or
874 874 ".*?(?<!\\)" | # double quoted strings or
875 875 \w+ | # identifier
876 876 \S # other characters
877 877 ''', re.VERBOSE | re.DOTALL)
878 878 # 1. find the nearest identifier that comes before an unclosed
879 879 # parenthesis before the cursor
880 880 # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
881 881 tokens = regexp.findall(self.text_until_cursor)
882 882 iterTokens = reversed(tokens); openPar = 0
883 883
884 884 for token in iterTokens:
885 885 if token == ')':
886 886 openPar -= 1
887 887 elif token == '(':
888 888 openPar += 1
889 889 if openPar > 0:
890 890 # found the last unclosed parenthesis
891 891 break
892 892 else:
893 893 return []
894 894 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
895 895 ids = []
896 896 isId = re.compile(r'\w+$').match
897 897
898 898 while True:
899 899 try:
900 900 ids.append(next(iterTokens))
901 901 if not isId(ids[-1]):
902 902 ids.pop(); break
903 903 if not next(iterTokens) == '.':
904 904 break
905 905 except StopIteration:
906 906 break
907 907
908 908 # Find all named arguments already assigned to, as to avoid suggesting
909 909 # them again
910 910 usedNamedArgs = set()
911 911 par_level = -1
912 912 for token, next_token in zip(tokens, tokens[1:]):
913 913 if token == '(':
914 914 par_level += 1
915 915 elif token == ')':
916 916 par_level -= 1
917 917
918 918 if par_level != 0:
919 919 continue
920 920
921 921 if next_token != '=':
922 922 continue
923 923
924 924 usedNamedArgs.add(token)
925 925
926 926 # lookup the candidate callable matches either using global_matches
927 927 # or attr_matches for dotted names
928 928 if len(ids) == 1:
929 929 callableMatches = self.global_matches(ids[0])
930 930 else:
931 931 callableMatches = self.attr_matches('.'.join(ids[::-1]))
932 932 argMatches = []
933 933 for callableMatch in callableMatches:
934 934 try:
935 935 namedArgs = self._default_arguments(eval(callableMatch,
936 936 self.namespace))
937 937 except:
938 938 continue
939 939
940 940 # Remove used named arguments from the list, no need to show twice
941 941 for namedArg in set(namedArgs) - usedNamedArgs:
942 942 if namedArg.startswith(text):
943 943 argMatches.append(u"%s=" %namedArg)
944 944 return argMatches
945 945
946 946 def dict_key_matches(self, text):
947 947 "Match string keys in a dictionary, after e.g. 'foo[' "
948 948 def get_keys(obj):
949 949 # Objects can define their own completions by defining an
950 950 # _ipy_key_completions_() method.
951 951 method = get_real_method(obj, '_ipython_key_completions_')
952 952 if method is not None:
953 953 return method()
954 954
955 955 # Special case some common in-memory dict-like types
956 956 if isinstance(obj, dict) or\
957 957 _safe_isinstance(obj, 'pandas', 'DataFrame'):
958 958 try:
959 959 return list(obj.keys())
960 960 except Exception:
961 961 return []
962 962 elif _safe_isinstance(obj, 'numpy', 'ndarray') or\
963 963 _safe_isinstance(obj, 'numpy', 'void'):
964 964 return obj.dtype.names or []
965 965 return []
966 966
967 967 try:
968 968 regexps = self.__dict_key_regexps
969 969 except AttributeError:
970 970 dict_key_re_fmt = r'''(?x)
971 971 ( # match dict-referring expression wrt greedy setting
972 972 %s
973 973 )
974 974 \[ # open bracket
975 975 \s* # and optional whitespace
976 976 ([uUbB]? # string prefix (r not handled)
977 977 (?: # unclosed string
978 978 '(?:[^']|(?<!\\)\\')*
979 979 |
980 980 "(?:[^"]|(?<!\\)\\")*
981 981 )
982 982 )?
983 983 $
984 984 '''
985 985 regexps = self.__dict_key_regexps = {
986 986 False: re.compile(dict_key_re_fmt % '''
987 987 # identifiers separated by .
988 988 (?!\d)\w+
989 989 (?:\.(?!\d)\w+)*
990 990 '''),
991 991 True: re.compile(dict_key_re_fmt % '''
992 992 .+
993 993 ''')
994 994 }
995 995
996 996 match = regexps[self.greedy].search(self.text_until_cursor)
997 997 if match is None:
998 998 return []
999 999
1000 1000 expr, prefix = match.groups()
1001 1001 try:
1002 1002 obj = eval(expr, self.namespace)
1003 1003 except Exception:
1004 1004 try:
1005 1005 obj = eval(expr, self.global_namespace)
1006 1006 except Exception:
1007 1007 return []
1008 1008
1009 1009 keys = get_keys(obj)
1010 1010 if not keys:
1011 1011 return keys
1012 1012 closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims)
1013 1013 if not matches:
1014 1014 return matches
1015 1015
1016 1016 # get the cursor position of
1017 1017 # - the text being completed
1018 1018 # - the start of the key text
1019 1019 # - the start of the completion
1020 1020 text_start = len(self.text_until_cursor) - len(text)
1021 1021 if prefix:
1022 1022 key_start = match.start(2)
1023 1023 completion_start = key_start + token_offset
1024 1024 else:
1025 1025 key_start = completion_start = match.end()
1026 1026
1027 1027 # grab the leading prefix, to make sure all completions start with `text`
1028 1028 if text_start > key_start:
1029 1029 leading = ''
1030 1030 else:
1031 1031 leading = text[text_start:completion_start]
1032 1032
1033 1033 # the index of the `[` character
1034 1034 bracket_idx = match.end(1)
1035 1035
1036 1036 # append closing quote and bracket as appropriate
1037 1037 # this is *not* appropriate if the opening quote or bracket is outside
1038 1038 # the text given to this method
1039 1039 suf = ''
1040 1040 continuation = self.line_buffer[len(self.text_until_cursor):]
1041 1041 if key_start > text_start and closing_quote:
1042 1042 # quotes were opened inside text, maybe close them
1043 1043 if continuation.startswith(closing_quote):
1044 1044 continuation = continuation[len(closing_quote):]
1045 1045 else:
1046 1046 suf += closing_quote
1047 1047 if bracket_idx > text_start:
1048 1048 # brackets were opened inside text, maybe close them
1049 1049 if not continuation.startswith(']'):
1050 1050 suf += ']'
1051 1051
1052 1052 return [leading + k + suf for k in matches]
1053 1053
1054 1054 def unicode_name_matches(self, text):
1055 1055 u"""Match Latex-like syntax for unicode characters base
1056 1056 on the name of the character.
1057 1057
1058 1058 This does \\GREEK SMALL LETTER ETA -> Ξ·
1059 1059
1060 1060 Works only on valid python 3 identifier, or on combining characters that
1061 1061 will combine to form a valid identifier.
1062 1062
1063 1063 Used on Python 3 only.
1064 1064 """
1065 1065 slashpos = text.rfind('\\')
1066 1066 if slashpos > -1:
1067 1067 s = text[slashpos+1:]
1068 1068 try :
1069 1069 unic = unicodedata.lookup(s)
1070 1070 # allow combining chars
1071 1071 if ('a'+unic).isidentifier():
1072 1072 return '\\'+s,[unic]
1073 1073 except KeyError:
1074 1074 pass
1075 1075 return u'', []
1076 1076
1077 1077
1078 1078
1079 1079
1080 1080 def latex_matches(self, text):
1081 1081 u"""Match Latex syntax for unicode characters.
1082 1082
1083 1083 This does both \\alp -> \\alpha and \\alpha -> Ξ±
1084 1084
1085 1085 Used on Python 3 only.
1086 1086 """
1087 1087 slashpos = text.rfind('\\')
1088 1088 if slashpos > -1:
1089 1089 s = text[slashpos:]
1090 1090 if s in latex_symbols:
1091 1091 # Try to complete a full latex symbol to unicode
1092 1092 # \\alpha -> Ξ±
1093 1093 return s, [latex_symbols[s]]
1094 1094 else:
1095 1095 # If a user has partially typed a latex symbol, give them
1096 1096 # a full list of options \al -> [\aleph, \alpha]
1097 1097 matches = [k for k in latex_symbols if k.startswith(s)]
1098 1098 return s, matches
1099 1099 return u'', []
1100 1100
1101 1101 def dispatch_custom_completer(self, text):
1102 1102 if not self.custom_completers:
1103 1103 return
1104 1104
1105 1105 line = self.line_buffer
1106 1106 if not line.strip():
1107 1107 return None
1108 1108
1109 1109 # Create a little structure to pass all the relevant information about
1110 1110 # the current completion to any custom completer.
1111 1111 event = Bunch()
1112 1112 event.line = line
1113 1113 event.symbol = text
1114 1114 cmd = line.split(None,1)[0]
1115 1115 event.command = cmd
1116 1116 event.text_until_cursor = self.text_until_cursor
1117 1117
1118 1118 # for foo etc, try also to find completer for %foo
1119 1119 if not cmd.startswith(self.magic_escape):
1120 1120 try_magic = self.custom_completers.s_matches(
1121 1121 self.magic_escape + cmd)
1122 1122 else:
1123 1123 try_magic = []
1124 1124
1125 1125 for c in itertools.chain(self.custom_completers.s_matches(cmd),
1126 1126 try_magic,
1127 1127 self.custom_completers.flat_matches(self.text_until_cursor)):
1128 1128 try:
1129 1129 res = c(event)
1130 1130 if res:
1131 1131 # first, try case sensitive match
1132 1132 withcase = [cast_unicode_py2(r) for r in res if r.startswith(text)]
1133 1133 if withcase:
1134 1134 return withcase
1135 1135 # if none, then case insensitive ones are ok too
1136 1136 text_low = text.lower()
1137 1137 return [cast_unicode_py2(r) for r in res if r.lower().startswith(text_low)]
1138 1138 except TryNext:
1139 1139 pass
1140 1140
1141 1141 return None
1142 1142
1143 1143 @_strip_single_trailing_space
1144 1144 def complete(self, text=None, line_buffer=None, cursor_pos=None):
1145 1145 """Find completions for the given text and line context.
1146 1146
1147 1147 Note that both the text and the line_buffer are optional, but at least
1148 1148 one of them must be given.
1149 1149
1150 1150 Parameters
1151 1151 ----------
1152 1152 text : string, optional
1153 1153 Text to perform the completion on. If not given, the line buffer
1154 1154 is split using the instance's CompletionSplitter object.
1155 1155
1156 1156 line_buffer : string, optional
1157 1157 If not given, the completer attempts to obtain the current line
1158 1158 buffer via readline. This keyword allows clients which are
1159 1159 requesting for text completions in non-readline contexts to inform
1160 1160 the completer of the entire text.
1161 1161
1162 1162 cursor_pos : int, optional
1163 1163 Index of the cursor in the full line buffer. Should be provided by
1164 1164 remote frontends where kernel has no access to frontend state.
1165 1165
1166 1166 Returns
1167 1167 -------
1168 1168 text : str
1169 1169 Text that was actually used in the completion.
1170 1170
1171 1171 matches : list
1172 1172 A list of completion matches.
1173 1173 """
1174 1174 # if the cursor position isn't given, the only sane assumption we can
1175 1175 # make is that it's at the end of the line (the common case)
1176 1176 if cursor_pos is None:
1177 1177 cursor_pos = len(line_buffer) if text is None else len(text)
1178 1178
1179 1179 if self.use_main_ns:
1180 1180 self.namespace = __main__.__dict__
1181 1181
1182 1182 if PY3:
1183 1183
1184 1184 base_text = text if not line_buffer else line_buffer[:cursor_pos]
1185 1185 latex_text, latex_matches = self.latex_matches(base_text)
1186 1186 if latex_matches:
1187 1187 return latex_text, latex_matches
1188 1188 name_text = ''
1189 1189 name_matches = []
1190 1190 for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches):
1191 1191 name_text, name_matches = meth(base_text)
1192 1192 if name_text:
1193 1193 return name_text, name_matches
1194 1194
1195 1195 # if text is either None or an empty string, rely on the line buffer
1196 1196 if not text:
1197 1197 text = self.splitter.split_line(line_buffer, cursor_pos)
1198 1198
1199 1199 # If no line buffer is given, assume the input text is all there was
1200 1200 if line_buffer is None:
1201 1201 line_buffer = text
1202 1202
1203 1203 self.line_buffer = line_buffer
1204 1204 self.text_until_cursor = self.line_buffer[:cursor_pos]
1205 1205
1206 1206 # Start with a clean slate of completions
1207 1207 self.matches[:] = []
1208 1208 custom_res = self.dispatch_custom_completer(text)
1209 1209 if custom_res is not None:
1210 1210 # did custom completers produce something?
1211 1211 self.matches = custom_res
1212 1212 else:
1213 1213 # Extend the list of completions with the results of each
1214 1214 # matcher, so we return results to the user from all
1215 1215 # namespaces.
1216 1216 if self.merge_completions:
1217 1217 self.matches = []
1218 1218 for matcher in self.matchers:
1219 1219 try:
1220 1220 self.matches.extend(matcher(text))
1221 1221 except:
1222 1222 # Show the ugly traceback if the matcher causes an
1223 1223 # exception, but do NOT crash the kernel!
1224 1224 sys.excepthook(*sys.exc_info())
1225 1225 else:
1226 1226 for matcher in self.matchers:
1227 1227 self.matches = matcher(text)
1228 1228 if self.matches:
1229 1229 break
1230 1230 # FIXME: we should extend our api to return a dict with completions for
1231 1231 # different types of objects. The rlcomplete() method could then
1232 1232 # simply collapse the dict into a list for readline, but we'd have
1233 1233 # richer completion semantics in other evironments.
1234 1234 self.matches = sorted(set(self.matches), key=completions_sorting_key)
1235 1235
1236 1236 return text, self.matches
General Comments 0
You need to be logged in to leave comments. Login now