summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ipalib/plugable.py17
-rw-r--r--ipalib/tests/test_plugable.py75
2 files changed, 78 insertions, 14 deletions
diff --git a/ipalib/plugable.py b/ipalib/plugable.py
index 054b12db..de5f3f8f 100644
--- a/ipalib/plugable.py
+++ b/ipalib/plugable.py
@@ -27,10 +27,19 @@ import errors
def to_cli(name):
- assert isinstance(name, basestring)
+ """
+ Takes a Python identifier and transforms it into form suitable for the
+ Command Line Interface.
+ """
+ assert isinstance(name, str)
return name.replace('__', '.').replace('_', '-')
+
def from_cli(cli_name):
+ """
+ Takes a string from the Command Line Interface and transforms it into a
+ Python identifier.
+ """
assert isinstance(cli_name, basestring)
return cli_name.replace('-', '_').replace('.', '__')
@@ -69,7 +78,6 @@ class Proxy(object):
__slots__ = (
'__obj',
'name',
- 'cli_name',
)
def __init__(self, obj, proxy_name=None):
@@ -77,11 +85,10 @@ class Proxy(object):
Proxy attributes on `obj`.
"""
if proxy_name is None:
- proxy_name = obj.name
+ proxy_name = obj.__class__.__name__
assert isinstance(proxy_name, str)
object.__setattr__(self, '_Proxy__obj', obj)
object.__setattr__(self, 'name', proxy_name)
- object.__setattr__(self, 'cli_name', to_cli(proxy_name))
for name in self.__slots__:
object.__setattr__(self, name, getattr(obj, name))
@@ -107,7 +114,7 @@ class Proxy(object):
return '%s(%r)' % (self.__class__.__name__, self.__obj)
def __str__(self):
- return self.cli_name
+ return to_cli(self.name)
class Registrar(object):
diff --git a/ipalib/tests/test_plugable.py b/ipalib/tests/test_plugable.py
index 0421e72b..023bf45f 100644
--- a/ipalib/tests/test_plugable.py
+++ b/ipalib/tests/test_plugable.py
@@ -55,20 +55,77 @@ def test_Plugin():
def test_Proxy():
class CommandProxy(plugable.Proxy):
__slots__ = (
- 'get_label',
+ 'validate',
'__call__',
)
- class Command(plugable.Plugin):
- def get_label(self):
- return 'Add User'
- def __call__(self, *argv, **kw):
- return (argv, kw)
+ class do_something(object):
+ def __repr__(self):
+ return '<my repr>'
- i = Command()
- p = CommandProxy(i, 'hello')
+ def __call__(self, arg):
+ return arg + 1
+
+ def validate(self, arg):
+ return arg + 2
+
+ def not_public(self, arg):
+ return arg + 3
+
+ # Test basic Proxy functionality
+ i = do_something()
+ p = CommandProxy(i)
assert '__dict__' not in dir(p)
- #assert repr(p) == 'CommandProxy(%s.Command())' % __name__
+ assert p.name == 'do_something'
+ assert str(p) == 'do-something'
+ assert repr(p) == 'CommandProxy(<my repr>)'
+ assert p(1) == 2
+ assert p.validate(1) == 3
+
+ # Test that proxy_name can be overriden:
+ i = do_something()
+ p = CommandProxy(i, proxy_name='user__add')
+ assert '__dict__' not in dir(p)
+ assert p.name == 'user__add'
+ assert str(p) == 'user.add'
+ assert repr(p) == 'CommandProxy(<my repr>)'
+ assert p(1) == 2
+ assert p.validate(1) == 3
+
+ # Test that attributes not listed in __slots__ are not present:
+ name = 'not_public'
+ i = do_something()
+ p = CommandProxy(i)
+ assert getattr(i, name)(1) == 4
+ raised = False
+ try:
+ getattr(p, name)
+ except AttributeError:
+ raised = True
+ assert raised
+
+ # Test that attributes are read-only:
+ name = 'validate'
+ i = do_something()
+ p = CommandProxy(i)
+ assert getattr(p, name)(1) == 3
+ raised = False
+ try:
+ # Test __setattr__()
+ setattr(p, name, 'new_object')
+ except AttributeError:
+ raised = True
+ assert raised
+ raised = False
+ try:
+ # Test __delattr__()
+ delattr(p, name)
+ except AttributeError:
+ raised = True
+ assert raised
+
+
+
def test_Registrar():