diff options
author | Jason Gerard DeRose <jderose@redhat.com> | 2008-07-20 07:09:28 +0000 |
---|---|---|
committer | Jason Gerard DeRose <jderose@redhat.com> | 2008-07-20 07:09:28 +0000 |
commit | 0cb26ef3ec68739a888f4295103210d301c2f9a8 (patch) | |
tree | 00b3b83408cf386a8d948f01918f2c20b5fd6c50 /ipalib/base2.py | |
parent | c2df39156979ea5a01901b97504c1de276364dfc (diff) | |
download | freeipa.git-0cb26ef3ec68739a888f4295103210d301c2f9a8.tar.gz freeipa.git-0cb26ef3ec68739a888f4295103210d301c2f9a8.tar.xz freeipa.git-0cb26ef3ec68739a888f4295103210d301c2f9a8.zip |
15: Added ipalib.base2 module where I'm experimenting with a 3rd approach that is a hybrid of the first two: a decoupled late binding OO strategy
Diffstat (limited to 'ipalib/base2.py')
-rw-r--r-- | ipalib/base2.py | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/ipalib/base2.py b/ipalib/base2.py new file mode 100644 index 00000000..fa5536bd --- /dev/null +++ b/ipalib/base2.py @@ -0,0 +1,102 @@ +# Authors: +# Jason Gerard DeRose <jderose@redhat.com> +# +# Copyright (C) 2008 Red Hat +# see file 'COPYING' for use and warranty information +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; version 2 only +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +""" +Base classes for plug-in architecture and generative API. +""" + +import inspect +import exceptions +from base import NameSpace + + +class Named(object): + def __get_name(self): + return self.__class__.__name__ + name = property(__get_name) + + +class WithObj(Named): + _obj = None + __obj = None + + def __get_obj(self): + return self.__obj + def __set_obj(self, obj): + if self.__obj is not None: + raise exceptions.TwiceSetError(self.__class__.__name__, 'obj') + 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): + pass + +class Property(WithObj): + pass + +class Object(Named): + pass + + +class Registrar(object): + __object = None + __commands = None + __properties = None + + def __init__(self): + self.__tmp_objects = {} + self.__tmp_commands = {} + self.__tmp_properties = {} + + def __get_objects(self): + return self.__objects + objects = property(__get_objects) + + def __get_commands(self): + return self.__commands + 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) + assert isinstance(i, Property) + + + def register(self, cls): + assert inspect.isclass(cls) + assert issubclass(cls, Named) + i = cls() + (target, key) = self.__get_target(i) + target[key] = i + + def finalize(self): + for cmd in self.__tmp_commands.values(): + if cmd._obj is None: + continue + obj = self.__tmp_objects[cmd._obj] + cmd.obj = obj + self.__objects = NameSpace(self.__tmp_objects) + self.__commands = NameSpace(self.__tmp_commands) |