From f0da043cb82ad73b4f0c057549f97045b35d2710 Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Tue, 19 Jun 2012 16:14:31 -0400 Subject: [PATCH] add DriverManager --- stevedore/__init__.py | 2 ++ stevedore/driver.py | 24 ++++++++++++++++++++++++ stevedore/tests/test_driver.py | 17 +++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 stevedore/driver.py create mode 100644 stevedore/tests/test_driver.py diff --git a/stevedore/__init__.py b/stevedore/__init__.py index 958ab7e..9b19761 100644 --- a/stevedore/__init__.py +++ b/stevedore/__init__.py @@ -1,3 +1,5 @@ from .extension import ExtensionManager from .enabled import EnabledExtensionManager from .named import NamedExtensionManager +from .hook import HookManager +from .driver import DriverManager diff --git a/stevedore/driver.py b/stevedore/driver.py new file mode 100644 index 0000000..9c0effc --- /dev/null +++ b/stevedore/driver.py @@ -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)) + ) diff --git a/stevedore/tests/test_driver.py b/stevedore/tests/test_driver.py new file mode 100644 index 0000000..984eaac --- /dev/null +++ b/stevedore/tests/test_driver.py @@ -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)