summaryrefslogtreecommitdiffstats
path: root/ipalib
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2008-08-07 00:14:38 +0000
committerJason Gerard DeRose <jderose@redhat.com>2008-08-07 00:14:38 +0000
commit7335af8a9eb4b5ab6a0884f686a51a050464320b (patch)
treebabc5a385d0a74b0e8d4ffc88026c22e968810bb /ipalib
parent03bad04e7bdf6bf02eca13e0b3af3beb587fdc3d (diff)
downloadfreeipa.git-7335af8a9eb4b5ab6a0884f686a51a050464320b.tar.gz
freeipa.git-7335af8a9eb4b5ab6a0884f686a51a050464320b.tar.xz
freeipa.git-7335af8a9eb4b5ab6a0884f686a51a050464320b.zip
68: Ported to changes in NameSpace, Proxy; updated unit tests
Diffstat (limited to 'ipalib')
-rw-r--r--ipalib/plugable.py32
-rw-r--r--ipalib/public.py55
-rw-r--r--ipalib/tests/test_plugable.py15
-rw-r--r--ipalib/tests/test_public.py1
4 files changed, 45 insertions, 58 deletions
diff --git a/ipalib/plugable.py b/ipalib/plugable.py
index 769e5617..91a9143d 100644
--- a/ipalib/plugable.py
+++ b/ipalib/plugable.py
@@ -127,6 +127,7 @@ class Proxy(ReadOnly):
'base',
'name',
'__target',
+ '__name_attr',
)
def __init__(self, base, target, name_attr='name'):
if not inspect.isclass(base):
@@ -135,6 +136,7 @@ class Proxy(ReadOnly):
raise ValueError('arg2 must be instance of arg1, got %r' % target)
object.__setattr__(self, 'base', base)
object.__setattr__(self, '_Proxy__target', target)
+ object.__setattr__(self, '_Proxy__name_attr', name_attr)
# Check base.public
assert type(self.base.public) is frozenset
@@ -163,6 +165,14 @@ class Proxy(ReadOnly):
def _clone(self, name_attr):
return self.__class__(self.base, self.__target, name_attr)
+ def __repr__(self):
+ return '%s(%s, %r, %r)' % (
+ self.__class__.__name__,
+ self.base.__name__,
+ self.__target,
+ self.__name_attr,
+ )
+
class NameSpace(ReadOnly):
"""
@@ -311,27 +321,27 @@ class API(ReadOnly):
keys = tuple(b.__name__ for b in allowed)
object.__setattr__(self, '_API__keys', keys)
object.__setattr__(self, 'register', Registrar(*allowed))
- object.__setattr__(self, '_API__plugins', [])
def __call__(self):
"""
Finalize the registration, instantiate the plugins.
"""
- for (base, plugins) in self.register:
- ns = NameSpace(self.__plugin_iter(base, plugins))
+ d = {}
+ def plugin_iter(base, classes):
+ for cls in classes:
+ if cls not in d:
+ d[cls] = cls()
+ plugin = d[cls]
+ yield Proxy(base, plugin)
+
+ for (base, classes) in self.register:
+ ns = NameSpace(plugin_iter(base, classes))
assert not hasattr(self, base.__name__)
object.__setattr__(self, base.__name__, ns)
- for plugin in self.__plugins:
+ for plugin in d.values():
plugin.finalize(self)
assert plugin.api is self
- def __plugin_iter(self, base, plugins):
- assert issubclass(base.proxy, Proxy)
- for cls in plugins:
- plugin = cls()
- self.__plugins.append(plugin)
- yield base.proxy(plugin)
-
def __iter__(self):
for key in self.__keys:
yield key
diff --git a/ipalib/public.py b/ipalib/public.py
index 5c413ab0..692a6d3c 100644
--- a/ipalib/public.py
+++ b/ipalib/public.py
@@ -26,21 +26,13 @@ import re
import plugable
-class generic_proxy(plugable.Proxy):
- __slots__ = (
- 'get_doc',
- )
-
-
-class cmd_proxy(plugable.Proxy):
- __slots__ = (
+class cmd(plugable.Plugin):
+ public = frozenset((
'__call__',
'get_doc',
- )
+ 'opt',
-
-class cmd(plugable.Plugin):
- proxy = cmd_proxy
+ ))
__opt = None
def get_doc(self, _):
@@ -74,15 +66,11 @@ class cmd(plugable.Plugin):
print repr(self)
-class obj_proxy(plugable.Proxy):
- __slots__ = (
+class obj(plugable.Plugin):
+ public = frozenset((
'mthd',
'prop',
- )
-
-
-class obj(plugable.Plugin):
- proxy = obj_proxy
+ ))
__mthd = None
__prop = None
@@ -105,17 +93,11 @@ class obj(plugable.Plugin):
def __filter(self, name):
for i in getattr(self.api, name):
if i.obj_name == self.name:
- yield i._clone(i.attr_name)
+ yield i._clone('attr_name')
-ATTR_SLOTS = (
- 'obj_name',
- 'attr_name',
-)
-
class attr(plugable.Plugin):
__obj = None
- proxy = generic_proxy
def __init__(self):
m = re.match('^([a-z]+)_([a-z]+)$', self.__class__.__name__)
@@ -144,23 +126,18 @@ class attr(plugable.Plugin):
self.__obj = api.obj[self.obj_name]
-class mthd_proxy(plugable.Proxy):
- __slots__ = (
- '__call__',
- 'get_doc',
- ) + ATTR_SLOTS
-
class mthd(attr, cmd):
- proxy = mthd_proxy
+ public = frozenset((
+ 'obj',
+ 'obj_name',
+ ))
-class prop_proxy(plugable.Proxy):
- __slots__ = (
- 'get_doc',
- ) + ATTR_SLOTS
-
class prop(attr):
- proxy = prop_proxy
+ public = frozenset((
+ 'obj',
+ 'obj_name',
+ ))
def get_doc(self, _):
return _('prop doc')
diff --git a/ipalib/tests/test_plugable.py b/ipalib/tests/test_plugable.py
index 0e170564..ebc3a78f 100644
--- a/ipalib/tests/test_plugable.py
+++ b/ipalib/tests/test_plugable.py
@@ -352,18 +352,19 @@ def test_Registrar():
def test_API():
assert issubclass(plugable.API, plugable.ReadOnly)
- # Setup the test plugins, create the Registrar:
- class ExampleProxy(plugable.Proxy):
- __slots__ = ['method']
-
+ # Setup the test bases, create the API:
class base0(plugable.Plugin):
- proxy = ExampleProxy
+ public = frozenset((
+ 'method',
+ ))
def method(self, n):
return n
class base1(plugable.Plugin):
- proxy = ExampleProxy
+ public = frozenset((
+ 'method',
+ ))
def method(self, n):
return n + 1
@@ -415,7 +416,7 @@ def test_API():
for p in xrange(3):
plugin_name = get_plugin(b, p)
proxy = ns[plugin_name]
- assert isinstance(proxy, ExampleProxy)
+ assert isinstance(proxy, plugable.Proxy)
assert proxy.name == plugin_name
assert read_only(ns, plugin_name) is proxy
assert read_only(proxy, 'method')(7) == 7 + b
diff --git a/ipalib/tests/test_public.py b/ipalib/tests/test_public.py
index bfe951ab..0985658e 100644
--- a/ipalib/tests/test_public.py
+++ b/ipalib/tests/test_public.py
@@ -28,7 +28,6 @@ from ipalib import public, plugable, errors
def test_cmd():
cls = public.cmd
assert issubclass(cls, plugable.Plugin)
- assert cls.proxy is public.cmd_proxy
def test_obj():