Browse Source

Use oslo.serialization

Since module `jsonutils` from common code is graduated in
`oslo.serialization`, we should remove code from oslo-incubator and use this
library.

Change-Id: Ia8b5ef598ff415cdde19f523a36a552918f9f94b
Andrey Kurilin 4 years ago
parent
commit
8c0fd9a674

+ 0
- 186
novaclient/openstack/common/jsonutils.py View File

@@ -1,186 +0,0 @@
1
-# Copyright 2010 United States Government as represented by the
2
-# Administrator of the National Aeronautics and Space Administration.
3
-# Copyright 2011 Justin Santa Barbara
4
-# All Rights Reserved.
5
-#
6
-#    Licensed under the Apache License, Version 2.0 (the "License"); you may
7
-#    not use this file except in compliance with the License. You may obtain
8
-#    a copy of the License at
9
-#
10
-#         http://www.apache.org/licenses/LICENSE-2.0
11
-#
12
-#    Unless required by applicable law or agreed to in writing, software
13
-#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
-#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
-#    License for the specific language governing permissions and limitations
16
-#    under the License.
17
-
18
-'''
19
-JSON related utilities.
20
-
21
-This module provides a few things:
22
-
23
-    1) A handy function for getting an object down to something that can be
24
-    JSON serialized.  See to_primitive().
25
-
26
-    2) Wrappers around loads() and dumps().  The dumps() wrapper will
27
-    automatically use to_primitive() for you if needed.
28
-
29
-    3) This sets up anyjson to use the loads() and dumps() wrappers if anyjson
30
-    is available.
31
-'''
32
-
33
-
34
-import codecs
35
-import datetime
36
-import functools
37
-import inspect
38
-import itertools
39
-import sys
40
-
41
-if sys.version_info < (2, 7):
42
-    # On Python <= 2.6, json module is not C boosted, so try to use
43
-    # simplejson module if available
44
-    try:
45
-        import simplejson as json
46
-    except ImportError:
47
-        import json
48
-else:
49
-    import json
50
-
51
-import six
52
-import six.moves.xmlrpc_client as xmlrpclib
53
-
54
-from novaclient.openstack.common import gettextutils
55
-from novaclient.openstack.common import importutils
56
-from novaclient.openstack.common import strutils
57
-from novaclient.openstack.common import timeutils
58
-
59
-netaddr = importutils.try_import("netaddr")
60
-
61
-_nasty_type_tests = [inspect.ismodule, inspect.isclass, inspect.ismethod,
62
-                     inspect.isfunction, inspect.isgeneratorfunction,
63
-                     inspect.isgenerator, inspect.istraceback, inspect.isframe,
64
-                     inspect.iscode, inspect.isbuiltin, inspect.isroutine,
65
-                     inspect.isabstract]
66
-
67
-_simple_types = (six.string_types + six.integer_types
68
-                 + (type(None), bool, float))
69
-
70
-
71
-def to_primitive(value, convert_instances=False, convert_datetime=True,
72
-                 level=0, max_depth=3):
73
-    """Convert a complex object into primitives.
74
-
75
-    Handy for JSON serialization. We can optionally handle instances,
76
-    but since this is a recursive function, we could have cyclical
77
-    data structures.
78
-
79
-    To handle cyclical data structures we could track the actual objects
80
-    visited in a set, but not all objects are hashable. Instead we just
81
-    track the depth of the object inspections and don't go too deep.
82
-
83
-    Therefore, convert_instances=True is lossy ... be aware.
84
-
85
-    """
86
-    # handle obvious types first - order of basic types determined by running
87
-    # full tests on nova project, resulting in the following counts:
88
-    # 572754 <type 'NoneType'>
89
-    # 460353 <type 'int'>
90
-    # 379632 <type 'unicode'>
91
-    # 274610 <type 'str'>
92
-    # 199918 <type 'dict'>
93
-    # 114200 <type 'datetime.datetime'>
94
-    #  51817 <type 'bool'>
95
-    #  26164 <type 'list'>
96
-    #   6491 <type 'float'>
97
-    #    283 <type 'tuple'>
98
-    #     19 <type 'long'>
99
-    if isinstance(value, _simple_types):
100
-        return value
101
-
102
-    if isinstance(value, datetime.datetime):
103
-        if convert_datetime:
104
-            return timeutils.strtime(value)
105
-        else:
106
-            return value
107
-
108
-    # value of itertools.count doesn't get caught by nasty_type_tests
109
-    # and results in infinite loop when list(value) is called.
110
-    if type(value) == itertools.count:
111
-        return six.text_type(value)
112
-
113
-    # FIXME(vish): Workaround for LP bug 852095. Without this workaround,
114
-    #              tests that raise an exception in a mocked method that
115
-    #              has a @wrap_exception with a notifier will fail. If
116
-    #              we up the dependency to 0.5.4 (when it is released) we
117
-    #              can remove this workaround.
118
-    if getattr(value, '__module__', None) == 'mox':
119
-        return 'mock'
120
-
121
-    if level > max_depth:
122
-        return '?'
123
-
124
-    # The try block may not be necessary after the class check above,
125
-    # but just in case ...
126
-    try:
127
-        recursive = functools.partial(to_primitive,
128
-                                      convert_instances=convert_instances,
129
-                                      convert_datetime=convert_datetime,
130
-                                      level=level,
131
-                                      max_depth=max_depth)
132
-        if isinstance(value, dict):
133
-            return dict((k, recursive(v)) for k, v in six.iteritems(value))
134
-        elif isinstance(value, (list, tuple)):
135
-            return [recursive(lv) for lv in value]
136
-
137
-        # It's not clear why xmlrpclib created their own DateTime type, but
138
-        # for our purposes, make it a datetime type which is explicitly
139
-        # handled
140
-        if isinstance(value, xmlrpclib.DateTime):
141
-            value = datetime.datetime(*tuple(value.timetuple())[:6])
142
-
143
-        if convert_datetime and isinstance(value, datetime.datetime):
144
-            return timeutils.strtime(value)
145
-        elif isinstance(value, gettextutils.Message):
146
-            return value.data
147
-        elif hasattr(value, 'iteritems'):
148
-            return recursive(dict(value.iteritems()), level=level + 1)
149
-        elif hasattr(value, '__iter__'):
150
-            return recursive(list(value))
151
-        elif convert_instances and hasattr(value, '__dict__'):
152
-            # Likely an instance of something. Watch for cycles.
153
-            # Ignore class member vars.
154
-            return recursive(value.__dict__, level=level + 1)
155
-        elif netaddr and isinstance(value, netaddr.IPAddress):
156
-            return six.text_type(value)
157
-        else:
158
-            if any(test(value) for test in _nasty_type_tests):
159
-                return six.text_type(value)
160
-            return value
161
-    except TypeError:
162
-        # Class objects are tricky since they may define something like
163
-        # __iter__ defined but it isn't callable as list().
164
-        return six.text_type(value)
165
-
166
-
167
-def dumps(value, default=to_primitive, **kwargs):
168
-    return json.dumps(value, default=default, **kwargs)
169
-
170
-
171
-def loads(s, encoding='utf-8'):
172
-    return json.loads(strutils.safe_decode(s, encoding))
173
-
174
-
175
-def load(fp, encoding='utf-8'):
176
-    return json.load(codecs.getreader(encoding)(fp))
177
-
178
-
179
-try:
180
-    import anyjson
181
-except ImportError:
182
-    pass
183
-else:
184
-    anyjson._modules.append((__name__, 'dumps', TypeError,
185
-                                       'loads', ValueError, 'load'))
186
-    anyjson.force_implementation(__name__)

