Use shared util helper for driver name + config extraction

Change-Id: I43465b8f5868e64bdf38d2873417a8a4a403a23b
This commit is contained in:
Joshua Harlow
2015-12-22 17:15:32 -08:00
parent 25a8e4b132
commit c145bef224
3 changed files with 20 additions and 27 deletions

View File

@@ -16,7 +16,6 @@
import contextlib import contextlib
import six
from stevedore import driver from stevedore import driver
from taskflow import exceptions as exc from taskflow import exceptions as exc
@@ -51,16 +50,7 @@ def fetch(name, conf, namespace=BACKEND_NAMESPACE, **kwargs):
is ``{'a': 'b', 'c': 'd'}`` to the constructor of that board is ``{'a': 'b', 'c': 'd'}`` to the constructor of that board
instance (also including the name specified). instance (also including the name specified).
""" """
if isinstance(conf, six.string_types): board, conf = misc.extract_driver_and_conf(conf, 'board')
conf = {'board': conf}
board = conf['board']
try:
uri = misc.parse_uri(board)
except (TypeError, ValueError):
pass
else:
board = uri.scheme
conf = misc.merge_uri(uri, conf.copy())
LOG.debug('Looking for %r jobboard driver in %r', board, namespace) LOG.debug('Looking for %r jobboard driver in %r', board, namespace)
try: try:
mgr = driver.DriverManager(namespace, board, mgr = driver.DriverManager(namespace, board,

View File

@@ -16,7 +16,6 @@
import contextlib import contextlib
import six
from stevedore import driver from stevedore import driver
from taskflow import exceptions as exc from taskflow import exceptions as exc
@@ -51,30 +50,21 @@ def fetch(conf, namespace=BACKEND_NAMESPACE, **kwargs):
is ``{'a': 'b', 'c': 'd'}`` to the constructor of that persistence backend is ``{'a': 'b', 'c': 'd'}`` to the constructor of that persistence backend
instance. instance.
""" """
if isinstance(conf, six.string_types): backend, conf = misc.extract_driver_and_conf(conf, 'connection')
conf = {'connection': conf}
backend_name = conf['connection']
try:
uri = misc.parse_uri(backend_name)
except (TypeError, ValueError):
pass
else:
backend_name = uri.scheme
conf = misc.merge_uri(uri, conf.copy())
# If the backend is like 'mysql+pymysql://...' which informs the # If the backend is like 'mysql+pymysql://...' which informs the
# backend to use a dialect (supported by sqlalchemy at least) we just want # backend to use a dialect (supported by sqlalchemy at least) we just want
# to look at the first component to find our entrypoint backend name... # to look at the first component to find our entrypoint backend name...
if backend_name.find("+") != -1: if backend.find("+") != -1:
backend_name = backend_name.split("+", 1)[0] backend = backend.split("+", 1)[0]
LOG.debug('Looking for %r backend driver in %r', backend_name, namespace) LOG.debug('Looking for %r backend driver in %r', backend, namespace)
try: try:
mgr = driver.DriverManager(namespace, backend_name, mgr = driver.DriverManager(namespace, backend,
invoke_on_load=True, invoke_on_load=True,
invoke_args=(conf,), invoke_args=(conf,),
invoke_kwds=kwargs) invoke_kwds=kwargs)
return mgr.driver return mgr.driver
except RuntimeError as e: except RuntimeError as e:
raise exc.NotFound("Could not find backend %s: %s" % (backend_name, e)) raise exc.NotFound("Could not find backend %s: %s" % (backend, e))
@contextlib.contextmanager @contextlib.contextmanager

View File

@@ -112,6 +112,19 @@ def countdown_iter(start_at, decr=1):
start_at -= decr start_at -= decr
def extract_driver_and_conf(conf, conf_key):
"""Common function to get a driver name and its configuration."""
if isinstance(conf, six.string_types):
conf = {conf_key: conf}
maybe_uri = conf[conf_key]
try:
uri = parse_uri(maybe_uri)
except (TypeError, ValueError):
return (maybe_uri, conf)
else:
return (uri.scheme, merge_uri(uri, conf.copy()))
def reverse_enumerate(items): def reverse_enumerate(items):
"""Like reversed(enumerate(items)) but with less copying/cloning...""" """Like reversed(enumerate(items)) but with less copying/cloning..."""
for i in countdown_iter(len(items)): for i in countdown_iter(len(items)):