17: Registar.finalize() now sets the commands property on each object with commands

This commit is contained in:
Jason Gerard DeRose 2008-07-20 17:33:17 +00:00
parent cf32ac3370
commit 66cd39f519
2 changed files with 51 additions and 1 deletions

View File

@ -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

View File

@ -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']