Debian Codegraph @

zaqarclient/transport/__init__.py

raw
1 # Copyright (c) 2013 Red Hat, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import urllib.parse
17
18 from stevedore import driver
19
20 from zaqarclient import errors as _errors
21
22
23 def get_transport(transport='http', version=2, options=None):
24 """Gets a transport and returns it.
25
26 :param transport: Transport name.
27 Default: http
28 :type transport: str
29 :param version: Version of the target transport.
30 Default: 2
31 :type version: int
32
33 :returns: A `Transport` instance.
34 :rtype: `zaqarclient.transport.Transport`
35 """
36
37 entry_point = '{}.v{}'.format(transport, version)
38
39 try:
40 namespace = 'zaqarclient.transport'
41 mgr = driver.DriverManager(namespace,
42 entry_point,
43 invoke_on_load=True,
44 invoke_args=[options])
45 except RuntimeError as ex:
46 raise _errors.DriverLoadFailure(entry_point, ex)
47
48 return mgr.driver
49
50
51 def get_transport_for(url_or_request, version=2, options=None):
52 """Gets a transport for a given url.
53
54 An example transport URL might be::
55
56 zmq://example.org:8888/v2/
57
58 :param url_or_request: a transport URL
59 :type url_or_request: str or
60 `zaqarclient.transport.request.Request`
61 :param version: Version of the target transport.
62 Default: 2
63 :type version: int
64
65 :returns: A `Transport` instance.
66 :rtype: `zaqarclient.transport.Transport`
67 """
68
69 url = url_or_request
70 if not isinstance(url_or_request, str):
71 url = url_or_request.endpoint
72
73 parsed = urllib.parse.urlparse(url)
74 return get_transport(parsed.scheme, version, options)