From 15c419de124d3f85f18ce96bb412e7c533fb3b4c Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Sun, 20 Jul 2008 23:09:29 +0000 Subject: 24: Ported Registar to changes around Attribute; updated unit tests --- ipalib/base.py | 153 ++++++++++++++++++++++++++------------------------------- 1 file changed, 69 insertions(+), 84 deletions(-) (limited to 'ipalib/base.py') diff --git a/ipalib/base.py b/ipalib/base.py index b4d20450a..a62d58121 100644 --- a/ipalib/base.py +++ b/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): -- cgit