Browse Source

Merge "Use oslo.utils"

Jenkins 4 years ago
parent
commit
7377b42482

+ 2
- 2
novaclient/client.py View File

@@ -31,6 +31,7 @@ import re
31 31
 import time
32 32
 
33 33
 from keystoneclient import adapter
34
+from oslo.utils import netutils
34 35
 import requests
35 36
 from requests import adapters
36 37
 
@@ -43,7 +44,6 @@ from six.moves.urllib import parse
43 44
 
44 45
 from novaclient import exceptions
45 46
 from novaclient.openstack.common.gettextutils import _
46
-from novaclient.openstack.common import network_utils
47 47
 from novaclient import service_catalog
48 48
 from novaclient import utils
49 49
 
@@ -558,7 +558,7 @@ class HTTPClient(object):
558 558
                                              extract_token=False)
559 559
 
560 560
     def authenticate(self):
561
-        magic_tuple = network_utils.urlsplit(self.auth_url)
561
+        magic_tuple = netutils.urlsplit(self.auth_url)
562 562
         scheme, netloc, path, query, frag = magic_tuple
563 563
         port = magic_tuple.port
564 564
         if port is None:

+ 0
- 108
novaclient/openstack/common/network_utils.py View File

@@ -1,108 +0,0 @@
1
-# Copyright 2012 OpenStack Foundation.
2
-# All Rights Reserved.
3
-#
4
-#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5
-#    not use this file except in compliance with the License. You may obtain
6
-#    a copy of the License at
7
-#
8
-#         http://www.apache.org/licenses/LICENSE-2.0
9
-#
10
-#    Unless required by applicable law or agreed to in writing, software
11
-#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
-#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
-#    License for the specific language governing permissions and limitations
14
-#    under the License.
15
-
16
-"""
17
-Network-related utilities and helper functions.
18
-"""
19
-
20
-# TODO(jd) Use six.moves once
21
-# https://bitbucket.org/gutworth/six/pull-request/28
22
-# is merged
23
-try:
24
-    import urllib.parse
25
-    SplitResult = urllib.parse.SplitResult
26
-except ImportError:
27
-    import urlparse
28
-    SplitResult = urlparse.SplitResult
29
-
30
-from six.moves.urllib import parse
31
-
32
-
33
-def parse_host_port(address, default_port=None):
34
-    """Interpret a string as a host:port pair.
35
-
36
-    An IPv6 address MUST be escaped if accompanied by a port,
37
-    because otherwise ambiguity ensues: 2001:db8:85a3::8a2e:370:7334
38
-    means both [2001:db8:85a3::8a2e:370:7334] and
39
-    [2001:db8:85a3::8a2e:370]:7334.
40
-
41
-    >>> parse_host_port('server01:80')
42
-    ('server01', 80)
43
-    >>> parse_host_port('server01')
44
-    ('server01', None)
45
-    >>> parse_host_port('server01', default_port=1234)
46
-    ('server01', 1234)
47
-    >>> parse_host_port('[::1]:80')
48
-    ('::1', 80)
49
-    >>> parse_host_port('[::1]')
50
-    ('::1', None)
51
-    >>> parse_host_port('[::1]', default_port=1234)
52
-    ('::1', 1234)
53
-    >>> parse_host_port('2001:db8:85a3::8a2e:370:7334', default_port=1234)
54
-    ('2001:db8:85a3::8a2e:370:7334', 1234)
55
-
56
-    """
57
-    if address[0] == '[':
58
-        # Escaped ipv6
59
-        _host, _port = address[1:].split(']')
60
-        host = _host
61
-        if ':' in _port:
62
-            port = _port.split(':')[1]
63
-        else:
64
-            port = default_port
65
-    else:
66
-        if address.count(':') == 1:
67
-            host, port = address.split(':')
68
-        else:
69
-            # 0 means ipv4, >1 means ipv6.
70
-            # We prohibit unescaped ipv6 addresses with port.
71
-            host = address
72
-            port = default_port
73
-
74
-    return (host, None if port is None else int(port))
75
-
76
-
77
-class ModifiedSplitResult(SplitResult):
78
-    """Split results class for urlsplit."""
79
-
80
-    # NOTE(dims): The functions below are needed for Python 2.6.x.
81
-    # We can remove these when we drop support for 2.6.x.
82
-    @property
83
-    def hostname(self):
84
-        netloc = self.netloc.split('@', 1)[-1]
85
-        host, port = parse_host_port(netloc)
86
-        return host
87
-
88
-    @property
89
-    def port(self):
90
-        netloc = self.netloc.split('@', 1)[-1]
91
-        host, port = parse_host_port(netloc)
92
-        return port
93
-
94
-
95
-def urlsplit(url, scheme='', allow_fragments=True):
96
-    """Parse a URL using urlparse.urlsplit(), splitting query and fragments.
97
-    This function papers over Python issue9374 when needed.
98
-
99
-    The parameters are the same as urlparse.urlsplit.
100
-    """
101
-    scheme, netloc, path, query, fragment = parse.urlsplit(
102
-        url, scheme, allow_fragments)
103
-    if allow_fragments and '#' in path:
104
-        path, fragment = path.split('#', 1)
105
-    if '?' in path:
106
-        path, query = path.split('?', 1)
107
-    return ModifiedSplitResult(scheme, netloc,
108
-                               path, query, fragment)

