add DriverManager
This commit is contained in:
parent
b3293b9d92
commit
f0da043cb8
@ -1,3 +1,5 @@
|
||||
from .extension import ExtensionManager
|
||||
from .enabled import EnabledExtensionManager
|
||||
from .named import NamedExtensionManager
|
||||
from .hook import HookManager
|
||||
from .driver import DriverManager
|
||||
|
24
stevedore/driver.py
Normal file
24
stevedore/driver.py
Normal file
@ -0,0 +1,24 @@
|
||||
from .hook import HookManager
|
||||
|
||||
|
||||
class DriverManager(HookManager):
|
||||
"""Load a single plugin with a given name from the namespace.
|
||||
"""
|
||||
|
||||
def __init__(self, namespace, name,
|
||||
invoke_on_load=False, invoke_args=(), invoke_kwds={}):
|
||||
super(HookManager, self).__init__(
|
||||
namespace,
|
||||
[name],
|
||||
invoke_on_load=invoke_on_load,
|
||||
invoke_args=invoke_args,
|
||||
invoke_kwds=invoke_kwds,
|
||||
)
|
||||
if not self.extensions:
|
||||
raise RuntimeError('No %r driver found' % namespace)
|
||||
if len(self.extensions) > 1:
|
||||
raise RuntimeError('Multiple %r drivers found: %s' %
|
||||
(namespace,
|
||||
','.join('%s:%s' % (e.module_name, e.attrs[0])
|
||||
for e in self.extensions))
|
||||
)
|
17
stevedore/tests/test_driver.py
Normal file
17
stevedore/tests/test_driver.py
Normal file
@ -0,0 +1,17 @@
|
||||
"""Tests for stevedore.extension
|
||||
"""
|
||||
|
||||
from stevedore import driver
|
||||
|
||||
|
||||
def test_detect_plugins():
|
||||
em = driver.DriverManager('stevedore.test.extension', 't1')
|
||||
names = sorted(em.names())
|
||||
assert names == ['t1']
|
||||
|
||||
|
||||
def test_no_drivers():
|
||||
try:
|
||||
driver.DriverManager('stevedore.test.extension.none', 't1')
|
||||
except RuntimeError as err:
|
||||
assert "No 'stevedore.test.extension.none' driver found" == str(err)
|
Loading…
Reference in New Issue
Block a user