diff options
Diffstat (limited to 'ipalib/plugable.py')
-rw-r--r-- | ipalib/plugable.py | 58 |
1 files changed, 54 insertions, 4 deletions
diff --git a/ipalib/plugable.py b/ipalib/plugable.py index fd87586d..b0ba32b7 100644 --- a/ipalib/plugable.py +++ b/ipalib/plugable.py @@ -29,7 +29,9 @@ import re import inspect import errors from errors import check_type, check_isinstance -from config import Environment +from config import Environment, Env +import constants +import util class ReadOnly(object): @@ -707,19 +709,67 @@ class API(DictProxy): """ Dynamic API object through which `Plugin` instances are accessed. """ - __finalized = False def __init__(self, *allowed): self.__d = dict() + self.__done = set() self.register = Registrar(*allowed) - self.env = Environment() + self.env = Env() super(API, self).__init__(self.__d) + def __doing(self, name): + if name in self.__done: + raise StandardError( + '%s.%s() already called' % (self.__class__.__name__, name) + ) + self.__done.add(name) + + def __do_if_not_done(self, name): + if name not in self.__done: + getattr(self, name)() + + def isdone(self, name): + return name in self.__done + + def bootstrap(self, **overrides): + """ + Initialize environment variables needed by built-in plugins. + """ + self.__doing('bootstrap') + self.env._bootstrap(**overrides) + self.env._finalize_core(**dict(constants.DEFAULT_CONFIG)) + if self.env.mode == 'unit_test': + return + logger = util.configure_logging( + self.env.log, + self.env.verbose, + ) + object.__setattr__(self, 'logger', logger) + + def load_plugins(self): + """ + Load plugins from all standard locations. + + `API.bootstrap` will automatically be called if it hasn't been + already. + """ + self.__doing('load_plugins') + self.__do_if_not_done('bootstrap') + if self.env.mode == 'unit_test': + return + util.import_plugins_subpackage('ipalib') + if self.env.in_server: + util.import_plugins_subpackage('ipa_server') + def finalize(self): """ Finalize the registration, instantiate the plugins. + + `API.bootstrap` will automatically be called if it hasn't been + already. """ - assert not self.__finalized, 'finalize() can only be called once' + self.__doing('finalize') + self.__do_if_not_done('bootstrap') class PluginInstance(object): """ |