+ 5
- 4
novaclient/shell.py View File

@@ -29,6 +29,8 @@ import os
29 29
 import pkgutil
30 30
 import sys
31 31
 
32
+from oslo.utils import encodeutils
33
+from oslo.utils import strutils
32 34
 import pkg_resources
33 35
 import six
34 36
 
@@ -47,7 +49,6 @@ from novaclient import exceptions as exc
47 49
 import novaclient.extension
48 50
 from novaclient.openstack.common import cliutils
49 51
 from novaclient.openstack.common.gettextutils import _
50
-from novaclient.openstack.common import strutils
51 52
 from novaclient import utils
52 53
 from novaclient.v1_1 import shell as shell_v1_1
53 54
 from novaclient.v3 import shell as shell_v3
@@ -795,13 +796,13 @@ class OpenStackHelpFormatter(argparse.HelpFormatter):
795 796
 
796 797
 def main():
797 798
     try:
798
-        argv = [strutils.safe_decode(a) for a in sys.argv[1:]]
799
+        argv = [encodeutils.safe_decode(a) for a in sys.argv[1:]]
799 800
         OpenStackComputeShell().main(argv)
800 801
 
801 802
     except Exception as e:
802 803
         logger.debug(e, exc_info=1)
803
-        details = {'name': strutils.safe_encode(e.__class__.__name__),
804
-                   'msg': strutils.safe_encode(six.text_type(e))}
804
+        details = {'name': encodeutils.safe_encode(e.__class__.__name__),
805
+                   'msg': encodeutils.safe_encode(six.text_type(e))}
805 806
         print("ERROR (%(name)s): %(msg)s" % details,
806 807
               file=sys.stderr)
807 808
         sys.exit(1)

+ 1
- 1
novaclient/tests/v1_1/fakes.py View File

@@ -16,12 +16,12 @@
16 16
 
17 17
 import datetime
18 18
 
19
+from oslo.utils import strutils
19 20
 import six
20 21
 from six.moves.urllib import parse
21 22
 
22 23
 from novaclient import client as base_client
23 24
 from novaclient import exceptions
24
-from novaclient.openstack.common import strutils
25 25
 from novaclient.tests import fakes
26 26
 from novaclient.tests import utils
27 27
 from novaclient.v1_1 import client

+ 4
- 3
novaclient/tests/v1_1/test_shell.py View File

@@ -22,6 +22,7 @@ import os
22 22
 
23 23
 import fixtures
24 24
 import mock
25
+from oslo.utils import timeutils
25 26
 import six
26 27
 from six.moves import builtins
27 28
 
@@ -1232,9 +1233,9 @@ class ShellTest(utils.TestCase):
1232 1233
                            'end=2005-02-01T00:00:00&' +
1233 1234
                            'detailed=1')
1234 1235
 
1235
-    @mock.patch('novaclient.openstack.common.timeutils.utcnow')
1236
-    def test_usage_list_no_args(self, mock_utcnow):
1237
-        mock_utcnow.return_value = datetime.datetime(2005, 2, 1, 0, 0)
1236
+    def test_usage_list_no_args(self):
1237
+        timeutils.set_time_override(datetime.datetime(2005, 2, 1, 0, 0))
1238
+        self.addCleanup(timeutils.clear_time_override)
1238 1239
         self.run_command('usage-list')
