Leverage openstack.common.jsonutils
This patch removes our own home brewed json helper functions and leverages the ones from openstack.common.jsonutils instead. Change-Id: Idc5ee0fec56b0a0f337433a830647d4f421a8283 Closes-bug: 1365263
This commit is contained in:
@@ -17,14 +17,10 @@
|
||||
|
||||
"""Utilities and helper functions."""
|
||||
|
||||
import datetime
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
import six
|
||||
|
||||
from neutronclient.common import _
|
||||
from neutronclient.common import exceptions
|
||||
from neutronclient.openstack.common import strutils
|
||||
@@ -42,39 +38,6 @@ def env(*vars, **kwargs):
|
||||
return kwargs.get('default', '')
|
||||
|
||||
|
||||
def to_primitive(value):
|
||||
if isinstance(value, list) or isinstance(value, tuple):
|
||||
o = []
|
||||
for v in value:
|
||||
o.append(to_primitive(v))
|
||||
return o
|
||||
elif isinstance(value, dict):
|
||||
o = {}
|
||||
for k, v in six.iteritems(value):
|
||||
o[k] = to_primitive(v)
|
||||
return o
|
||||
elif isinstance(value, datetime.datetime):
|
||||
return str(value)
|
||||
elif hasattr(value, 'iteritems'):
|
||||
return to_primitive(dict(value.iteritems()))
|
||||
elif hasattr(value, '__iter__'):
|
||||
return to_primitive(list(value))
|
||||
else:
|
||||
return value
|
||||
|
||||
|
||||
def dumps(value, indent=None):
|
||||
try:
|
||||
return json.dumps(value, indent=indent)
|
||||
except TypeError:
|
||||
pass
|
||||
return json.dumps(to_primitive(value))
|
||||
|
||||
|
||||
def loads(s):
|
||||
return json.loads(s)
|
||||
|
||||
|
||||
def import_class(import_str):
|
||||
"""Returns a class from a string including module and class.
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ from neutronclient.common import command
|
||||
from neutronclient.common import exceptions
|
||||
from neutronclient.common import utils
|
||||
from neutronclient.openstack.common.gettextutils import _
|
||||
from neutronclient.openstack.common import jsonutils
|
||||
|
||||
HEX_ELEM = '[0-9A-Fa-f]'
|
||||
UUID_PATTERN = '-'.join([HEX_ELEM + '{8}', HEX_ELEM + '{4}',
|
||||
@@ -416,12 +417,12 @@ class NeutronCommand(command.OpenStackCommand):
|
||||
if self.resource in data:
|
||||
for k, v in six.iteritems(data[self.resource]):
|
||||
if isinstance(v, list):
|
||||
value = '\n'.join(utils.dumps(
|
||||
value = '\n'.join(jsonutils.dumps(
|
||||
i, indent=self.json_indent) if isinstance(i, dict)
|
||||
else str(i) for i in v)
|
||||
data[self.resource][k] = value
|
||||
elif isinstance(v, dict):
|
||||
value = utils.dumps(v, indent=self.json_indent)
|
||||
value = jsonutils.dumps(v, indent=self.json_indent)
|
||||
data[self.resource][k] = value
|
||||
elif v is None:
|
||||
data[self.resource][k] = ''
|
||||
|
||||
@@ -20,11 +20,12 @@ from neutronclient.common import exceptions
|
||||
from neutronclient.common import utils
|
||||
from neutronclient.neutron import v2_0 as neutronV20
|
||||
from neutronclient.openstack.common.gettextutils import _
|
||||
from neutronclient.openstack.common import jsonutils
|
||||
|
||||
|
||||
def _format_fixed_ips(port):
|
||||
try:
|
||||
return '\n'.join([utils.dumps(ip) for ip in port['fixed_ips']])
|
||||
return '\n'.join([jsonutils.dumps(ip) for ip in port['fixed_ips']])
|
||||
except Exception:
|
||||
return ''
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ from neutronclient.common import exceptions
|
||||
from neutronclient.common import utils
|
||||
from neutronclient.neutron import v2_0 as neutronV20
|
||||
from neutronclient.openstack.common.gettextutils import _
|
||||
from neutronclient.openstack.common import jsonutils
|
||||
|
||||
|
||||
def get_tenant_id(tenant_id, client):
|
||||
@@ -128,7 +129,7 @@ class ShowQuota(neutronV20.NeutronCommand, show.ShowOne):
|
||||
if value:
|
||||
value += "\n"
|
||||
if isinstance(_item, dict):
|
||||
value += utils.dumps(_item)
|
||||
value += jsonutils.dumps(_item)
|
||||
else:
|
||||
value += str(_item)
|
||||
data[self.resource][k] = value
|
||||
@@ -233,7 +234,7 @@ class UpdateQuota(neutronV20.NeutronCommand, show.ShowOne):
|
||||
if value:
|
||||
value += "\n"
|
||||
if isinstance(_item, dict):
|
||||
value += utils.dumps(_item)
|
||||
value += jsonutils.dumps(_item)
|
||||
else:
|
||||
value += str(_item)
|
||||
data[self.resource][k] = value
|
||||
|
||||
@@ -20,11 +20,12 @@ from neutronclient.common import exceptions
|
||||
from neutronclient.common import utils
|
||||
from neutronclient.neutron import v2_0 as neutronV20
|
||||
from neutronclient.openstack.common.gettextutils import _
|
||||
from neutronclient.openstack.common import jsonutils
|
||||
|
||||
|
||||
def _format_allocation_pools(subnet):
|
||||
try:
|
||||
return '\n'.join([utils.dumps(pool) for pool in
|
||||
return '\n'.join([jsonutils.dumps(pool) for pool in
|
||||
subnet['allocation_pools']])
|
||||
except Exception:
|
||||
return ''
|
||||
@@ -32,7 +33,7 @@ def _format_allocation_pools(subnet):
|
||||
|
||||
def _format_dns_nameservers(subnet):
|
||||
try:
|
||||
return '\n'.join([utils.dumps(server) for server in
|
||||
return '\n'.join([jsonutils.dumps(server) for server in
|
||||
subnet['dns_nameservers']])
|
||||
except Exception:
|
||||
return ''
|
||||
@@ -40,7 +41,7 @@ def _format_dns_nameservers(subnet):
|
||||
|
||||
def _format_host_routes(subnet):
|
||||
try:
|
||||
return '\n'.join([utils.dumps(route) for route in
|
||||
return '\n'.join([jsonutils.dumps(route) for route in
|
||||
subnet['host_routes']])
|
||||
except Exception:
|
||||
return ''
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
|
||||
import sys
|
||||
|
||||
from neutronclient.common import utils
|
||||
from neutronclient.neutron.v2_0 import agent
|
||||
from neutronclient.openstack.common import jsonutils
|
||||
from neutronclient.tests.unit import test_cli20
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ class CLITestV20Agent(test_cli20.CLITestV20Base):
|
||||
self._test_list_columns(cmd, resources, contents, args)
|
||||
_str = self.fake_stdout.make_string()
|
||||
|
||||
returned_agents = utils.loads(_str)
|
||||
returned_agents = jsonutils.loads(_str)
|
||||
self.assertEqual(1, len(returned_agents))
|
||||
ag = returned_agents[0]
|
||||
self.assertEqual(3, len(ag))
|
||||
@@ -48,7 +48,7 @@ class CLITestV20Agent(test_cli20.CLITestV20Base):
|
||||
self._test_list_columns(cmd, resources, contents, args)
|
||||
_str = self.fake_stdout.make_string()
|
||||
|
||||
returned_agents = utils.loads(_str)
|
||||
returned_agents = jsonutils.loads(_str)
|
||||
self.assertEqual(1, len(returned_agents))
|
||||
ag = returned_agents[0]
|
||||
self.assertEqual(1, len(ag))
|
||||
|
||||
@@ -19,8 +19,8 @@ import sys
|
||||
from mox3 import mox
|
||||
|
||||
from neutronclient.common import exceptions
|
||||
from neutronclient.common import utils
|
||||
from neutronclient.neutron.v2_0 import network
|
||||
from neutronclient.openstack.common import jsonutils
|
||||
from neutronclient import shell
|
||||
from neutronclient.tests.unit import test_cli20
|
||||
|
||||
@@ -282,7 +282,7 @@ class CLITestV20NetworkJSON(test_cli20.CLITestV20Base):
|
||||
self._test_list_nets_columns(cmd, returned_body,
|
||||
args=['-f', 'json', '-c', 'id'])
|
||||
_str = self.fake_stdout.make_string()
|
||||
returned_networks = utils.loads(_str)
|
||||
returned_networks = jsonutils.loads(_str)
|
||||
self.assertEqual(1, len(returned_networks))
|
||||
net = returned_networks[0]
|
||||
self.assertEqual(1, len(net))
|
||||
@@ -296,7 +296,7 @@ class CLITestV20NetworkJSON(test_cli20.CLITestV20Base):
|
||||
"subnets": []}]}
|
||||
self._test_list_nets_columns(cmd, returned_body)
|
||||
_str = self.fake_stdout.make_string()
|
||||
returned_networks = utils.loads(_str)
|
||||
returned_networks = jsonutils.loads(_str)
|
||||
self.assertEqual(1, len(returned_networks))
|
||||
net = returned_networks[0]
|
||||
self.assertEqual(3, len(net))
|
||||
|
||||
@@ -13,10 +13,8 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import datetime
|
||||
import sys
|
||||
|
||||
import six
|
||||
import testtools
|
||||
|
||||
from neutronclient.common import exceptions
|
||||
@@ -107,81 +105,6 @@ class TestUtils(testtools.TestCase):
|
||||
self.assertEqual(('test_name', 'test_id', 'test', 'pass'), act)
|
||||
|
||||
|
||||
class JSONUtilsTestCase(testtools.TestCase):
|
||||
def test_dumps(self):
|
||||
self.assertEqual(utils.dumps({'a': 'b'}), '{"a": "b"}')
|
||||
|
||||
def test_dumps_dict_with_date_value(self):
|
||||
x = datetime.datetime(1920, 2, 3, 4, 5, 6, 7)
|
||||
res = utils.dumps({1: 'a', 2: x})
|
||||
expected = '{"1": "a", "2": "1920-02-03 04:05:06.000007"}'
|
||||
self.assertEqual(expected, res)
|
||||
|
||||
def test_dumps_dict_with_spaces(self):
|
||||
x = datetime.datetime(1920, 2, 3, 4, 5, 6, 7)
|
||||
res = utils.dumps({1: 'a ', 2: x})
|
||||
expected = '{"1": "a ", "2": "1920-02-03 04:05:06.000007"}'
|
||||
self.assertEqual(expected, res)
|
||||
|
||||
def test_loads(self):
|
||||
self.assertEqual(utils.loads('{"a": "b"}'), {'a': 'b'})
|
||||
|
||||
|
||||
class ToPrimitiveTestCase(testtools.TestCase):
|
||||
def test_list(self):
|
||||
self.assertEqual(utils.to_primitive([1, 2, 3]), [1, 2, 3])
|
||||
|
||||
def test_empty_list(self):
|
||||
self.assertEqual(utils.to_primitive([]), [])
|
||||
|
||||
def test_tuple(self):
|
||||
self.assertEqual(utils.to_primitive((1, 2, 3)), [1, 2, 3])
|
||||
|
||||
def test_empty_tuple(self):
|
||||
self.assertEqual(utils.to_primitive(()), [])
|
||||
|
||||
def test_dict(self):
|
||||
self.assertEqual(
|
||||
utils.to_primitive(dict(a=1, b=2, c=3)),
|
||||
dict(a=1, b=2, c=3))
|
||||
|
||||
def test_empty_dict(self):
|
||||
self.assertEqual(utils.to_primitive({}), {})
|
||||
|
||||
def test_datetime(self):
|
||||
x = datetime.datetime(1920, 2, 3, 4, 5, 6, 7)
|
||||
self.assertEqual(
|
||||
utils.to_primitive(x),
|
||||
'1920-02-03 04:05:06.000007')
|
||||
|
||||
def test_iter(self):
|
||||
x = range(1, 6)
|
||||
self.assertEqual(utils.to_primitive(x), [1, 2, 3, 4, 5])
|
||||
|
||||
def test_iteritems(self):
|
||||
d = {'a': 1, 'b': 2, 'c': 3}
|
||||
|
||||
class IterItemsClass(object):
|
||||
def iteritems(self):
|
||||
return six.iteritems(d)
|
||||
|
||||
x = IterItemsClass()
|
||||
p = utils.to_primitive(x)
|
||||
self.assertEqual(p, {'a': 1, 'b': 2, 'c': 3})
|
||||
|
||||
def test_nasties(self):
|
||||
def foo():
|
||||
pass
|
||||
x = [datetime, foo, dir]
|
||||
ret = utils.to_primitive(x)
|
||||
self.assertEqual(len(ret), 3)
|
||||
|
||||
def test_to_primitive_dict_with_date_value(self):
|
||||
x = datetime.datetime(1920, 2, 3, 4, 5, 6, 7)
|
||||
res = utils.to_primitive({'a': x})
|
||||
self.assertEqual({'a': '1920-02-03 04:05:06.000007'}, res)
|
||||
|
||||
|
||||
class ImportClassTestCase(testtools.TestCase):
|
||||
def test_import_class(self):
|
||||
dt = utils.import_class('datetime.datetime')
|
||||
|
||||
Reference in New Issue
Block a user