369: Added Object.backend attribute used to associated it with a particular backend component

This commit is contained in:
Jason Gerard DeRose 2008-09-26 02:43:11 +00:00
parent 433d2e2e11
commit aa45ec616a
2 changed files with 43 additions and 7 deletions

View File

@ -511,17 +511,22 @@ class Command(plugable.Plugin):
class Object(plugable.Plugin):
__public__ = frozenset((
'backend',
'methods',
'properties',
'params',
'primary_key',
'params_minus_pk',
))
backend = None
methods = None
properties = None
params = None
primary_key = None
params_minus_pk = None
# Can override in subclasses:
backend_name = None
takes_params = tuple()
def set_api(self, api):
@ -549,8 +554,13 @@ class Object(plugable.Plugin):
filter(lambda p: not p.primary_key, self.params()), sort=False
)
if 'Backend' in self.api and self.backend_name in self.api.Backend:
self.backend = self.api.Backend[self.backend_name]
def __get_attrs(self, name):
namespace = getattr(self.api, name)
if name not in self.api:
return
namespace = self.api[name]
assert type(namespace) is plugable.NameSpace
for proxy in namespace(): # Equivalent to dict.itervalues()
if proxy.obj_name == self.name:

View File

@ -23,7 +23,7 @@ Unit tests for `ipalib.frontend` module.
from tstutil import raises, getitem, no_set, no_del, read_only, ClassChecker
from tstutil import check_TypeError
from ipalib import frontend, plugable, errors, ipa_types
from ipalib import frontend, backend, plugable, errors, ipa_types
def test_RULE_FLAG():
@ -760,6 +760,7 @@ class test_Object(ClassChecker):
def test_class(self):
assert self.cls.__bases__ == (plugable.Plugin,)
assert self.cls.backend is None
assert self.cls.methods is None
assert self.cls.properties is None
assert self.cls.params is None
@ -771,6 +772,7 @@ class test_Object(ClassChecker):
Test the `frontend.Object.__init__` method.
"""
o = self.cls()
assert o.backend is None
assert o.methods is None
assert o.properties is None
assert o.params is None
@ -810,13 +812,16 @@ class test_Object(ClassChecker):
properties='property_%d',
)
class api(object):
Method = plugable.NameSpace(
_d = dict(
Method=plugable.NameSpace(
get_attributes(cnt, formats['methods'])
)
Property = plugable.NameSpace(
),
Property=plugable.NameSpace(
get_attributes(cnt, formats['properties'])
)
),
)
api = plugable.MagicDict(_d)
assert len(api.Method) == cnt * 3
assert len(api.Property) == cnt * 3
@ -914,6 +919,27 @@ class test_Object(ClassChecker):
assert str(e) == \
'example3 (Object) has multiple primary keys: one, two, four'
def test_backend(self):
"""
Test the `frontend.Object.backend` attribute.
"""
api = plugable.API(
frontend.Object,
frontend.Method,
frontend.Property,
backend.Backend,
)
class ldap(backend.Backend):
whatever = 'It worked!'
api.register(ldap)
class user(frontend.Object):
backend_name = 'ldap'
api.register(user)
api.finalize()
b = api.Object.user.backend
assert isinstance(b, ldap)
assert b.whatever == 'It worked!'
class test_Attribute(ClassChecker):
"""