1239 1240
         self.assert_called('GET',
1240 1241
                            '/os-simple-tenant-usage?' +

+ 2
- 1
novaclient/tests/v3/fakes.py View File

@@ -16,7 +16,8 @@
16 16
 
17 17
 import datetime
18 18
 
19
-from novaclient.openstack.common import strutils
19
+from oslo.utils import strutils
20
+
20 21
 from novaclient.tests import fakes
21 22
 from novaclient.tests.v1_1 import fakes as fakes_v1_1
22 23
 from novaclient.v3 import client

+ 15
- 5
novaclient/utils.py View File

@@ -17,6 +17,7 @@ import sys
17 17
 import textwrap
18 18
 import uuid
19 19
 
20
+from oslo.utils import encodeutils
20 21
 import pkg_resources
21 22
 import prettytable
22 23
 import six
@@ -25,7 +26,6 @@ from novaclient import exceptions
25 26
 from novaclient.openstack.common import cliutils
26 27
 from novaclient.openstack.common.gettextutils import _
27 28
 from novaclient.openstack.common import jsonutils
28
-from novaclient.openstack.common import strutils
29 29
 
30 30
 
31 31
 arg = cliutils.arg
@@ -127,9 +127,12 @@ def print_list(objs, fields, formatters={}, sortby_index=None):
127 127
         pt.add_row(row)
128 128
 
129 129
     if sortby is not None:
130
-        result = strutils.safe_encode(pt.get_string(sortby=sortby))
130
+        result = encodeutils.safe_encode(pt.get_string(sortby=sortby))
131 131
     else:
132
-        result = strutils.safe_encode(pt.get_string())
132
+        result = encodeutils.safe_encode(pt.get_string())
133
+
134
+    if six.PY3:
135
+        result = result.decode()
133 136
 
134 137
     print(result)
135 138
 
@@ -197,7 +200,10 @@ def print_dict(d, dict_property="Property", dict_value="Value", wrap=0):
197 200
                 v = '-'
198 201
             pt.add_row([k, v])
199 202
 
200
-    result = strutils.safe_encode(pt.get_string())
203
+    result = encodeutils.safe_encode(pt.get_string())
204
+
205
+    if six.PY3:
206
+        result = result.decode()
201 207
 
202 208
     print(result)
203 209
 
@@ -219,7 +225,11 @@ def find_resource(manager, name_or_id, **find_args):
219 225
 
220 226
     # now try to get entity as uuid
221 227
     try:
222
-        tmp_id = strutils.safe_encode(name_or_id)
228
+        tmp_id = encodeutils.safe_encode(name_or_id)
229
+
230
+        if six.PY3:
231
+            tmp_id = tmp_id.decode()
232
+
223 233
         uuid.UUID(tmp_id)
224 234
         return manager.get(tmp_id)
225 235
     except (TypeError, ValueError, exceptions.NotFound):

+ 1
- 1
novaclient/v1_1/flavors.py View File

@@ -16,12 +16,12 @@
16 16
 Flavor interface.
17 17
 """
18 18
 
19
+from oslo.utils import strutils
19 20
 from six.moves.urllib import parse
20 21
 
21 22
 from novaclient import base
22 23
 from novaclient import exceptions
23 24
 from novaclient.openstack.common.gettextutils import _
24
-from novaclient.openstack.common import strutils
25 25
 from novaclient import utils
26 26
 
27 27
 

+ 2
- 2
novaclient/v1_1/servers.py View File

@@ -21,13 +21,13 @@ Server interface.
21 21
 
22 22
 import base64
23 23
 
24
+from oslo.utils import encodeutils
24 25
 import six
25 26
 from six.moves.urllib import parse
26 27
 
27 28
 from novaclient import base
28 29
 from novaclient import crypto
29 30
 from novaclient.openstack.common.gettextutils import _
30
-from novaclient.openstack.common import strutils
31 31
 from novaclient.v1_1 import security_groups
32 32
 
33 33
 
@@ -454,7 +454,7 @@ class ServerManager(base.BootingManagerWithFind):
454 454
             if six.PY3:
455 455
                 userdata = userdata.encode("utf-8")
456 456
             else:
457
-                userdata = strutils.safe_encode(userdata)
457
+                userdata = encodeutils.safe_encode(userdata)
458 458
 
459 459
             userdata_b64 = base64.b64encode(userdata).decode('utf-8')
460 460
             body["server"]["user_data"] = userdata_b64

+ 4
- 3
novaclient/v1_1/shell.py View File

@@ -28,12 +28,13 @@ import os
28 28
 import sys
29 29
 import time
30 30
 
31
+from oslo.utils import encodeutils
32
+from oslo.utils import strutils
33
+from oslo.utils import timeutils
31 34
 import six
32 35
 
33 36
 from novaclient import exceptions
34 37
 from novaclient.openstack.common.gettextutils import _
35
-from novaclient.openstack.common import strutils
36
-from novaclient.openstack.common import timeutils
37 38
 from novaclient.openstack.common import uuidutils
38 39
 from novaclient import utils
39 40
 from novaclient.v1_1 import availability_zones
@@ -2303,7 +2304,7 @@ def _print_secgroups(secgroups):
2303 2304
 
2304 2305
 def _get_secgroup(cs, secgroup):
2305 2306
     # Check secgroup is an ID (nova-network) or UUID (neutron)
2306
-    if (utils.is_integer_like(strutils.safe_encode(secgroup))
2307
+    if (utils.is_integer_like(encodeutils.safe_encode(secgroup))
2307 2308
                or uuidutils.is_uuid_like(secgroup)):
2308 2309
         try:
2309 2310
             return cs.security_groups.get(secgroup)

+ 3
- 2
novaclient/v3/images.py View File

@@ -17,10 +17,11 @@
17 17
 Image interface.
18 18
 """
19 19
 
20
+from oslo.utils import encodeutils
21
+from oslo.utils import strutils
20 22
 from six.moves.urllib import parse
21 23
 
22 24
 from novaclient import base
23
-from novaclient.openstack.common import strutils
24 25
 
25 26
 
26 27
 class Image(base.Resource):
@@ -51,7 +52,7 @@ class ImageManager(base.ManagerWithFind):
51 52
 
52 53
     def _image_meta_from_headers(self, headers):
53 54
         meta = {'properties': {}}
54
-        safe_decode = strutils.safe_decode
55
+        safe_decode = encodeutils.safe_decode
55 56
         for key, value in headers.items():
56 57
             value = safe_decode(value, incoming='utf-8')
57 58
             if key.startswith('x-image-meta-property-'):

+ 2
- 2
novaclient/v3/servers.py View File

@@ -21,13 +21,13 @@ Server interface.
21 21
 
22 22
 import base64
23 23
 
24
+from oslo.utils import encodeutils
24 25
 import six
25 26
 from six.moves.urllib import parse
26 27
 
27 28
 from novaclient import base
28 29
 from novaclient import crypto
29 30
 from novaclient.openstack.common.gettextutils import _
30
-from novaclient.openstack.common import strutils
31 31
 
32 32
 REBOOT_SOFT, REBOOT_HARD = 'SOFT', 'HARD'
33 33
 
@@ -402,7 +402,7 @@ class ServerManager(base.BootingManagerWithFind):
402 402
             if six.PY3:
403 403
                 userdata = userdata.encode("utf-8")
404 404
             else:
405
-                userdata = strutils.safe_encode(userdata)
405
+                userdata = encodeutils.safe_encode(userdata)
406 406
 
407 407
             data = base64.b64encode(userdata).decode('utf-8')
408 408
             body["server"]["os-user-data:user_data"] = data

+ 2
- 2
novaclient/v3/shell.py View File

@@ -27,12 +27,12 @@ import os
27 27
 import sys
28 28
 import time
29 29
 
30
+from oslo.utils import strutils
31
+from oslo.utils import timeutils
30 32
 import six
31 33
 
32 34
 from novaclient import exceptions
33 35
 from novaclient.openstack.common.gettextutils import _
34
-from novaclient.openstack.common import strutils
35
-from novaclient.openstack.common import timeutils
36 36
 from novaclient.openstack.common import uuidutils
37 37
 from novaclient import utils
38 38
 from novaclient.v3 import availability_zones

+ 0
- 4
openstack-common.conf View File

@@ -5,11 +5,7 @@ module=apiclient
5 5
 module=cliutils
6 6
 module=gettextutils
7 7
 module=install_venv_common
8
-module=importutils
9 8
 module=jsonutils
10
-module=network_utils
11
-module=strutils
12
-module=timeutils
13 9
 module=uuidutils
14 10
 
15 11
 # The base module to hold the copy of openstack.common

+ 1
- 0
requirements.txt View File

@@ -1,6 +1,7 @@
1 1
 pbr>=0.6,!=0.7,<1.0
2 2
 argparse
3 3
 iso8601>=0.1.9
4
+oslo.utils>=0.2.0       # Apache-2.0
4 5
 PrettyTable>=0.7,<0.8
5 6
 requests>=1.2.1
6 7
 simplejson>=2.2.0

Loading…
Cancel
Save