mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
24: Ported Registar to changes around Attribute; updated unit tests
This commit is contained in:
parent
6f58880dcd
commit
15c419de12
151
ipalib/base.py
151
ipalib/base.py
@ -158,17 +158,13 @@ class Attribute(Named):
|
||||
|
||||
def __get_obj(self):
|
||||
return self.__obj
|
||||
obj = property(__get_obj)
|
||||
|
||||
def set_obj(self, obj=None):
|
||||
if self.__locked:
|
||||
def __set_obj(self, obj):
|
||||
if self.__obj is not None:
|
||||
raise exceptions.TwiceSetError(self.__class__.__name__, 'obj')
|
||||
self.__locked = True
|
||||
if obj is None:
|
||||
return
|
||||
assert isinstance(obj, Object)
|
||||
assert obj.name == self.__obj_name
|
||||
self.__obj = obj
|
||||
assert self.obj is obj
|
||||
obj = property(__get_obj, __set_obj)
|
||||
|
||||
def __get_obj_name(self):
|
||||
return self.__obj_name
|
||||
@ -189,50 +185,37 @@ class Property(Attribute):
|
||||
return self.attr_name
|
||||
|
||||
|
||||
class WithObj(Named):
|
||||
_obj = None
|
||||
__obj = None
|
||||
__obj_locked = False
|
||||
|
||||
def __get_obj(self):
|
||||
return self.__obj
|
||||
def __set_obj(self, obj):
|
||||
if self.__obj_locked:
|
||||
raise exceptions.TwiceSetError(self.__class__.__name__, 'obj')
|
||||
self.__obj_locked = True
|
||||
if obj is None:
|
||||
assert self.__obj is None
|
||||
assert self.obj is None
|
||||
else:
|
||||
assert isinstance(obj, Named)
|
||||
assert isinstance(self._obj, str)
|
||||
assert obj.name == self._obj
|
||||
self.__obj = obj
|
||||
assert self.obj is obj
|
||||
obj = property(__get_obj, __set_obj)
|
||||
|
||||
|
||||
class Command(WithObj):
|
||||
def __call__(self):
|
||||
print 'You called %s()' % self.name
|
||||
|
||||
class Command(AbstractCommand, Named):
|
||||
pass
|
||||
|
||||
|
||||
class Object(Named):
|
||||
__commands = None
|
||||
__methods = None
|
||||
__properties = None
|
||||
|
||||
def __get_commands(self):
|
||||
return self.__commands
|
||||
def __set_commands(self, commands):
|
||||
if self.__commands is not None:
|
||||
def __get_methods(self):
|
||||
return self.__methods
|
||||
def __set_methods(self, methods):
|
||||
if self.__methods is not None:
|
||||
raise exceptions.TwiceSetError(
|
||||
self.__class__.__name__, 'commands'
|
||||
self.__class__.__name__, 'methods'
|
||||
)
|
||||
assert type(commands) is NameSpace
|
||||
self.__commands = commands
|
||||
assert self.commands is commands
|
||||
commands = property(__get_commands, __set_commands)
|
||||
assert type(methods) is NameSpace
|
||||
self.__methods = methods
|
||||
assert self.methods is methods
|
||||
methods = property(__get_methods, __set_methods)
|
||||
|
||||
def __get_properties(self):
|
||||
return self.__properties
|
||||
def __set_properties(self, properties):
|
||||
if self.__properties is not None:
|
||||
raise exceptions.TwiceSetError(
|
||||
self.__class__.__name__, 'properties'
|
||||
)
|
||||
assert type(properties) is NameSpace
|
||||
self.__properties = properties
|
||||
assert self.properties is properties
|
||||
properties = property(__get_properties, __set_properties)
|
||||
|
||||
|
||||
|
||||
@ -262,41 +245,32 @@ class AttributeCollector(object):
|
||||
class Collector(object):
|
||||
def __init__(self):
|
||||
self.__d = {}
|
||||
self.globals = []
|
||||
|
||||
def __getitem__(self, key):
|
||||
assert isinstance(key, str)
|
||||
if key not in self.__d:
|
||||
self.__d[key] = []
|
||||
return self.__d[key]
|
||||
def __get_d(self):
|
||||
return dict(self.__d)
|
||||
d = property(__get_d)
|
||||
|
||||
def __iter__(self):
|
||||
for key in self.__d:
|
||||
yield key
|
||||
|
||||
def add(self, i):
|
||||
assert isinstance(i, WithObj)
|
||||
if i._obj is None:
|
||||
self.globals.append(i)
|
||||
else:
|
||||
self[i._obj].append(i)
|
||||
|
||||
def namespaces(self):
|
||||
for key in self:
|
||||
d = dict((i.name, i) for i in self[key])
|
||||
yield (key, NameSpace(d))
|
||||
assert isinstance(i, Named)
|
||||
self.__d[i.name] = i
|
||||
|
||||
def ns(self):
|
||||
return NameSpace(self.__d)
|
||||
|
||||
|
||||
class Registrar(object):
|
||||
__object = None
|
||||
__objects = None
|
||||
__commands = None
|
||||
__properties = None
|
||||
|
||||
def __init__(self):
|
||||
self.__tmp_objects = {}
|
||||
self.__tmp_commands = {}
|
||||
self.__tmp_properties = {}
|
||||
self.__tmp_commands = Collector()
|
||||
self.__tmp_objects = Collector()
|
||||
self.__tmp_methods = AttributeCollector()
|
||||
self.__tmp_properties = AttributeCollector()
|
||||
|
||||
def __get_objects(self):
|
||||
return self.__objects
|
||||
@ -307,33 +281,44 @@ class Registrar(object):
|
||||
commands = property(__get_commands)
|
||||
|
||||
def __get_target(self, i):
|
||||
if isinstance(i, Object):
|
||||
return (self.__tmp_objects, i.name)
|
||||
if isinstance(i, Command):
|
||||
return (self.__tmp_commands, i.name)
|
||||
return self.__tmp_commands
|
||||
if isinstance(i, Object):
|
||||
return self.__tmp_objects
|
||||
if isinstance(i, Method):
|
||||
return self.__tmp_methods
|
||||
assert isinstance(i, Property)
|
||||
return self.__tmp_properties
|
||||
|
||||
|
||||
def register(self, cls):
|
||||
assert inspect.isclass(cls)
|
||||
assert issubclass(cls, Named)
|
||||
i = cls()
|
||||
(target, key) = self.__get_target(i)
|
||||
target[key] = i
|
||||
self.__get_target(i).add(i)
|
||||
|
||||
|
||||
def finalize(self):
|
||||
obj_cmd = Collector()
|
||||
for cmd in self.__tmp_commands.values():
|
||||
if cmd._obj is None:
|
||||
cmd.obj = None
|
||||
else:
|
||||
obj = self.__tmp_objects[cmd._obj]
|
||||
cmd.obj = obj
|
||||
obj_cmd.add(cmd)
|
||||
self.__objects = NameSpace(self.__tmp_objects)
|
||||
self.__commands = NameSpace(self.__tmp_commands)
|
||||
for (key, ns) in obj_cmd.namespaces():
|
||||
self.objects[key].commands = ns
|
||||
self.__objects = self.__tmp_objects.ns()
|
||||
for (key, ns) in self.__tmp_methods.namespaces():
|
||||
self.__objects[key].methods = ns
|
||||
for (key, ns) in self.__tmp_properties.namespaces():
|
||||
self.__objects[key].properties = ns
|
||||
commands = self.__tmp_commands.d
|
||||
for obj in self.__objects():
|
||||
assert isinstance(obj, Object)
|
||||
if obj.methods is None:
|
||||
obj.methods = NameSpace({})
|
||||
if obj.properties is None:
|
||||
obj.properties = NameSpace({})
|
||||
for m in obj.methods():
|
||||
m.obj = obj
|
||||
assert m.name not in commands
|
||||
commands[m.name] = m
|
||||
for p in obj.properties():
|
||||
p.obj = obj
|
||||
self.__commands = NameSpace(commands)
|
||||
|
||||
|
||||
|
||||
class API(Registrar):
|
||||
|
@ -197,15 +197,15 @@ def test_Attribute():
|
||||
i = user__add()
|
||||
assert i.obj_name == 'user'
|
||||
assert i.attr_name == 'add'
|
||||
assert read_only(i, 'obj') is None
|
||||
assert i.obj is None
|
||||
class user(base.Object):
|
||||
pass
|
||||
u = user()
|
||||
i.set_obj(u)
|
||||
assert read_only(i, 'obj') is u
|
||||
i.obj = u
|
||||
assert i.obj is u
|
||||
raised = False
|
||||
try:
|
||||
i.set_obj(u)
|
||||
i.obj = u
|
||||
except exceptions.TwiceSetError:
|
||||
raised = True
|
||||
assert raised
|
||||
@ -232,6 +232,15 @@ def test_Property():
|
||||
assert i.name == 'firstname'
|
||||
|
||||
|
||||
def test_Command():
|
||||
class dostuff(base.Command):
|
||||
pass
|
||||
i = dostuff()
|
||||
assert isinstance(i, base.AbstractCommand)
|
||||
assert i.name == 'dostuff'
|
||||
|
||||
|
||||
|
||||
def test_AttributeCollector():
|
||||
class user__add(base.Attribute):
|
||||
pass
|
||||
@ -259,93 +268,82 @@ def test_AttributeCollector():
|
||||
assert g.values() == [g_a]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def test_WithObj():
|
||||
class some_object(base.Named):
|
||||
def test_Collector():
|
||||
class user(base.Object):
|
||||
pass
|
||||
|
||||
class another_object(base.Named):
|
||||
class group(base.Object):
|
||||
pass
|
||||
u = user()
|
||||
g = group()
|
||||
c = base.Collector()
|
||||
c.add(u)
|
||||
c.add(g)
|
||||
ns = c.ns()
|
||||
assert isinstance(ns, base.NameSpace)
|
||||
assert set(ns) == set(['user', 'group'])
|
||||
assert ns.user is u
|
||||
assert ns.group is g
|
||||
|
||||
class some_command(base.WithObj):
|
||||
_obj = 'some_object'
|
||||
|
||||
obj = some_object()
|
||||
cmd = some_command()
|
||||
|
||||
# Test that it can be set:
|
||||
assert cmd.obj is None
|
||||
cmd.obj = obj
|
||||
assert cmd.obj is obj
|
||||
|
||||
# Test that it cannot be set twice:
|
||||
raised = False
|
||||
try:
|
||||
cmd.obj = obj
|
||||
except exceptions.TwiceSetError:
|
||||
raised = True
|
||||
assert raised
|
||||
|
||||
# Test that it can't be set with the wrong name:
|
||||
obj = another_object()
|
||||
cmd = some_command()
|
||||
raised = False
|
||||
try:
|
||||
cmd.obj = obj
|
||||
except AssertionError:
|
||||
raised = True
|
||||
assert raised
|
||||
|
||||
|
||||
def test_Registar():
|
||||
class adduser(base.Command):
|
||||
_obj = 'user'
|
||||
class moduser(base.Command):
|
||||
_obj = 'user'
|
||||
class deluser(base.Command):
|
||||
_obj = 'user'
|
||||
class finduser(base.Command):
|
||||
_obj = 'user'
|
||||
class kinit(base.Command):
|
||||
pass
|
||||
class user__add(base.Method):
|
||||
pass
|
||||
class user__del(base.Method):
|
||||
pass
|
||||
class user__firstname(base.Property):
|
||||
pass
|
||||
class user__lastname(base.Property):
|
||||
pass
|
||||
class user__login(base.Property):
|
||||
pass
|
||||
class user(base.Object):
|
||||
pass
|
||||
class group(base.Object):
|
||||
pass
|
||||
|
||||
r = base.Registrar()
|
||||
r.register(adduser)
|
||||
r.register(moduser)
|
||||
r.register(deluser)
|
||||
r.register(finduser)
|
||||
assert read_only(r, 'objects') is None
|
||||
assert read_only(r, 'commands') is None
|
||||
|
||||
|
||||
r.register(kinit)
|
||||
r.register(user__add)
|
||||
r.register(user__del)
|
||||
r.register(user__firstname)
|
||||
r.register(user__lastname)
|
||||
r.register(user__login)
|
||||
r.register(user)
|
||||
r.register(group)
|
||||
|
||||
r.finalize()
|
||||
assert len(r.commands) == 5
|
||||
assert len(r.objects) == 2
|
||||
|
||||
obj = r.objects.user
|
||||
assert type(obj) is user
|
||||
for name in ['adduser', 'moduser', 'deluser', 'finduser']:
|
||||
cmd = r.commands[name]
|
||||
assert type(cmd) is locals()[name]
|
||||
assert cmd.obj is obj
|
||||
objects = read_only(r, 'objects')
|
||||
assert isinstance(objects, base.NameSpace)
|
||||
assert len(objects) == 2
|
||||
assert list(objects) == ['group', 'user']
|
||||
assert type(objects.user) is user
|
||||
assert type(objects.group) is group
|
||||
|
||||
assert r.commands.kinit.obj is None
|
||||
u = objects.user
|
||||
assert len(u.methods) == 2
|
||||
assert list(u.methods) == ['add', 'del']
|
||||
assert len(u.properties) == 3
|
||||
assert list(u.properties) == ['firstname', 'lastname', 'login']
|
||||
|
||||
for cmd in r.commands():
|
||||
raised = False
|
||||
try:
|
||||
cmd.obj = None
|
||||
except exceptions.TwiceSetError:
|
||||
raised = True
|
||||
assert raised
|
||||
for m in u.methods():
|
||||
assert m.obj is u
|
||||
for p in u.properties():
|
||||
assert p.obj is u
|
||||
|
||||
u = r.objects.user
|
||||
assert isinstance(u.commands, base.NameSpace)
|
||||
assert len(u.commands) == 4
|
||||
assert list(u.commands) == ['adduser', 'deluser', 'finduser', 'moduser']
|
||||
g = objects.group
|
||||
assert len(g.methods) == 0
|
||||
assert len(g.properties) == 0
|
||||
|
||||
|
||||
assert len(r.commands) == 3
|
||||
assert list(r.commands) == sorted(['kinit', 'add_user', 'del_user'])
|
||||
|
Loading…
Reference in New Issue
Block a user