68: Ported to changes in NameSpace, Proxy; updated unit tests

This commit is contained in:
Jason Gerard DeRose 2008-08-07 00:14:38 +00:00
parent 03bad04e7b
commit 7335af8a9e
4 changed files with 45 additions and 58 deletions

View File

@ -127,6 +127,7 @@ class Proxy(ReadOnly):
'base', 'base',
'name', 'name',
'__target', '__target',
'__name_attr',
) )
def __init__(self, base, target, name_attr='name'): def __init__(self, base, target, name_attr='name'):
if not inspect.isclass(base): if not inspect.isclass(base):
@ -135,6 +136,7 @@ class Proxy(ReadOnly):
raise ValueError('arg2 must be instance of arg1, got %r' % target) raise ValueError('arg2 must be instance of arg1, got %r' % target)
object.__setattr__(self, 'base', base) object.__setattr__(self, 'base', base)
object.__setattr__(self, '_Proxy__target', target) object.__setattr__(self, '_Proxy__target', target)
object.__setattr__(self, '_Proxy__name_attr', name_attr)
# Check base.public # Check base.public
assert type(self.base.public) is frozenset assert type(self.base.public) is frozenset
@ -163,6 +165,14 @@ class Proxy(ReadOnly):
def _clone(self, name_attr): def _clone(self, name_attr):
return self.__class__(self.base, self.__target, 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): class NameSpace(ReadOnly):
""" """
@ -311,27 +321,27 @@ class API(ReadOnly):
keys = tuple(b.__name__ for b in allowed) keys = tuple(b.__name__ for b in allowed)
object.__setattr__(self, '_API__keys', keys) object.__setattr__(self, '_API__keys', keys)
object.__setattr__(self, 'register', Registrar(*allowed)) object.__setattr__(self, 'register', Registrar(*allowed))
object.__setattr__(self, '_API__plugins', [])
def __call__(self): def __call__(self):
""" """
Finalize the registration, instantiate the plugins. Finalize the registration, instantiate the plugins.
""" """
for (base, plugins) in self.register: d = {}
ns = NameSpace(self.__plugin_iter(base, plugins)) 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__) assert not hasattr(self, base.__name__)
object.__setattr__(self, base.__name__, ns) object.__setattr__(self, base.__name__, ns)
for plugin in self.__plugins: for plugin in d.values():
plugin.finalize(self) plugin.finalize(self)
assert plugin.api is 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): def __iter__(self):
for key in self.__keys: for key in self.__keys:
yield key yield key

View File

@ -26,21 +26,13 @@ import re
import plugable import plugable
class generic_proxy(plugable.Proxy): class cmd(plugable.Plugin):
__slots__ = ( public = frozenset((
'get_doc',
)
class cmd_proxy(plugable.Proxy):
__slots__ = (
'__call__', '__call__',
'get_doc', 'get_doc',
) 'opt',
))
class cmd(plugable.Plugin):
proxy = cmd_proxy
__opt = None __opt = None
def get_doc(self, _): def get_doc(self, _):
@ -74,15 +66,11 @@ class cmd(plugable.Plugin):
print repr(self) print repr(self)
class obj_proxy(plugable.Proxy): class obj(plugable.Plugin):
__slots__ = ( public = frozenset((
'mthd', 'mthd',
'prop', 'prop',
) ))
class obj(plugable.Plugin):
proxy = obj_proxy
__mthd = None __mthd = None
__prop = None __prop = None
@ -105,17 +93,11 @@ class obj(plugable.Plugin):
def __filter(self, name): def __filter(self, name):
for i in getattr(self.api, name): for i in getattr(self.api, name):
if i.obj_name == self.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): class attr(plugable.Plugin):
__obj = None __obj = None
proxy = generic_proxy
def __init__(self): def __init__(self):
m = re.match('^([a-z]+)_([a-z]+)$', self.__class__.__name__) 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] self.__obj = api.obj[self.obj_name]
class mthd_proxy(plugable.Proxy):
__slots__ = (
'__call__',
'get_doc',
) + ATTR_SLOTS
class mthd(attr, cmd): 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): class prop(attr):
proxy = prop_proxy public = frozenset((
'obj',
'obj_name',
))
def get_doc(self, _): def get_doc(self, _):
return _('prop doc') return _('prop doc')

View File

@ -352,18 +352,19 @@ def test_Registrar():
def test_API(): def test_API():
assert issubclass(plugable.API, plugable.ReadOnly) assert issubclass(plugable.API, plugable.ReadOnly)
# Setup the test plugins, create the Registrar: # Setup the test bases, create the API:
class ExampleProxy(plugable.Proxy):
__slots__ = ['method']
class base0(plugable.Plugin): class base0(plugable.Plugin):
proxy = ExampleProxy public = frozenset((
'method',
))
def method(self, n): def method(self, n):
return n return n
class base1(plugable.Plugin): class base1(plugable.Plugin):
proxy = ExampleProxy public = frozenset((
'method',
))
def method(self, n): def method(self, n):
return n + 1 return n + 1
@ -415,7 +416,7 @@ def test_API():
for p in xrange(3): for p in xrange(3):
plugin_name = get_plugin(b, p) plugin_name = get_plugin(b, p)
proxy = ns[plugin_name] proxy = ns[plugin_name]
assert isinstance(proxy, ExampleProxy) assert isinstance(proxy, plugable.Proxy)
assert proxy.name == plugin_name assert proxy.name == plugin_name
assert read_only(ns, plugin_name) is proxy assert read_only(ns, plugin_name) is proxy
assert read_only(proxy, 'method')(7) == 7 + b assert read_only(proxy, 'method')(7) == 7 + b

View File

@ -28,7 +28,6 @@ from ipalib import public, plugable, errors
def test_cmd(): def test_cmd():
cls = public.cmd cls = public.cmd
assert issubclass(cls, plugable.Plugin) assert issubclass(cls, plugable.Plugin)
assert cls.proxy is public.cmd_proxy
def test_obj(): def test_obj():