diff options
author | Jason Gerard DeRose <jderose@redhat.com> | 2008-07-20 17:33:17 +0000 |
---|---|---|
committer | Jason Gerard DeRose <jderose@redhat.com> | 2008-07-20 17:33:17 +0000 |
commit | 66cd39f51991abbff1c5a8c08fa8c9b1f358284a (patch) | |
tree | 90bbcafbc1751f7362c0c31492718622a16a213b /ipalib | |
parent | cf32ac337062d76babb8efbf217c4045138b686a (diff) | |
download | freeipa-66cd39f51991abbff1c5a8c08fa8c9b1f358284a.tar.gz freeipa-66cd39f51991abbff1c5a8c08fa8c9b1f358284a.tar.xz freeipa-66cd39f51991abbff1c5a8c08fa8c9b1f358284a.zip |
17: Registar.finalize() now sets the commands property on each object with commands
Diffstat (limited to 'ipalib')
-rw-r--r-- | ipalib/base2.py | 47 | ||||
-rw-r--r-- | ipalib/tests/test_base2.py | 5 |
2 files changed, 51 insertions, 1 deletions
diff --git a/ipalib/base2.py b/ipalib/base2.py index 98c0a1800..827996e92 100644 --- a/ipalib/base2.py +++ b/ipalib/base2.py @@ -62,7 +62,48 @@ class Property(WithObj): pass class Object(Named): - pass + __commands = None + + def __get_commands(self): + return self.__commands + def __set_commands(self, commands): + if self.__commands is not None: + raise exceptions.TwiceSetError( + self.__class__.__name__, 'commands' + ) + assert type(commands) is NameSpace + self.__commands = commands + assert self.commands is commands + commands = property(__get_commands, __set_commands) + + +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 __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)) + class Registrar(object): @@ -99,11 +140,15 @@ class Registrar(object): target[key] = 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 diff --git a/ipalib/tests/test_base2.py b/ipalib/tests/test_base2.py index 398f6c634..0cebdade3 100644 --- a/ipalib/tests/test_base2.py +++ b/ipalib/tests/test_base2.py @@ -148,3 +148,8 @@ def test_Registar(): except exceptions.TwiceSetError: raised = True assert raised + + u = r.objects.user + assert isinstance(u.commands, base.NameSpace) + assert len(u.commands) == 4 + assert list(u.commands) == ['adduser', 'deluser', 'finduser', 'moduser'] |