diff options
author | Andrew Bogott <abogott@wikimedia.org> | 2012-07-03 00:16:36 -0500 |
---|---|---|
committer | Andrew Bogott <abogott@wikimedia.org> | 2012-07-03 00:48:52 -0500 |
commit | 85f6662f25e9d2696ec6086a1423c1b93a58cb0c (patch) | |
tree | 6ffafa3ec536a70aa76267dead8891f5406fc4dc | |
parent | 506486550480fb222a92447e1e8baca855d126aa (diff) | |
download | oslo-85f6662f25e9d2696ec6086a1423c1b93a58cb0c.tar.gz oslo-85f6662f25e9d2696ec6086a1423c1b93a58cb0c.tar.xz oslo-85f6662f25e9d2696ec6086a1423c1b93a58cb0c.zip |
Switch common files to using jsonutils.
For blueprint use-common-jsonutils
Pretty much just a search and replace.
Change-Id: I66f63e7b0ff82a37d8a6b9acef94f4e10a45bf90
-rw-r--r-- | openstack/common/log.py | 4 | ||||
-rw-r--r-- | openstack/common/notifier/log_notifier.py | 4 | ||||
-rw-r--r-- | openstack/common/policy.py | 9 | ||||
-rw-r--r-- | openstack/common/rpc/impl_fake.py | 4 | ||||
-rw-r--r-- | openstack/common/rpc/impl_qpid.py | 6 | ||||
-rw-r--r-- | openstack/common/rpc/impl_zmq.py | 6 | ||||
-rw-r--r-- | openstack/common/wsgi.py | 6 | ||||
-rw-r--r-- | tests/unit/extensions/foxinsocks.py | 10 | ||||
-rw-r--r-- | tests/unit/rpc/test_common.py | 14 | ||||
-rw-r--r-- | tests/unit/test_extensions.py | 43 | ||||
-rw-r--r-- | tests/unit/test_log.py | 6 | ||||
-rw-r--r-- | tests/unit/test_policy.py | 4 |
12 files changed, 59 insertions, 57 deletions
diff --git a/openstack/common/log.py b/openstack/common/log.py index 9d8bddf..348f3ab 100644 --- a/openstack/common/log.py +++ b/openstack/common/log.py @@ -32,7 +32,6 @@ It also allows setting of formatting information through conf. import cStringIO import inspect import itertools -import json import logging import logging.config import logging.handlers @@ -42,6 +41,7 @@ import sys import traceback from openstack.common import cfg +from openstack.common import jsonutils from openstack.common import local from openstack.common import notifier @@ -241,7 +241,7 @@ class JSONFormatter(logging.Formatter): if record.exc_info: message['traceback'] = self.formatException(record.exc_info) - return json.dumps(message) + return jsonutils.dumps(message) class PublishErrorsHandler(logging.Handler): diff --git a/openstack/common/notifier/log_notifier.py b/openstack/common/notifier/log_notifier.py index 594fd3e..3b025f0 100644 --- a/openstack/common/notifier/log_notifier.py +++ b/openstack/common/notifier/log_notifier.py @@ -13,9 +13,9 @@ # License for the specific language governing permissions and limitations # under the License. -import json from openstack.common import cfg +from openstack.common import jsonutils from openstack.common import log as logging @@ -31,4 +31,4 @@ def notify(_context, message): priority = priority.lower() logger = logging.getLogger( 'openstack.common.notification.%s' % message['event_type']) - getattr(logger, priority)(json.dumps(message)) + getattr(logger, priority)(jsonutils.dumps(message)) diff --git a/openstack/common/policy.py b/openstack/common/policy.py index 203995a..52cfa2b 100644 --- a/openstack/common/policy.py +++ b/openstack/common/policy.py @@ -17,11 +17,12 @@ """Common Policy Engine Implementation""" -import json import logging import urllib import urllib2 +from openstack.common import jsonutils + LOG = logging.getLogger(__name__) @@ -132,7 +133,7 @@ class Brain(object): @classmethod def load_json(cls, data, default_rule=None): """Init a brain using json instead of a rules dictionary.""" - rules_dict = json.loads(data) + rules_dict = jsonutils.loads(data) return cls(rules=rules_dict, default_rule=default_rule) def __init__(self, rules=None, default_rule=None): @@ -231,8 +232,8 @@ class HttpBrain(Brain): """ url = match % target_dict - data = {'target': json.dumps(target_dict), - 'credentials': json.dumps(cred_dict)} + data = {'target': jsonutils.dumps(target_dict), + 'credentials': jsonutils.dumps(cred_dict)} post_data = urllib.urlencode(data) f = urllib2.urlopen(url, post_data) return f.read() == "True" diff --git a/openstack/common/rpc/impl_fake.py b/openstack/common/rpc/impl_fake.py index fba20c9..ff16208 100644 --- a/openstack/common/rpc/impl_fake.py +++ b/openstack/common/rpc/impl_fake.py @@ -18,11 +18,11 @@ queues. Casts will block, but this is very useful for tests. """ import inspect -import json import time import eventlet +from openstack.common import jsonutils from openstack.common.rpc import common as rpc_common CONSUMERS = {} @@ -121,7 +121,7 @@ def create_connection(conf, new=True): def check_serialize(msg): """Make sure a message intended for rpc can be serialized.""" - json.dumps(msg) + jsonutils.dumps(msg) def multicall(conf, context, topic, msg, timeout=None): diff --git a/openstack/common/rpc/impl_qpid.py b/openstack/common/rpc/impl_qpid.py index d01bf0d..677b54b 100644 --- a/openstack/common/rpc/impl_qpid.py +++ b/openstack/common/rpc/impl_qpid.py @@ -17,7 +17,6 @@ import functools import itertools -import json import logging import time import uuid @@ -29,6 +28,7 @@ import qpid.messaging.exceptions from openstack.common import cfg from openstack.common.gettextutils import _ +from openstack.common import jsonutils from openstack.common.rpc import amqp as rpc_amqp from openstack.common.rpc import common as rpc_common @@ -125,7 +125,7 @@ class ConsumerBase(object): addr_opts["node"]["x-declare"].update(node_opts) addr_opts["link"]["x-declare"].update(link_opts) - self.address = "%s ; %s" % (node_name, json.dumps(addr_opts)) + self.address = "%s ; %s" % (node_name, jsonutils.dumps(addr_opts)) self.reconnect(session) @@ -230,7 +230,7 @@ class Publisher(object): if node_opts: addr_opts["node"]["x-declare"].update(node_opts) - self.address = "%s ; %s" % (node_name, json.dumps(addr_opts)) + self.address = "%s ; %s" % (node_name, jsonutils.dumps(addr_opts)) self.reconnect(session) diff --git a/openstack/common/rpc/impl_zmq.py b/openstack/common/rpc/impl_zmq.py index 6bb9aa9..3eefa96 100644 --- a/openstack/common/rpc/impl_zmq.py +++ b/openstack/common/rpc/impl_zmq.py @@ -14,7 +14,6 @@ # License for the specific language governing permissions and limitations # under the License. -import json import pprint import string import sys @@ -28,6 +27,7 @@ import greenlet from openstack.common import cfg from openstack.common.gettextutils import _ from openstack.common import importutils +from openstack.common import jsonutils from openstack.common.rpc import common as rpc_common @@ -76,7 +76,7 @@ def _serialize(data): Error if a developer passes us bad data. """ try: - return str(json.dumps(data, ensure_ascii=True)) + return str(jsonutils.dumps(data, ensure_ascii=True)) except TypeError: LOG.error(_("JSON serialization failed.")) raise @@ -87,7 +87,7 @@ def _deserialize(data): Deserialization wrapper """ LOG.debug(_("Deserializing: %s"), data) - return json.loads(data) + return jsonutils.loads(data) class ZmqSocket(object): diff --git a/openstack/common/wsgi.py b/openstack/common/wsgi.py index c08e4d7..416fe09 100644 --- a/openstack/common/wsgi.py +++ b/openstack/common/wsgi.py @@ -23,7 +23,6 @@ import eventlet.wsgi eventlet.patcher.monkey_patch(all=False, socket=True) -import json import logging import sys import routes @@ -34,6 +33,7 @@ from xml.dom import minidom from xml.parsers import expat from openstack.common import exception +from openstack.common import jsonutils LOG = logging.getLogger('wsgi') @@ -372,7 +372,7 @@ class JSONDictSerializer(DictSerializer): _dtime = obj - datetime.timedelta(microseconds=obj.microsecond) return _dtime.isoformat() return obj - return json.dumps(data, default=sanitizer) + return jsonutils.dumps(data, default=sanitizer) class XMLDictSerializer(DictSerializer): @@ -641,7 +641,7 @@ class JSONDeserializer(TextDeserializer): def _from_json(self, datastring): try: - return json.loads(datastring) + return jsonutils.loads(datastring) except ValueError: msg = _("cannot understand JSON") raise exception.MalformedRequestBody(reason=msg) diff --git a/tests/unit/extensions/foxinsocks.py b/tests/unit/extensions/foxinsocks.py index 2291a9f..bb1eb5c 100644 --- a/tests/unit/extensions/foxinsocks.py +++ b/tests/unit/extensions/foxinsocks.py @@ -15,9 +15,9 @@ # License for the specific language governing permissions and limitations # under the License. -import json from openstack.common import extensions +from openstack.common import jsonutils class FoxInSocksController(object): @@ -67,9 +67,9 @@ class Foxinsocks(object): def _goose_handler(req, res): #NOTE: This only handles JSON responses. # You can use content type header to test for XML. - data = json.loads(res.body) + data = jsonutils.loads(res.body) data['FOXNSOX:googoose'] = req.GET.get('chewing') - res.body = json.dumps(data) + res.body = jsonutils.dumps(data) return res req_ext1 = extensions.RequestExtension('GET', '/dummy_resources/:(id)', @@ -79,9 +79,9 @@ class Foxinsocks(object): def _bands_handler(req, res): #NOTE: This only handles JSON responses. # You can use content type header to test for XML. - data = json.loads(res.body) + data = jsonutils.loads(res.body) data['FOXNSOX:big_bands'] = 'Pig Bands!' - res.body = json.dumps(data) + res.body = jsonutils.dumps(data) return res req_ext2 = extensions.RequestExtension('GET', '/dummy_resources/:(id)', diff --git a/tests/unit/rpc/test_common.py b/tests/unit/rpc/test_common.py index 0b282eb..8755b75 100644 --- a/tests/unit/rpc/test_common.py +++ b/tests/unit/rpc/test_common.py @@ -17,7 +17,6 @@ Unit Tests for 'common' functons used through rpc code. """ -import json import logging import sys import unittest @@ -26,6 +25,7 @@ from openstack.common import cfg from openstack.common import context from openstack.common import exception from openstack.common import importutils +from openstack.common import jsonutils from openstack.common import rpc from openstack.common.rpc import amqp as rpc_amqp from openstack.common.rpc import common as rpc_common @@ -58,7 +58,7 @@ class RpcCommonTestCase(unittest.TestCase): except Exception as exc: failure = rpc_common.serialize_remote_exception(sys.exc_info()) - failure = json.loads(failure) + failure = jsonutils.loads(failure) #assure the traceback was added self.assertEqual(expected['class'], failure['class']) self.assertEqual(expected['module'], failure['module']) @@ -79,7 +79,7 @@ class RpcCommonTestCase(unittest.TestCase): except Exception as exc: failure = rpc_common.serialize_remote_exception(sys.exc_info()) - failure = json.loads(failure) + failure = jsonutils.loads(failure) #assure the traceback was added self.assertEqual(expected['class'], failure['class']) self.assertEqual(expected['module'], failure['module']) @@ -92,7 +92,7 @@ class RpcCommonTestCase(unittest.TestCase): 'message': exception.OpenstackException.message, 'tb': ['raise OpenstackException'], } - serialized = json.dumps(failure) + serialized = jsonutils.dumps(failure) after_exc = rpc_common.deserialize_remote_exception(FLAGS, serialized) self.assertTrue(isinstance(after_exc, exception.OpenstackException)) @@ -107,7 +107,7 @@ class RpcCommonTestCase(unittest.TestCase): 'kwargs': {'cmd': '/bin/echo failed'}, 'message': 'foo', } - serialized = json.dumps(failure) + serialized = jsonutils.dumps(failure) after_exc = rpc_common.deserialize_remote_exception(FLAGS, serialized) self.assertTrue(isinstance(after_exc, rpc_common.RemoteError)) @@ -121,7 +121,7 @@ class RpcCommonTestCase(unittest.TestCase): 'module': self.__class__.__module__, 'tb': ['raise FakeUserDefinedException'], } - serialized = json.dumps(failure) + serialized = jsonutils.dumps(failure) after_exc = rpc_common.deserialize_remote_exception(FLAGS, serialized) self.assertTrue(isinstance(after_exc, FakeUserDefinedException)) @@ -143,7 +143,7 @@ class RpcCommonTestCase(unittest.TestCase): 'module': self.__class__.__module__, 'tb': ['raise FakeIDontExistException'], } - serialized = json.dumps(failure) + serialized = jsonutils.dumps(failure) after_exc = rpc_common.deserialize_remote_exception(FLAGS, serialized) self.assertTrue(isinstance(after_exc, rpc_common.RemoteError)) diff --git a/tests/unit/test_extensions.py b/tests/unit/test_extensions.py index f3c067b..78cf368 100644 --- a/tests/unit/test_extensions.py +++ b/tests/unit/test_extensions.py @@ -15,7 +15,6 @@ # under the License. import inspect -import json import os.path import routes import unittest @@ -28,6 +27,7 @@ from webtest import TestApp from openstack.common import config from openstack.common import extensions +from openstack.common import jsonutils from openstack.common import wsgi from tests.unit import extension_stubs @@ -86,7 +86,8 @@ class ResourceExtensionTest(unittest.TestCase): response = test_app.get("/tweedles/some_id/custom_member_action") self.assertEqual(200, response.status_int) - self.assertEqual(json.loads(response.body)['member_action'], "value") + self.assertEqual(jsonutils.loads(response.body)['member_action'], + "value") def test_resource_extension_for_get_custom_collection_action(self): controller = self.ResourceExtensionController() @@ -97,7 +98,7 @@ class ResourceExtensionTest(unittest.TestCase): response = test_app.put("/tweedles/custom_collection_action") self.assertEqual(200, response.status_int) - self.assertEqual(json.loads(response.body)['collection'], "value") + self.assertEqual(jsonutils.loads(response.body)['collection'], "value") def test_resource_extension_for_put_custom_collection_action(self): controller = self.ResourceExtensionController() @@ -109,7 +110,7 @@ class ResourceExtensionTest(unittest.TestCase): response = test_app.put("/tweedles/custom_collection_action") self.assertEqual(200, response.status_int) - self.assertEqual(json.loads(response.body)['collection'], 'value') + self.assertEqual(jsonutils.loads(response.body)['collection'], 'value') def test_resource_extension_for_post_custom_collection_action(self): controller = self.ResourceExtensionController() @@ -121,7 +122,7 @@ class ResourceExtensionTest(unittest.TestCase): response = test_app.post("/tweedles/custom_collection_action") self.assertEqual(200, response.status_int) - self.assertEqual(json.loads(response.body)['collection'], 'value') + self.assertEqual(jsonutils.loads(response.body)['collection'], 'value') def test_resource_extension_for_delete_custom_collection_action(self): controller = self.ResourceExtensionController() @@ -133,7 +134,7 @@ class ResourceExtensionTest(unittest.TestCase): response = test_app.delete("/tweedles/custom_collection_action") self.assertEqual(200, response.status_int) - self.assertEqual(json.loads(response.body)['collection'], 'value') + self.assertEqual(jsonutils.loads(response.body)['collection'], 'value') def test_resource_ext_for_formatted_req_on_custom_collection_action(self): controller = self.ResourceExtensionController() @@ -145,7 +146,7 @@ class ResourceExtensionTest(unittest.TestCase): response = test_app.get("/tweedles/custom_collection_action.json") self.assertEqual(200, response.status_int) - self.assertEqual(json.loads(response.body)['collection'], "value") + self.assertEqual(jsonutils.loads(response.body)['collection'], "value") def test_resource_ext_for_nested_resource_custom_collection_action(self): controller = self.ResourceExtensionController() @@ -160,7 +161,7 @@ class ResourceExtensionTest(unittest.TestCase): "/tweedles/custom_collection_action") self.assertEqual(200, response.status_int) - self.assertEqual(json.loads(response.body)['collection'], "value") + self.assertEqual(jsonutils.loads(response.body)['collection'], "value") def test_returns_404_for_non_existant_extension(self): test_app = setup_extensions_app(SimpleExtensionManager(None)) @@ -179,7 +180,7 @@ class ActionExtensionTest(unittest.TestCase): def test_extended_action_for_adding_extra_data(self): action_name = 'FOXNSOX:add_tweedle' action_params = dict(name='Beetle') - req_body = json.dumps({action_name: action_params}) + req_body = jsonutils.dumps({action_name: action_params}) response = self.extension_app.post( '/dummy_resources/1/action', req_body, content_type='application/json') @@ -189,7 +190,7 @@ class ActionExtensionTest(unittest.TestCase): def test_extended_action_for_deleting_extra_data(self): action_name = 'FOXNSOX:delete_tweedle' action_params = dict(name='Bailey') - req_body = json.dumps({action_name: action_params}) + req_body = jsonutils.dumps({action_name: action_params}) response = self.extension_app.post( "/dummy_resources/1/action", req_body, content_type='application/json') @@ -198,7 +199,7 @@ class ActionExtensionTest(unittest.TestCase): def test_returns_404_for_non_existant_action(self): non_existant_action = 'blah_action' action_params = dict(name="test") - req_body = json.dumps({non_existant_action: action_params}) + req_body = jsonutils.dumps({non_existant_action: action_params}) response = self.extension_app.post( "/dummy_resources/1/action", @@ -210,7 +211,7 @@ class ActionExtensionTest(unittest.TestCase): def test_returns_404_for_non_existant_resource(self): action_name = 'add_tweedle' action_params = dict(name='Beetle') - req_body = json.dumps({action_name: action_params}) + req_body = jsonutils.dumps({action_name: action_params}) response = self.extension_app.post( "/asdf/1/action", req_body, @@ -235,9 +236,9 @@ class RequestExtensionTest(unittest.TestCase): def test_extend_get_resource_response(self): def extend_response_data(req, res): - data = json.loads(res.body) + data = jsonutils.loads(res.body) data['FOXNSOX:extended_key'] = req.GET.get('extended_key') - res.body = json.dumps(data) + res.body = jsonutils.dumps(data) return res app = self._setup_app_with_request_handler(extend_response_data, 'GET') @@ -245,7 +246,7 @@ class RequestExtensionTest(unittest.TestCase): self.assertEqual(200, response.status_int) - response_data = json.loads(response.body) + response_data = jsonutils.loads(response.body) self.assertEqual('extended_data', response_data['FOXNSOX:extended_key']) self.assertEqual('knox', response_data['fort']) @@ -255,21 +256,21 @@ class RequestExtensionTest(unittest.TestCase): response = app.get("/dummy_resources/1?chewing=newblue") - response_data = json.loads(response.body) + response_data = jsonutils.loads(response.body) self.assertEqual('newblue', response_data['FOXNSOX:googoose']) self.assertEqual("Pig Bands!", response_data['FOXNSOX:big_bands']) def test_edit_previously_uneditable_field(self): def _update_handler(req, res): - data = json.loads(res.body) - data['uneditable'] = json.loads(req.body)['uneditable'] - res.body = json.dumps(data) + data = jsonutils.loads(res.body) + data['uneditable'] = jsonutils.loads(req.body)['uneditable'] + res.body = jsonutils.dumps(data) return res base_app = TestApp(setup_base_app()) response = base_app.put("/dummy_resources/1", - json.dumps({'uneditable': "new_value"}), + jsonutils.dumps({'uneditable': "new_value"}), headers={'Content-Type': "application/json"}) self.assertEqual(response.json['uneditable'], "original_value") @@ -277,7 +278,7 @@ class RequestExtensionTest(unittest.TestCase): 'PUT') ext_response = ext_app.put( "/dummy_resources/1", - json.dumps({'uneditable': "new_value"}), + jsonutils.dumps({'uneditable': "new_value"}), headers={'Content-Type': "application/json"}) self.assertEqual(ext_response.json['uneditable'], "new_value") diff --git a/tests/unit/test_log.py b/tests/unit/test_log.py index f417db5..8eaffca 100644 --- a/tests/unit/test_log.py +++ b/tests/unit/test_log.py @@ -1,10 +1,10 @@ import cStringIO -import json import logging import sys from openstack.common import context from openstack.common import cfg +from openstack.common import jsonutils from openstack.common import log from openstack.common.notifier import api as notifier from openstack.common.notifier import list_notifier @@ -144,7 +144,7 @@ class JSONFormatterTestCase(test_utils.BaseTestCase): test_data = {'test': 'log'} self.log.debug(test_msg, test_data) - data = json.loads(self.stream.getvalue()) + data = jsonutils.loads(self.stream.getvalue()) self.assertTrue(data) self.assertTrue('extra' in data) self.assertEqual('test-json', data['name']) @@ -168,7 +168,7 @@ class JSONFormatterTestCase(test_utils.BaseTestCase): except Exception: self.log.exception(test_msg, test_data) - data = json.loads(self.stream.getvalue()) + data = jsonutils.loads(self.stream.getvalue()) self.assertTrue(data) self.assertTrue('extra' in data) self.assertEqual('test-json', data['name']) diff --git a/tests/unit/test_policy.py b/tests/unit/test_policy.py index 9471855..449c8a9 100644 --- a/tests/unit/test_policy.py +++ b/tests/unit/test_policy.py @@ -17,7 +17,6 @@ """Test of Policy Engine For Nova""" -import json import os.path import StringIO import unittest @@ -26,6 +25,7 @@ import urllib import mock import urllib2 +from openstack.common import jsonutils from openstack.common import policy @@ -374,7 +374,7 @@ class HttpBrainTestCase(unittest.TestCase): result = {} for item in self.post_data.split('&'): key, _sep, value = item.partition('=') - result[key] = json.loads(urllib.unquote_plus(value)) + result[key] = jsonutils.loads(urllib.unquote_plus(value)) return result |