##// END OF EJS Templates
added __all__ to completer.py and added basic tests for test_dir2...
Tim Couper -
Show More
@@ -336,7 +336,6 b' class Completer(Configurable):'
336 #io.rprint('Completer->attr_matches, txt=%r' % text) # dbg
336 #io.rprint('Completer->attr_matches, txt=%r' % text) # dbg
337 # Another option, seems to work great. Catches things like ''.<tab>
337 # Another option, seems to work great. Catches things like ''.<tab>
338 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
338 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
339
340 if m:
339 if m:
341 expr, attr = m.group(1, 3)
340 expr, attr = m.group(1, 3)
342 elif self.greedy:
341 elif self.greedy:
@@ -355,6 +354,9 b' class Completer(Configurable):'
355 except:
354 except:
356 return []
355 return []
357
356
357 if self.limit_to__all__ and hasattr(obj, '__all__'):
358 words = get__all__entries(obj)
359 else:
358 words = dir2(obj)
360 words = dir2(obj)
359
361
360 try:
362 try:
@@ -371,6 +373,16 b' class Completer(Configurable):'
371 return res
373 return res
372
374
373
375
376 def get__all__entries(obj):
377 """returns the strings in the __all__ attribute"""
378 try:
379 words = getattr(obj,'__all__')
380 except:
381 return []
382
383 return [w for w in words if isinstance(w, basestring)]
384
385
374 class IPCompleter(Completer):
386 class IPCompleter(Completer):
375 """Extension of the completer class with IPython-specific features"""
387 """Extension of the completer class with IPython-specific features"""
376
388
@@ -403,6 +415,16 b' class IPCompleter(Completer):'
403 When 0: nothing will be excluded.
415 When 0: nothing will be excluded.
404 """
416 """
405 )
417 )
418 limit_to__all__ = Enum((0,1), default_value=1, config=True,
419 help="""Instruct the completer to use __all__ for the completion
420
421 Specifically, when completing on ``object.<tab>``.
422
423 When 1: only those names in obj.__all__ will be included.
424
425 When 0 [default]: the values in the __all__ attribute are ignored
426 """
427 )
406
428
407 def __init__(self, shell=None, namespace=None, global_namespace=None,
429 def __init__(self, shell=None, namespace=None, global_namespace=None,
408 alias_table=None, use_readline=True,
430 alias_table=None, use_readline=True,
@@ -18,6 +18,7 b' from IPython.core import completer'
18 from IPython.external.decorators import knownfailureif
18 from IPython.external.decorators import knownfailureif
19 from IPython.utils.tempdir import TemporaryDirectory
19 from IPython.utils.tempdir import TemporaryDirectory
20 from IPython.utils.generics import complete_object
20 from IPython.utils.generics import complete_object
21 from IPython.testing.globalipapp import get_ipython
21
22
22 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
23 # Test functions
24 # Test functions
@@ -230,3 +231,14 b' def test_omit__names():'
230 nt.assert_false('ip._hidden_attr' in matches)
231 nt.assert_false('ip._hidden_attr' in matches)
231 del ip._hidden_attr
232 del ip._hidden_attr
232
233
234 def test_get__all__entries_ok():
235 class A(object):
236 __all__ = ['x', 1]
237 words = completer.get__all__entries(A())
238 nt.assert_equal(words, ['x'])
239
240 def test_get__all__entries_no__all__ok():
241 class A(object):
242 pass
243 words = completer.get__all__entries(A())
244 nt.assert_equal(words, []) No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now