diff options
author | Jason Gerard DeRose <jderose@redhat.com> | 2008-07-19 04:28:03 +0000 |
---|---|---|
committer | Jason Gerard DeRose <jderose@redhat.com> | 2008-07-19 04:28:03 +0000 |
commit | ef7594ffe1bad349dc539f69ee90708460999a71 (patch) | |
tree | 0b6773b10d9c8946cb5d4ecd02644c9f2fb4eaad /ipalib/base.py | |
parent | 5470a0d29a9131a5b95e6092df898ee579600e07 (diff) | |
download | freeipa-ef7594ffe1bad349dc539f69ee90708460999a71.tar.gz freeipa-ef7594ffe1bad349dc539f69ee90708460999a71.tar.xz freeipa-ef7594ffe1bad349dc539f69ee90708460999a71.zip |
4: Got basics of API.register_command() working; added corresponding unit tests
Diffstat (limited to 'ipalib/base.py')
-rw-r--r-- | ipalib/base.py | 70 |
1 files changed, 51 insertions, 19 deletions
diff --git a/ipalib/base.py b/ipalib/base.py index eb84dd12..96dd300d 100644 --- a/ipalib/base.py +++ b/ipalib/base.py @@ -21,10 +21,24 @@ Base classes for plug-in architecture and generative API. """ -from exceptions import SetAttributeError +import inspect +import exceptions -class Command(object): +class Named(object): + #def __init__(self, prefix): + # clsname = self.__class__.__name__ + def __get_name(self): + return self.__class__.__name__ + name = property(__get_name) + + def __get_cli_name(self): + return self.name.replace('_', '-') + cli_name = property(__get_cli_name) + + +class Command(Named): + def normalize(self, kw): raise NotImplementedError @@ -35,11 +49,11 @@ class Command(object): raise NotImplementedError def __call__(self, **kw): - kw = self.normalize(kw) - invalid = self.validate(kw) + normalized = self.normalize(kw) + invalid = self.validate(normalized) if invalid: return invalid - return self.execute(kw) + return self.execute(normalize) class Argument(object): @@ -65,7 +79,7 @@ class NameSpace(object): For example, setting an attribute the normal way will raise an exception: >>> ns.my_message = 'some new value' - (raises ipalib.exceptions.SetAttributeError) + (raises exceptions.SetAttributeError) But a programmer could still set the attribute like this: @@ -96,7 +110,7 @@ class NameSpace(object): NameSpace has been locked; otherwise calls object.__setattr__(). """ if self.__locked: - raise SetAttributeError(name) + raise exceptions.SetAttributeError(name) super(NameSpace, self).__setattr__(name, value) def __getitem__(self, key): @@ -134,17 +148,35 @@ class NameSpace(object): class API(object): - def __init__(self): - self.__c = object() - self.__o = object() - - def __get_c(self): - return self.__c - c = property(__get_c) + __commands = None + __objects = None + __locked = False - def __get_o(self): - return self.__o - o = property(__get_o) - - def register_command(self, name, callback, override=False): + def __init__(self): + self.__c = {} # Proposed commands + self.__o = {} # Proposed objects + + def __get_objects(self): + return self.__objects + objects = property(__get_objects) + + def __get_commands(self): + return self.__commands + commands = property(__get_commands) + + def __merge(self, target, base, cls, override): + assert type(target) is dict + assert inspect.isclass(base) + assert inspect.isclass(cls) + assert type(override) is bool + if not issubclass(cls, base): + raise exceptions.RegistrationError( + cls, + '%s.%s' % (base.__module__, base.__name__) + ) + + def register_command(self, cls, override=False): + self.__merge(self.__c, Command, cls, override) + + def finalize(self): pass |