diff options
author | Jan Cholasta <jcholast@redhat.com> | 2015-06-22 10:58:43 +0000 |
---|---|---|
committer | Jan Cholasta <jcholast@redhat.com> | 2015-07-01 13:05:30 +0000 |
commit | e39fe4ed31042bd28357d093fdbd93b4d6d59aaa (patch) | |
tree | c9edd3b3d710ae642d91eb8ca0c060cb5f6d0f0c /ipalib/plugable.py | |
parent | 2d1515323acb4125306817096bafab6623de0b47 (diff) | |
download | freeipa-e39fe4ed31042bd28357d093fdbd93b4d6d59aaa.tar.gz freeipa-e39fe4ed31042bd28357d093fdbd93b4d6d59aaa.tar.xz freeipa-e39fe4ed31042bd28357d093fdbd93b4d6d59aaa.zip |
plugable: Pass API to plugins on initialization rather than using set_api
https://fedorahosted.org/freeipa/ticket/3090
Reviewed-By: Martin Babinsky <mbabinsk@redhat.com>
Diffstat (limited to 'ipalib/plugable.py')
-rw-r--r-- | ipalib/plugable.py | 50 |
1 files changed, 22 insertions, 28 deletions
diff --git a/ipalib/plugable.py b/ipalib/plugable.py index d13e5ed88..bdd19c2f4 100644 --- a/ipalib/plugable.py +++ b/ipalib/plugable.py @@ -224,8 +224,9 @@ class Plugin(ReadOnly): label = None - def __init__(self): - self.__api = None + def __init__(self, api): + assert api is not None + self.__api = api self.__finalize_called = False self.__finalized = False self.__finalize_lock = threading.RLock() @@ -254,14 +255,27 @@ class Plugin(ReadOnly): ) ) - def __get_api(self): + @property + def api(self): """ - Return `API` instance passed to `set_api()`. - - If `set_api()` has not yet been called, None is returned. + Return `API` instance passed to `__init__()`. """ return self.__api - api = property(__get_api) + + # FIXME: for backward compatibility only + @property + def env(self): + return self.__api.env + + # FIXME: for backward compatibility only + @property + def Backend(self): + return self.__api.Backend + + # FIXME: for backward compatibility only + @property + def Command(self): + return self.__api.Command def finalize(self): """ @@ -336,23 +350,6 @@ class Plugin(ReadOnly): "attribute '%s' of plugin '%s' was not set in finalize()" % (self.name, obj.name) ) - def set_api(self, api): - """ - Set reference to `API` instance. - """ - assert self.__api is None, 'set_api() can only be called once' - assert api is not None, 'set_api() argument cannot be None' - self.__api = api - if not isinstance(api, API): - return - for name in api: - assert not hasattr(self, name) - setattr(self, name, api[name]) - for name in ('env', 'context'): - if hasattr(api, name): - assert not hasattr(self, name) - setattr(self, name, getattr(api, name)) - def call(self, executable, *args): """ Call ``executable`` with ``args`` using subprocess.call(). @@ -746,7 +743,7 @@ class API(DictProxy): try: instance = plugins[klass] except KeyError: - instance = plugins[klass] = klass() + instance = plugins[klass] = klass(self) members.append(instance) plugin_info.setdefault( '%s.%s' % (klass.__module__, klass.__name__), @@ -760,9 +757,6 @@ class API(DictProxy): self.__d[name] = namespace object.__setattr__(self, name, namespace) - for instance in plugins.itervalues(): - instance.set_api(self) - for klass, instance in plugins.iteritems(): if not production_mode: assert instance.api is self |