summaryrefslogtreecommitdiffstats
path: root/ipalib/base.py
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2008-07-22 06:41:33 +0000
committerJason Gerard DeRose <jderose@redhat.com>2008-07-22 06:41:33 +0000
commitfc33f5d359573cd977d168ea1fbed97cdc55c992 (patch)
tree6532ceb40e602993a4aab8e2fa80423968aeaafe /ipalib/base.py
parent0c574d830062d7957c2c65081e3e66fc0bb41759 (diff)
downloadfreeipa-fc33f5d359573cd977d168ea1fbed97cdc55c992.tar.gz
freeipa-fc33f5d359573cd977d168ea1fbed97cdc55c992.tar.xz
freeipa-fc33f5d359573cd977d168ea1fbed97cdc55c992.zip
28: Added new base.Register class that is a more generic way of doing the plugin registration and doesn't itself instatiate any plugins; added corresponding unit tests
Diffstat (limited to 'ipalib/base.py')
-rw-r--r--ipalib/base.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/ipalib/base.py b/ipalib/base.py
index aa8670187..6209139f5 100644
--- a/ipalib/base.py
+++ b/ipalib/base.py
@@ -292,6 +292,64 @@ class Collector(object):
return NameSpace(self.__d)
+class Proxy(object):
+ def __init__(self, d):
+ self.__d = d
+
+ def __getattr__(self, name):
+ if name not in self.__d:
+ raise AttributeError(name)
+ return self.__d[name]
+
+
+
+class Register(object):
+ __allowed = (
+ Command,
+ Object,
+ Method,
+ Property,
+ )
+
+ def __init__(self):
+ self.__d = {}
+ for base in self.__allowed:
+ assert inspect.isclass(base)
+ assert base.__name__ not in self.__d
+ sub_d = {}
+ self.__d[base.__name__] = sub_d
+ setattr(self, base.__name__, Proxy(sub_d))
+
+ def __iter__(self):
+ for key in self.__d:
+ yield key
+
+ def __getitem__(self, key):
+ return dict(self.__d[key])
+
+ def items(self):
+ for key in self:
+ yield (key, self[key])
+
+ def __findbase(self, cls):
+ if not inspect.isclass(cls):
+ raise exceptions.RegistrationError('not a class', cls)
+ for base in self.__allowed:
+ if issubclass(cls, base):
+ return base
+ raise exceptions.RegistrationError(
+ 'not subclass of an allowed base',
+ cls,
+ )
+
+ def __call__(self, cls):
+ base = self.__findbase(cls)
+ ns = self.__d[base.__name__]
+ assert cls.__name__ not in ns
+ ns[cls.__name__] = cls
+
+
+
class Registrar(object):
__objects = None
__commands = None