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):
|
def __get_obj(self):
|
||||||
return self.__obj
|
return self.__obj
|
||||||
obj = property(__get_obj)
|
def __set_obj(self, obj):
|
||||||
|
if self.__obj is not None:
|
||||||
def set_obj(self, obj=None):
|
|
||||||
if self.__locked:
|
|
||||||
raise exceptions.TwiceSetError(self.__class__.__name__, 'obj')
|
raise exceptions.TwiceSetError(self.__class__.__name__, 'obj')
|
||||||
self.__locked = True
|
|
||||||
if obj is None:
|
|
||||||
return
|
|
||||||
assert isinstance(obj, Object)
|
assert isinstance(obj, Object)
|
||||||
assert obj.name == self.__obj_name
|
|
||||||
self.__obj = obj
|
self.__obj = obj
|
||||||
|
assert self.obj is obj
|
||||||
|
obj = property(__get_obj, __set_obj)
|
||||||
|
|
||||||
def __get_obj_name(self):
|
def __get_obj_name(self):
|
||||||
return self.__obj_name
|
return self.__obj_name
|
||||||
@ -189,50 +185,37 @@ class Property(Attribute):
|
|||||||
return self.attr_name
|
return self.attr_name
|
||||||
|
|
||||||
|
|
||||||
class WithObj(Named):
|
class Command(AbstractCommand, Named):
|
||||||
_obj = None
|
pass
|
||||||
__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 Object(Named):
|
class Object(Named):
|
||||||
__commands = None
|
__methods = None
|
||||||
|
__properties = None
|
||||||
|
|
||||||
def __get_commands(self):
|
def __get_methods(self):
|
||||||
return self.__commands
|
return self.__methods
|
||||||
def __set_commands(self, commands):
|
def __set_methods(self, methods):
|
||||||
if self.__commands is not None:
|
if self.__methods is not None:
|
||||||
raise exceptions.TwiceSetError(
|
raise exceptions.TwiceSetError(
|
||||||
self.__class__.__name__, 'commands'
|
self.__class__.__name__, 'methods'
|
||||||
)
|
)
|
||||||
assert type(commands) is NameSpace
|
assert type(methods) is NameSpace
|
||||||
self.__commands = commands
|
self.__methods = methods
|
||||||
assert self.commands is commands
|
assert self.methods is methods
|
||||||
commands = property(__get_commands, __set_commands)
|
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):
|
class Collector(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.__d = {}
|
self.__d = {}
|
||||||
self.globals = []
|
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __get_d(self):
|
||||||
assert isinstance(key, str)
|
return dict(self.__d)
|
||||||
if key not in self.__d:
|
d = property(__get_d)
|
||||||
self.__d[key] = []
|
|
||||||
return self.__d[key]
|
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
for key in self.__d:
|
for key in self.__d:
|
||||||
yield key
|
yield key
|
||||||
|
|
||||||
def add(self, i):
|
def add(self, i):
|
||||||
assert isinstance(i, WithObj)
|
assert isinstance(i, Named)
|
||||||
if i._obj is None:
|
self.__d[i.name] = i
|
||||||
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))
|
|
||||||
|
|
||||||
|
def ns(self):
|
||||||
|
return NameSpace(self.__d)
|
||||||
|
|
||||||
|
|
||||||
class Registrar(object):
|
class Registrar(object):
|
||||||
__object = None
|
__objects = None
|
||||||
__commands = None
|
__commands = None
|
||||||
__properties = None
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.__tmp_objects = {}
|
self.__tmp_commands = Collector()
|
||||||
self.__tmp_commands = {}
|
self.__tmp_objects = Collector()
|
||||||
self.__tmp_properties = {}
|
self.__tmp_methods = AttributeCollector()
|
||||||
|
self.__tmp_properties = AttributeCollector()
|
||||||
|
|
||||||
def __get_objects(self):
|
def __get_objects(self):
|
||||||
return self.__objects
|
return self.__objects
|
||||||
@ -307,33 +281,44 @@ class Registrar(object):
|
|||||||
commands = property(__get_commands)
|
commands = property(__get_commands)
|
||||||
|
|
||||||
def __get_target(self, i):
|
def __get_target(self, i):
|
||||||
if isinstance(i, Object):
|
|
||||||
return (self.__tmp_objects, i.name)
|
|
||||||
if isinstance(i, Command):
|
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)
|
assert isinstance(i, Property)
|
||||||
|
return self.__tmp_properties
|
||||||
|
|
||||||
|
|
||||||
def register(self, cls):
|
def register(self, cls):
|
||||||
assert inspect.isclass(cls)
|
assert inspect.isclass(cls)
|
||||||
assert issubclass(cls, Named)
|
assert issubclass(cls, Named)
|
||||||
i = cls()
|
i = cls()
|
||||||
(target, key) = self.__get_target(i)
|
self.__get_target(i).add(i)
|
||||||
target[key] = i
|
|
||||||
|
|
||||||
def finalize(self):
|
def finalize(self):
|
||||||
obj_cmd = Collector()
|
self.__objects = self.__tmp_objects.ns()
|
||||||
for cmd in self.__tmp_commands.values():
|
for (key, ns) in self.__tmp_methods.namespaces():
|
||||||
if cmd._obj is None:
|
self.__objects[key].methods = ns
|
||||||
cmd.obj = None
|
for (key, ns) in self.__tmp_properties.namespaces():
|
||||||
else:
|
self.__objects[key].properties = ns
|
||||||
obj = self.__tmp_objects[cmd._obj]
|
commands = self.__tmp_commands.d
|
||||||
cmd.obj = obj
|
for obj in self.__objects():
|
||||||
obj_cmd.add(cmd)
|
assert isinstance(obj, Object)
|
||||||
self.__objects = NameSpace(self.__tmp_objects)
|
if obj.methods is None:
|
||||||
self.__commands = NameSpace(self.__tmp_commands)
|
obj.methods = NameSpace({})
|
||||||
for (key, ns) in obj_cmd.namespaces():
|
if obj.properties is None:
|
||||||
self.objects[key].commands = ns
|
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):
|
class API(Registrar):
|
||||||
|
@ -197,15 +197,15 @@ def test_Attribute():
|
|||||||
i = user__add()
|
i = user__add()
|
||||||
assert i.obj_name == 'user'
|
assert i.obj_name == 'user'
|
||||||
assert i.attr_name == 'add'
|
assert i.attr_name == 'add'
|
||||||
assert read_only(i, 'obj') is None
|
assert i.obj is None
|
||||||
class user(base.Object):
|
class user(base.Object):
|
||||||
pass
|
pass
|
||||||
u = user()
|
u = user()
|
||||||
i.set_obj(u)
|
i.obj = u
|
||||||
assert read_only(i, 'obj') is u
|
assert i.obj is u
|
||||||
raised = False
|
raised = False
|
||||||
try:
|
try:
|
||||||
i.set_obj(u)
|
i.obj = u
|
||||||
except exceptions.TwiceSetError:
|
except exceptions.TwiceSetError:
|
||||||
raised = True
|
raised = True
|
||||||
assert raised
|
assert raised
|
||||||
@ -232,6 +232,15 @@ def test_Property():
|
|||||||
assert i.name == 'firstname'
|
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():
|
def test_AttributeCollector():
|
||||||
class user__add(base.Attribute):
|
class user__add(base.Attribute):
|
||||||
pass
|
pass
|
||||||
@ -259,93 +268,82 @@ def test_AttributeCollector():
|
|||||||
assert g.values() == [g_a]
|
assert g.values() == [g_a]
|
||||||
|
|
||||||
|
|
||||||
|
def test_Collector():
|
||||||
|
class user(base.Object):
|
||||||
|
|
||||||
def test_WithObj():
|
|
||||||
class some_object(base.Named):
|
|
||||||
pass
|
pass
|
||||||
|
class group(base.Object):
|
||||||
class another_object(base.Named):
|
|
||||||
pass
|
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():
|
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):
|
class kinit(base.Command):
|
||||||
pass
|
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):
|
class user(base.Object):
|
||||||
pass
|
pass
|
||||||
class group(base.Object):
|
class group(base.Object):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
r = base.Registrar()
|
r = base.Registrar()
|
||||||
r.register(adduser)
|
assert read_only(r, 'objects') is None
|
||||||
r.register(moduser)
|
assert read_only(r, 'commands') is None
|
||||||
r.register(deluser)
|
|
||||||
r.register(finduser)
|
|
||||||
r.register(kinit)
|
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(user)
|
||||||
r.register(group)
|
r.register(group)
|
||||||
|
|
||||||
r.finalize()
|
r.finalize()
|
||||||
assert len(r.commands) == 5
|
|
||||||
assert len(r.objects) == 2
|
|
||||||
|
|
||||||
obj = r.objects.user
|
objects = read_only(r, 'objects')
|
||||||
assert type(obj) is user
|
assert isinstance(objects, base.NameSpace)
|
||||||
for name in ['adduser', 'moduser', 'deluser', 'finduser']:
|
assert len(objects) == 2
|
||||||
cmd = r.commands[name]
|
assert list(objects) == ['group', 'user']
|
||||||
assert type(cmd) is locals()[name]
|
assert type(objects.user) is user
|
||||||
assert cmd.obj is obj
|
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():
|
for m in u.methods():
|
||||||
raised = False
|
assert m.obj is u
|
||||||
try:
|
for p in u.properties():
|
||||||
cmd.obj = None
|
assert p.obj is u
|
||||||
except exceptions.TwiceSetError:
|
|
||||||
raised = True
|
|
||||||
assert raised
|
|
||||||
|
|
||||||
u = r.objects.user
|
g = objects.group
|
||||||
assert isinstance(u.commands, base.NameSpace)
|
assert len(g.methods) == 0
|
||||||
assert len(u.commands) == 4
|
assert len(g.properties) == 0
|
||||||
assert list(u.commands) == ['adduser', 'deluser', 'finduser', 'moduser']
|
|
||||||
|
|
||||||
|
assert len(r.commands) == 3
|
||||||
|
assert list(r.commands) == sorted(['kinit', 'add_user', 'del_user'])
|
||||||
|
Loading…
Reference in New Issue
Block a user