mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
Implemented placeholder API.bootstrap() method; added API __doing(), __do_if_not_done(), isdone() methods borrowed from Env; API.finalize() now cascades call to API.bootstrap()
This commit is contained in:
parent
ff5cb4cf6f
commit
6b8abb0d78
@ -707,19 +707,40 @@ 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()
|
||||
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')
|
||||
|
||||
def finalize(self):
|
||||
"""
|
||||
Finalize the registration, instantiate the plugins.
|
||||
"""
|
||||
assert not self.__finalized, 'finalize() can only be called once'
|
||||
self.__doing('finalize')
|
||||
self.__do_if_not_done('bootstrap')
|
||||
|
||||
class PluginInstance(object):
|
||||
"""
|
||||
|
@ -764,7 +764,14 @@ def test_Registrar():
|
||||
assert issubclass(klass, base)
|
||||
|
||||
|
||||
def test_API():
|
||||
class test_API(ClassChecker):
|
||||
"""
|
||||
Test the `ipalib.plugable.API` class.
|
||||
"""
|
||||
|
||||
_cls = plugable.API
|
||||
|
||||
def test_API(self):
|
||||
"""
|
||||
Test the `ipalib.plugable.API` class.
|
||||
"""
|
||||
@ -817,7 +824,11 @@ def test_API():
|
||||
r(base1_plugin2)
|
||||
|
||||
# Test API instance:
|
||||
assert api.isdone('bootstrap') is False
|
||||
assert api.isdone('finalize') is False
|
||||
api.finalize()
|
||||
assert api.isdone('bootstrap') is True
|
||||
assert api.isdone('finalize') is True
|
||||
|
||||
def get_base(b):
|
||||
return 'base%d' % b
|
||||
@ -840,7 +851,8 @@ def test_API():
|
||||
assert read_only(proxy, 'method')(7) == 7 + b
|
||||
|
||||
# Test that calling finilize again raises AssertionError:
|
||||
raises(AssertionError, api.finalize)
|
||||
e = raises(StandardError, api.finalize)
|
||||
assert str(e) == 'API.finalize() already called', str(e)
|
||||
|
||||
# Test with base class that doesn't request a proxy
|
||||
class NoProxy(plugable.Plugin):
|
||||
@ -860,3 +872,14 @@ def test_API():
|
||||
assert getattr(api.NoProxy, name) is plugin
|
||||
assert isinstance(plugin, plugable.Plugin)
|
||||
assert not isinstance(plugin, plugable.PluginProxy)
|
||||
|
||||
def test_bootstrap(self):
|
||||
"""
|
||||
Test the `ipalib.plugable.API.bootstrap` method.
|
||||
"""
|
||||
o = self.cls()
|
||||
assert o.isdone('bootstrap') is False
|
||||
o.bootstrap()
|
||||
assert o.isdone('bootstrap') is True
|
||||
e = raises(StandardError, o.bootstrap)
|
||||
assert str(e) == 'API.bootstrap() already called'
|
||||
|
Loading…
Reference in New Issue
Block a user