##// 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:
@@ -346,7 +345,7 b' class Completer(Configurable):'
346 expr, attr = m2.group(1,2)
345 expr, attr = m2.group(1,2)
347 else:
346 else:
348 return []
347 return []
349
348
350 try:
349 try:
351 obj = eval(expr, self.namespace)
350 obj = eval(expr, self.namespace)
352 except:
351 except:
@@ -355,7 +354,10 b' class Completer(Configurable):'
355 except:
354 except:
356 return []
355 return []
357
356
358 words = dir2(obj)
357 if self.limit_to__all__ and hasattr(obj, '__all__'):
358 words = get__all__entries(obj)
359 else:
360 words = dir2(obj)
359
361
360 try:
362 try:
361 words = generics.complete_object(obj, words)
363 words = generics.complete_object(obj, words)
@@ -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,
@@ -602,7 +624,7 b' class IPCompleter(Completer):'
602
624
603 def python_matches(self,text):
625 def python_matches(self,text):
604 """Match attributes or global python names"""
626 """Match attributes or global python names"""
605
627
606 #io.rprint('Completer->python_matches, txt=%r' % text) # dbg
628 #io.rprint('Completer->python_matches, txt=%r' % text) # dbg
607 if "." in text:
629 if "." in text:
608 try:
630 try:
@@ -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
@@ -229,4 +230,15 b' def test_omit__names():'
229 nt.assert_false('ip.__str__' in matches)
230 nt.assert_false('ip.__str__' in matches)
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 No newline at end of file
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