From cf95840ed4258aa5b51b860acac3d52069f411da 2012-03-27 19:55:58 From: Tim Couper Date: 2012-03-27 19:55:58 Subject: [PATCH] added __all__ to completer.py and added basic tests for test_dir2 Signed-off-by: Tim Couper --- diff --git a/IPython/core/completer.py b/IPython/core/completer.py index ae96e72..f2428e8 100644 --- a/IPython/core/completer.py +++ b/IPython/core/completer.py @@ -336,7 +336,6 @@ class Completer(Configurable): #io.rprint('Completer->attr_matches, txt=%r' % text) # dbg # Another option, seems to work great. Catches things like ''. m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) - if m: expr, attr = m.group(1, 3) elif self.greedy: @@ -346,7 +345,7 @@ class Completer(Configurable): expr, attr = m2.group(1,2) else: return [] - + try: obj = eval(expr, self.namespace) except: @@ -355,7 +354,10 @@ class Completer(Configurable): except: return [] - words = dir2(obj) + if self.limit_to__all__ and hasattr(obj, '__all__'): + words = get__all__entries(obj) + else: + words = dir2(obj) try: words = generics.complete_object(obj, words) @@ -371,6 +373,16 @@ class Completer(Configurable): return res +def get__all__entries(obj): + """returns the strings in the __all__ attribute""" + try: + words = getattr(obj,'__all__') + except: + return [] + + return [w for w in words if isinstance(w, basestring)] + + class IPCompleter(Completer): """Extension of the completer class with IPython-specific features""" @@ -403,6 +415,16 @@ class IPCompleter(Completer): When 0: nothing will be excluded. """ ) + limit_to__all__ = Enum((0,1), default_value=1, config=True, + help="""Instruct the completer to use __all__ for the completion + + Specifically, when completing on ``object.``. + + When 1: only those names in obj.__all__ will be included. + + When 0 [default]: the values in the __all__ attribute are ignored + """ + ) def __init__(self, shell=None, namespace=None, global_namespace=None, alias_table=None, use_readline=True, @@ -602,7 +624,7 @@ class IPCompleter(Completer): def python_matches(self,text): """Match attributes or global python names""" - + #io.rprint('Completer->python_matches, txt=%r' % text) # dbg if "." in text: try: diff --git a/IPython/core/tests/test_completer.py b/IPython/core/tests/test_completer.py index 34cdff0..2f9ea48 100644 --- a/IPython/core/tests/test_completer.py +++ b/IPython/core/tests/test_completer.py @@ -18,6 +18,7 @@ from IPython.core import completer from IPython.external.decorators import knownfailureif from IPython.utils.tempdir import TemporaryDirectory from IPython.utils.generics import complete_object +from IPython.testing.globalipapp import get_ipython #----------------------------------------------------------------------------- # Test functions @@ -229,4 +230,15 @@ def test_omit__names(): nt.assert_false('ip.__str__' in matches) nt.assert_false('ip._hidden_attr' in matches) del ip._hidden_attr - \ No newline at end of file + +def test_get__all__entries_ok(): + class A(object): + __all__ = ['x', 1] + words = completer.get__all__entries(A()) + nt.assert_equal(words, ['x']) + +def test_get__all__entries_no__all__ok(): + class A(object): + pass + words = completer.get__all__entries(A()) + nt.assert_equal(words, []) \ No newline at end of file