Split get_transport into 2 different functions
This patch splits get_transport into:
- get_transport: Basic transport loading that does not depend
on a request.
- get_transport_for: Transport loading based on a request object.
Partially-Implements blueprint python-marconiclient-v1
Change-Id: Id1adfb3fc3d2914bf81e4b204ca3eae82a42b94d
This commit is contained in:
@@ -19,5 +19,15 @@ class MarconiError(Exception):
|
|||||||
"""Base class for errors."""
|
"""Base class for errors."""
|
||||||
|
|
||||||
|
|
||||||
|
class DriverLoadFailure(MarconiError):
|
||||||
|
"""Raised if a transport driver can't be loaded."""
|
||||||
|
|
||||||
|
def __init__(self, driver, ex):
|
||||||
|
msg = 'Failed to load transport driver "%s": %s' % (driver, ex)
|
||||||
|
super(DriverLoadFailure, self).__init__(msg)
|
||||||
|
self.driver = driver
|
||||||
|
self.ex = ex
|
||||||
|
|
||||||
|
|
||||||
class InvalidOperation(MarconiError):
|
class InvalidOperation(MarconiError):
|
||||||
"""Raised when attempted a non existent operation."""
|
"""Raised when attempted a non existent operation."""
|
||||||
|
|||||||
@@ -20,17 +20,35 @@ from stevedore import driver
|
|||||||
from marconiclient import errors
|
from marconiclient import errors
|
||||||
|
|
||||||
|
|
||||||
class DriverLoadFailure(errors.MarconiError):
|
def get_transport(conf, transport, version=1):
|
||||||
"""Raised if a transport driver can't be loaded."""
|
"""Gets a transport and returns it.
|
||||||
|
|
||||||
def __init__(self, driver, ex):
|
:param conf: the user configuration
|
||||||
msg = 'Failed to load transport driver "%s": %s' % (driver, ex)
|
:type conf: cfg.ConfigOpts
|
||||||
super(DriverLoadFailure, self).__init__(msg)
|
:param transport: Transport name
|
||||||
self.driver = driver
|
:type transport: `six.string_types`
|
||||||
self.ex = ex
|
:param version: Version of the target transport.
|
||||||
|
:type version: int
|
||||||
|
|
||||||
|
:returns: A `Transport` instance.
|
||||||
|
:rtype: `marconiclient.transport.Transport`
|
||||||
|
"""
|
||||||
|
|
||||||
|
entry_point = '{0}.v{1}'.format(transport, version)
|
||||||
|
|
||||||
|
try:
|
||||||
|
namespace = 'marconiclient.transport'
|
||||||
|
mgr = driver.DriverManager(namespace,
|
||||||
|
entry_point,
|
||||||
|
invoke_on_load=True,
|
||||||
|
invoke_args=[conf])
|
||||||
|
except RuntimeError as ex:
|
||||||
|
raise errors.DriverLoadFailure(entry_point, ex)
|
||||||
|
|
||||||
|
return mgr.driver
|
||||||
|
|
||||||
|
|
||||||
def get_transport(conf, url_or_request, module='queues'):
|
def get_transport_for(conf, url_or_request, version=1):
|
||||||
"""Gets a transport for a given url.
|
"""Gets a transport for a given url.
|
||||||
|
|
||||||
An example transport URL might be::
|
An example transport URL might be::
|
||||||
@@ -42,8 +60,8 @@ def get_transport(conf, url_or_request, module='queues'):
|
|||||||
:param url_or_request: a transport URL
|
:param url_or_request: a transport URL
|
||||||
:type url_or_request: `six.string_types` or
|
:type url_or_request: `six.string_types` or
|
||||||
`marconiclient.transport.request.Request`
|
`marconiclient.transport.request.Request`
|
||||||
:param module: Module the target transport belongs to.
|
:param version: Version of the target transport.
|
||||||
:type module: str
|
:type version: int
|
||||||
|
|
||||||
:returns: A `Transport` instance.
|
:returns: A `Transport` instance.
|
||||||
:rtype: `marconiclient.transport.Transport`
|
:rtype: `marconiclient.transport.Transport`
|
||||||
@@ -54,14 +72,4 @@ def get_transport(conf, url_or_request, module='queues'):
|
|||||||
url = url_or_request.endpoint
|
url = url_or_request.endpoint
|
||||||
|
|
||||||
parsed = parse.urlparse(url)
|
parsed = parse.urlparse(url)
|
||||||
|
return get_transport(conf, parsed.scheme, version)
|
||||||
try:
|
|
||||||
namespace = 'marconiclient.{0}.transport'.format(module)
|
|
||||||
mgr = driver.DriverManager(namespace,
|
|
||||||
parsed.scheme,
|
|
||||||
invoke_on_load=True,
|
|
||||||
invoke_args=[conf])
|
|
||||||
except RuntimeError as ex:
|
|
||||||
raise DriverLoadFailure(parsed.scheme, ex)
|
|
||||||
|
|
||||||
return mgr.driver
|
|
||||||
|
|||||||
Reference in New Issue
Block a user