From 7335af8a9eb4b5ab6a0884f686a51a050464320b Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Thu, 7 Aug 2008 00:14:38 +0000 Subject: [PATCH] 68: Ported to changes in NameSpace, Proxy; updated unit tests --- ipalib/plugable.py | 32 +++++++++++++------- ipalib/public.py | 55 ++++++++++------------------------- ipalib/tests/test_plugable.py | 15 +++++----- ipalib/tests/test_public.py | 1 - 4 files changed, 45 insertions(+), 58 deletions(-) diff --git a/ipalib/plugable.py b/ipalib/plugable.py index 769e5617e..91a9143d7 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 5c413ab09..692a6d3c3 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 0e170564e..ebc3a78f9 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 bfe951aba..0985658e1 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():