+ 0
- 210
novaclient/openstack/common/timeutils.py View File

@@ -1,210 +0,0 @@
1
-# Copyright 2011 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
-Time related utilities and helper functions.
18
-"""
19
-
20
-import calendar
21
-import datetime
22
-import time
23
-
24
-import iso8601
25
-import six
26
-
27
-
28
-# ISO 8601 extended time format with microseconds
29
-_ISO8601_TIME_FORMAT_SUBSECOND = '%Y-%m-%dT%H:%M:%S.%f'
30
-_ISO8601_TIME_FORMAT = '%Y-%m-%dT%H:%M:%S'
31
-PERFECT_TIME_FORMAT = _ISO8601_TIME_FORMAT_SUBSECOND
32
-
33
-
34
-def isotime(at=None, subsecond=False):
35
-    """Stringify time in ISO 8601 format."""
36
-    if not at:
37
-        at = utcnow()
38
-    st = at.strftime(_ISO8601_TIME_FORMAT
39
-                     if not subsecond
40
-                     else _ISO8601_TIME_FORMAT_SUBSECOND)
41
-    tz = at.tzinfo.tzname(None) if at.tzinfo else 'UTC'
42
-    st += ('Z' if tz == 'UTC' else tz)
43
-    return st
44
-
45
-
46
-def parse_isotime(timestr):
47
-    """Parse time from ISO 8601 format."""
48
-    try:
49
-        return iso8601.parse_date(timestr)
50
-    except iso8601.ParseError as e:
51
-        raise ValueError(six.text_type(e))
52
-    except TypeError as e:
53
-        raise ValueError(six.text_type(e))
54
-
55
-
56
-def strtime(at=None, fmt=PERFECT_TIME_FORMAT):
57
-    """Returns formatted utcnow."""
58
-    if not at:
59
-        at = utcnow()
60
-    return at.strftime(fmt)
61
-
62
-
63
-def parse_strtime(timestr, fmt=PERFECT_TIME_FORMAT):
64
-    """Turn a formatted time back into a datetime."""
65
-    return datetime.datetime.strptime(timestr, fmt)
66
-
67
-
68
-def normalize_time(timestamp):
69
-    """Normalize time in arbitrary timezone to UTC naive object."""
70
-    offset = timestamp.utcoffset()
71
-    if offset is None:
72
-        return timestamp
73
-    return timestamp.replace(tzinfo=None) - offset
74
-
75
-
76
-def is_older_than(before, seconds):
77
-    """Return True if before is older than seconds."""
78
-    if isinstance(before, six.string_types):
79
-        before = parse_strtime(before).replace(tzinfo=None)
80
-    else:
81
-        before = before.replace(tzinfo=None)
82
-
83
-    return utcnow() - before > datetime.timedelta(seconds=seconds)
84
-
85
-
86
-def is_newer_than(after, seconds):
87
-    """Return True if after is newer than seconds."""
88
-    if isinstance(after, six.string_types):
89
-        after = parse_strtime(after).replace(tzinfo=None)
90
-    else:
91
-        after = after.replace(tzinfo=None)
92
-
93
-    return after - utcnow() > datetime.timedelta(seconds=seconds)
94
-
95
-
96
-def utcnow_ts():
97
-    """Timestamp version of our utcnow function."""
98
-    if utcnow.override_time is None:
99
-        # NOTE(kgriffs): This is several times faster
100
-        # than going through calendar.timegm(...)
101
-        return int(time.time())
102
-
103
-    return calendar.timegm(utcnow().timetuple())
104
-
105
-
106
-def utcnow():
107
-    """Overridable version of utils.utcnow."""
108
-    if utcnow.override_time:
109
-        try:
110
-            return utcnow.override_time.pop(0)
111
-        except AttributeError:
112
-            return utcnow.override_time
113
-    return datetime.datetime.utcnow()
114
-
115
-
116
-def iso8601_from_timestamp(timestamp):
117
-    """Returns a iso8601 formatted date from timestamp."""
118
-    return isotime(datetime.datetime.utcfromtimestamp(timestamp))
119
-
120
-
121
-utcnow.override_time = None
122
-
123
-
124
-def set_time_override(override_time=None):
125
-    """Overrides utils.utcnow.
126
-
127
-    Make it return a constant time or a list thereof, one at a time.
128
-
129
-    :param override_time: datetime instance or list thereof. If not
130
-                          given, defaults to the current UTC time.
131
-    """
132
-    utcnow.override_time = override_time or datetime.datetime.utcnow()
133
-
134
-
135
-def advance_time_delta(timedelta):
136
-    """Advance overridden time using a datetime.timedelta."""
137
-    assert(not utcnow.override_time is None)
138
-    try:
139
-        for dt in utcnow.override_time:
140
-            dt += timedelta
141
-    except TypeError:
142
-        utcnow.override_time += timedelta
143
-
144
-
145
-def advance_time_seconds(seconds):
146
-    """Advance overridden time by seconds."""
147
-    advance_time_delta(datetime.timedelta(0, seconds))
148
-
149
-
150
-def clear_time_override():
151
-    """Remove the overridden time."""
152
-    utcnow.override_time = None
153
-
154
-
155
-def marshall_now(now=None):
156
-    """Make an rpc-safe datetime with microseconds.
157
-
158
-    Note: tzinfo is stripped, but not required for relative times.
159
-    """
160
-    if not now:
161
-        now = utcnow()
162
-    return dict(day=now.day, month=now.month, year=now.year, hour=now.hour,
163
-                minute=now.minute, second=now.second,
164
-                microsecond=now.microsecond)
165
-
166
-
167
-def unmarshall_time(tyme):
168
-    """Unmarshall a datetime dict."""
169
-    return datetime.datetime(day=tyme['day'],
170
-                             month=tyme['month'],
171
-                             year=tyme['year'],
172
-                             hour=tyme['hour'],
173
-                             minute=tyme['minute'],
174
-                             second=tyme['second'],
175
-                             microsecond=tyme['microsecond'])
176
-
177
-
178
-def delta_seconds(before, after):
179
-    """Return the difference between two timing objects.
180
-
181
-    Compute the difference in seconds between two date, time, or
182
-    datetime objects (as a float, to microsecond resolution).
183
-    """
184
-    delta = after - before
185
-    return total_seconds(delta)
186
-
187
-
188
-def total_seconds(delta):
189
-    """Return the total seconds of datetime.timedelta object.
190
-
191
-    Compute total seconds of datetime.timedelta, datetime.timedelta
192
-    doesn't have method total_seconds in Python2.6, calculate it manually.
193
-    """
194
-    try:
195
-        return delta.total_seconds()
196
-    except AttributeError:
197
-        return ((delta.days * 24 * 3600) + delta.seconds +
198
-                float(delta.microseconds) / (10 ** 6))
199
-
200
-
201
-def is_soon(dt, window):
202
-    """Determines if time is going to happen in the next window seconds.
203
-
204
-    :param dt: the time
205
-    :param window: minimum seconds to remain to consider the time not soon
206
-
207
-    :return: True if expiration is within the given duration
208
-    """
209
-    soon = (utcnow() + datetime.timedelta(seconds=window))
210
-    return normalize_time(dt) <= soon

+ 2
- 1
novaclient/tests/fixture_data/floatingips.py View File

@@ -10,7 +10,8 @@
10 10
 # License for the specific language governing permissions and limitations
11 11
 # under the License.
12 12
 
13
-from novaclient.openstack.common import jsonutils
13
+from oslo.serialization import jsonutils
14
+
14 15
 from novaclient.tests import fakes
15 16
 from novaclient.tests.fixture_data import base
16 17
 

+ 1
- 1
novaclient/tests/fixture_data/hosts.py View File

@@ -10,9 +10,9 @@
10 10
 # License for the specific language governing permissions and limitations
11 11
 # under the License.
12 12
 
13
+from oslo.serialization import jsonutils
13 14
 from six.moves.urllib import parse
14 15
 
15
-from novaclient.openstack.common import jsonutils
16 16
 from novaclient.tests.fixture_data import base
17 17
 
18 18
 

+ 2
- 1
novaclient/tests/fixture_data/images.py View File

@@ -10,7 +10,8 @@
10 10
 # License for the specific language governing permissions and limitations
11 11
 # under the License.
12 12
 
13
-from novaclient.openstack.common import jsonutils
13
+from oslo.serialization import jsonutils
14
+
14 15
 from novaclient.tests import fakes
15 16
 from novaclient.tests.fixture_data import base
16 17
 

+ 2
- 1
novaclient/tests/fixture_data/keypairs.py View File

@@ -10,7 +10,8 @@
10 10
 # License for the specific language governing permissions and limitations
11 11
 # under the License.
12 12
 
13
-from novaclient.openstack.common import jsonutils
13
+from oslo.serialization import jsonutils
14
+
14 15
 from novaclient.tests import fakes
15 16
 from novaclient.tests.fixture_data import base
16 17
 

+ 2
- 1
novaclient/tests/fixture_data/networks.py View File

@@ -10,7 +10,8 @@
10 10
 # License for the specific language governing permissions and limitations
11 11
 # under the License.
12 12
 
13
-from novaclient.openstack.common import jsonutils
13
+from oslo.serialization import jsonutils
14
+
14 15
 from novaclient.tests.fixture_data import base
15 16
 
16 17
 

+ 2
- 1
novaclient/tests/fixture_data/security_group_rules.py View File

@@ -10,7 +10,8 @@
10 10
 # License for the specific language governing permissions and limitations
11 11
 # under the License.
12 12
 
13
-from novaclient.openstack.common import jsonutils
13
+from oslo.serialization import jsonutils
14
+
14 15
 from novaclient.tests import fakes
15 16
 from novaclient.tests.fixture_data import base
16 17
 

+ 2
- 1
novaclient/tests/fixture_data/security_groups.py View File

@@ -10,7 +10,8 @@
10 10
 # License for the specific language governing permissions and limitations
11 11
 # under the License.
12 12
 
13
-from novaclient.openstack.common import jsonutils
13
+from oslo.serialization import jsonutils
14
+
14 15
 from novaclient.tests import fakes
15 16
 from novaclient.tests.fixture_data import base
16 17
 

+ 2
- 1
novaclient/tests/fixture_data/server_groups.py View File

@@ -10,7 +10,8 @@
10 10
 # License for the specific language governing permissions and limitations
11 11
 # under the License.
12 12
 
13
-from novaclient.openstack.common import jsonutils
13
+from oslo.serialization import jsonutils
14
+
14 15
 from novaclient.tests.fixture_data import base
15 16
 
16 17
 

+ 2
- 1
novaclient/tests/fixture_data/servers.py View File

@@ -10,7 +10,8 @@
10 10
 # License for the specific language governing permissions and limitations
11 11
 # under the License.
12 12
 
13
-from novaclient.openstack.common import jsonutils
13
+from oslo.serialization import jsonutils
14
+
14 15
 from novaclient.tests import fakes
15 16
 from novaclient.tests.fixture_data import base
16 17
 

+ 1
- 2
novaclient/tests/utils.py View File

@@ -14,14 +14,13 @@
14 14
 import os
15 15
 
16 16
 import fixtures
17
+from oslo.serialization import jsonutils
17 18
 import requests
18 19
 from requests_mock.contrib import fixture as requests_mock_fixture
19 20
 import six
20 21
 import testscenarios
21 22
 import testtools
22 23
 
23
-from novaclient.openstack.common import jsonutils
24
-
25 24
 AUTH_URL = "http://localhost:5002/auth_url"
26 25
 AUTH_URL_V1 = "http://localhost:5002/auth_url/v1.0"
27 26
 AUTH_URL_V2 = "http://localhost:5002/auth_url/v2.0"

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

@@ -13,10 +13,10 @@
13 13
 #    under the License.
14 14
 
15 15
 import mock
16
+from oslo.serialization import jsonutils
16 17
 import six
17 18
 
18 19
 from novaclient import exceptions
19
-from novaclient.openstack.common import jsonutils
20 20
 from novaclient.tests.fixture_data import client
21 21
 from novaclient.tests.fixture_data import floatingips
22 22
 from novaclient.tests.fixture_data import servers as data

+ 1
- 1
novaclient/utils.py View File

@@ -17,6 +17,7 @@ import sys
17 17
 import textwrap
18 18
 import uuid
19 19
 
20
+from oslo.serialization import jsonutils
20 21
 from oslo.utils import encodeutils
21 22
 import pkg_resources
22 23
 import prettytable
@@ -25,7 +26,6 @@ import six
25 26
 from novaclient import exceptions
26 27
 from novaclient.openstack.common import cliutils
27 28
 from novaclient.openstack.common.gettextutils import _
28
-from novaclient.openstack.common import jsonutils
29 29
 
30 30
 
31 31
 arg = cliutils.arg

+ 0
- 1
openstack-common.conf View File

@@ -5,7 +5,6 @@ module=apiclient
5 5
 module=cliutils
6 6
 module=gettextutils
7 7
 module=install_venv_common
8
-module=jsonutils
9 8
 module=uuidutils
10 9
 
11 10
 # The base module to hold the copy of openstack.common

+ 1
- 0
requirements.txt View File

@@ -4,6 +4,7 @@
4 4
 pbr>=0.6,!=0.7,<1.0
5 5
 argparse
6 6
 iso8601>=0.1.9
7
+oslo.serialization>=1.0.0               # Apache-2.0
7 8
 oslo.utils>=1.0.0                       # Apache-2.0
8 9
 PrettyTable>=0.7,<0.8
9 10
 requests>=1.2.1,!=2.4.0

Loading…
Cancel
Save