add DriverManager

This commit is contained in:
Doug Hellmann 2012-06-19 16:14:31 -04:00
parent b3293b9d92
commit f0da043cb8
3 changed files with 43 additions and 0 deletions

View File

@ -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
View 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))
)

View 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)