From aaeda502bb0ffc0adc99795a86b7396c8f2f0b76 Mon Sep 17 00:00:00 2001 From: gong yong sheng Date: Tue, 7 Jun 2016 13:48:39 +0800 Subject: [PATCH] Use oslo serialization jsonutils Change-Id: I081697d987fc7c2bcd4ae73b97aedc058c57a84f Partial-bug: #1552282 --- requirements.txt | 1 + tacker/openstack/common/jsonutils.py | 186 --------------------- tacker/openstack/common/policy.py | 2 +- tacker/tests/unit/extensions/foxinsocks.py | 3 +- tacker/tests/unit/test_extensions.py | 2 +- tacker/tests/unit/test_policy.py | 2 +- tacker/vm/infra_drivers/heat/heat.py | 2 +- tacker/vm/mgmt_drivers/abstract_driver.py | 2 +- tacker/vm/mgmt_drivers/openwrt/openwrt.py | 2 +- tacker/vm/monitor.py | 2 +- tacker/wsgi.py | 2 +- 11 files changed, 11 insertions(+), 195 deletions(-) delete mode 100644 tacker/openstack/common/jsonutils.py diff --git a/requirements.txt b/requirements.txt index 2ec679639..63cad7ae1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -28,6 +28,7 @@ oslo.config>=3.10.0 # Apache-2.0 oslo.log>=1.14.0 # Apache-2.0 oslo.messaging>=4.5.0 # Apache-2.0 oslo.rootwrap>=2.0.0 # Apache-2.0 +oslo.serialization>=1.10.0 # Apache-2.0 oslosphinx!=3.4.0,>=2.5.0 # Apache-2.0 python-novaclient!=2.33.0,>=2.29.0 # Apache-2.0 tosca-parser>=0.5.0 # Apache-2.0 diff --git a/tacker/openstack/common/jsonutils.py b/tacker/openstack/common/jsonutils.py deleted file mode 100644 index 0e833a0cd..000000000 --- a/tacker/openstack/common/jsonutils.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright 2010 United States Government as represented by the -# Administrator of the National Aeronautics and Space Administration. -# Copyright 2011 Justin Santa Barbara -# All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -''' -JSON related utilities. - -This module provides a few things: - - 1) A handy function for getting an object down to something that can be - JSON serialized. See to_primitive(). - - 2) Wrappers around loads() and dumps(). The dumps() wrapper will - automatically use to_primitive() for you if needed. - - 3) This sets up anyjson to use the loads() and dumps() wrappers if anyjson - is available. -''' - - -import codecs -import datetime -import functools -import inspect -import itertools -import sys - -if sys.version_info < (2, 7): - # On Python <= 2.6, json module is not C boosted, so try to use - # simplejson module if available - try: - import simplejson as json - except ImportError: - import json -else: - import json - -import six -import six.moves.xmlrpc_client as xmlrpclib - -from tacker.openstack.common import gettextutils -from tacker.openstack.common import importutils -from tacker.openstack.common import strutils -from tacker.openstack.common import timeutils - -netaddr = importutils.try_import("netaddr") - -_nasty_type_tests = [inspect.ismodule, inspect.isclass, inspect.ismethod, - inspect.isfunction, inspect.isgeneratorfunction, - inspect.isgenerator, inspect.istraceback, inspect.isframe, - inspect.iscode, inspect.isbuiltin, inspect.isroutine, - inspect.isabstract] - -_simple_types = (six.string_types + six.integer_types - + (type(None), bool, float)) - - -def to_primitive(value, convert_instances=False, convert_datetime=True, - level=0, max_depth=3): - """Convert a complex object into primitives. - - Handy for JSON serialization. We can optionally handle instances, - but since this is a recursive function, we could have cyclical - data structures. - - To handle cyclical data structures we could track the actual objects - visited in a set, but not all objects are hashable. Instead we just - track the depth of the object inspections and don't go too deep. - - Therefore, convert_instances=True is lossy ... be aware. - - """ - # handle obvious types first - order of basic types determined by running - # full tests on nova project, resulting in the following counts: - # 572754 - # 460353 - # 379632 - # 274610 - # 199918 - # 114200 - # 51817 - # 26164 - # 6491 - # 283 - # 19 - if isinstance(value, _simple_types): - return value - - if isinstance(value, datetime.datetime): - if convert_datetime: - return timeutils.strtime(value) - else: - return value - - # value of itertools.count doesn't get caught by nasty_type_tests - # and results in infinite loop when list(value) is called. - if type(value) == itertools.count: - return six.text_type(value) - - # FIXME(vish): Workaround for LP bug 852095. Without this workaround, - # tests that raise an exception in a mocked method that - # has a @wrap_exception with a notifier will fail. If - # we up the dependency to 0.5.4 (when it is released) we - # can remove this workaround. - if getattr(value, '__module__', None) == 'mox': - return 'mock' - - if level > max_depth: - return '?' - - # The try block may not be necessary after the class check above, - # but just in case ... - try: - recursive = functools.partial(to_primitive, - convert_instances=convert_instances, - convert_datetime=convert_datetime, - level=level, - max_depth=max_depth) - if isinstance(value, dict): - return dict((k, recursive(v)) for k, v in six.iteritems(value)) - elif isinstance(value, (list, tuple)): - return [recursive(lv) for lv in value] - - # It's not clear why xmlrpclib created their own DateTime type, but - # for our purposes, make it a datetime type which is explicitly - # handled - if isinstance(value, xmlrpclib.DateTime): - value = datetime.datetime(*tuple(value.timetuple())[:6]) - - if convert_datetime and isinstance(value, datetime.datetime): - return timeutils.strtime(value) - elif isinstance(value, gettextutils.Message): - return value.data - elif hasattr(value, 'iteritems'): - return recursive(dict(value.iteritems()), level=level + 1) - elif hasattr(value, '__iter__'): - return recursive(list(value)) - elif convert_instances and hasattr(value, '__dict__'): - # Likely an instance of something. Watch for cycles. - # Ignore class member vars. - return recursive(value.__dict__, level=level + 1) - elif netaddr and isinstance(value, netaddr.IPAddress): - return six.text_type(value) - else: - if any(test(value) for test in _nasty_type_tests): - return six.text_type(value) - return value - except TypeError: - # Class objects are tricky since they may define something like - # __iter__ defined but it isn't callable as list(). - return six.text_type(value) - - -def dumps(value, default=to_primitive, **kwargs): - return json.dumps(value, default=default, **kwargs) - - -def loads(s, encoding='utf-8'): - return json.loads(strutils.safe_decode(s, encoding)) - - -def load(fp, encoding='utf-8'): - return json.load(codecs.getreader(encoding)(fp)) - - -try: - import anyjson -except ImportError: - pass -else: - anyjson._modules.append((__name__, 'dumps', TypeError, - 'loads', ValueError, 'load')) - anyjson.force_implementation(__name__) diff --git a/tacker/openstack/common/policy.py b/tacker/openstack/common/policy.py index edfd9b665..af8ee4ac2 100644 --- a/tacker/openstack/common/policy.py +++ b/tacker/openstack/common/policy.py @@ -58,12 +58,12 @@ import abc import re from oslo_log import log as logging +from oslo_serialization import jsonutils import six from six.moves.urllib import parse as urllib_parse from six.moves.urllib import request as urlrequest from tacker.openstack.common.gettextutils import _ -from tacker.openstack.common import jsonutils LOG = logging.getLogger(__name__) diff --git a/tacker/tests/unit/extensions/foxinsocks.py b/tacker/tests/unit/extensions/foxinsocks.py index 95a3d4755..b50caaf7e 100644 --- a/tacker/tests/unit/extensions/foxinsocks.py +++ b/tacker/tests/unit/extensions/foxinsocks.py @@ -15,8 +15,9 @@ import abc +from oslo_serialization import jsonutils + from tacker.api import extensions -from tacker.openstack.common import jsonutils from tacker import wsgi diff --git a/tacker/tests/unit/test_extensions.py b/tacker/tests/unit/test_extensions.py index cd48100b6..8d3b203b0 100644 --- a/tacker/tests/unit/test_extensions.py +++ b/tacker/tests/unit/test_extensions.py @@ -17,6 +17,7 @@ import abc import mock from oslo_log import log as logging +from oslo_serialization import jsonutils import routes import webob import webtest @@ -24,7 +25,6 @@ import webtest from tacker.api import extensions from tacker.common import config from tacker.common import exceptions -from tacker.openstack.common import jsonutils from tacker.plugins.common import constants from tacker.tests import base from tacker.tests.unit import extension_stubs as ext_stubs diff --git a/tacker/tests/unit/test_policy.py b/tacker/tests/unit/test_policy.py index 81c1c2c4b..9accfd679 100644 --- a/tacker/tests/unit/test_policy.py +++ b/tacker/tests/unit/test_policy.py @@ -19,6 +19,7 @@ import fixtures import mock import six +from oslo_serialization import jsonutils as json from six.moves.urllib import request as urlrequest import tacker @@ -27,7 +28,6 @@ from tacker.common import exceptions from tacker import context from tacker import manager from tacker.openstack.common import importutils -from tacker.openstack.common import jsonutils as json from tacker.openstack.common import policy as common_policy from tacker import policy from tacker.tests import base diff --git a/tacker/vm/infra_drivers/heat/heat.py b/tacker/vm/infra_drivers/heat/heat.py index d0b3cda8a..9824dc809 100644 --- a/tacker/vm/infra_drivers/heat/heat.py +++ b/tacker/vm/infra_drivers/heat/heat.py @@ -20,6 +20,7 @@ import time from heatclient import exc as heatException from oslo_config import cfg from oslo_log import log as logging +from oslo_serialization import jsonutils from six import iteritems from toscaparser.tosca_template import ToscaTemplate from toscaparser.utils import yamlparser @@ -29,7 +30,6 @@ import yaml from tacker.common import clients from tacker.common import log from tacker.extensions import vnfm -from tacker.openstack.common import jsonutils from tacker.vm.infra_drivers import abstract_driver from tacker.vm.tosca import utils as toscautils diff --git a/tacker/vm/mgmt_drivers/abstract_driver.py b/tacker/vm/mgmt_drivers/abstract_driver.py index b0f55375e..cf43e1ecb 100644 --- a/tacker/vm/mgmt_drivers/abstract_driver.py +++ b/tacker/vm/mgmt_drivers/abstract_driver.py @@ -16,10 +16,10 @@ import abc +from oslo_serialization import jsonutils import six from tacker.api import extensions -from tacker.openstack.common import jsonutils from tacker.vm import constants diff --git a/tacker/vm/mgmt_drivers/openwrt/openwrt.py b/tacker/vm/mgmt_drivers/openwrt/openwrt.py index 7dee8c1f0..fbcbb89f9 100644 --- a/tacker/vm/mgmt_drivers/openwrt/openwrt.py +++ b/tacker/vm/mgmt_drivers/openwrt/openwrt.py @@ -16,11 +16,11 @@ from oslo_config import cfg from oslo_log import log as logging +from oslo_serialization import jsonutils import yaml from tacker.agent.linux import utils from tacker.common import log -from tacker.openstack.common import jsonutils from tacker.vm.mgmt_drivers import abstract_driver from tacker.vm.mgmt_drivers import constants as mgmt_constants diff --git a/tacker/vm/monitor.py b/tacker/vm/monitor.py index cbda7a201..03e715e2e 100644 --- a/tacker/vm/monitor.py +++ b/tacker/vm/monitor.py @@ -21,13 +21,13 @@ import time from oslo_config import cfg from oslo_log import log as logging +from oslo_serialization import jsonutils from oslo_utils import timeutils import six from tacker.common import clients from tacker.common import driver_manager from tacker import context as t_context -from tacker.openstack.common import jsonutils from tacker.vm.infra_drivers.heat import heat diff --git a/tacker/wsgi.py b/tacker/wsgi.py index 68997715d..84b1de561 100644 --- a/tacker/wsgi.py +++ b/tacker/wsgi.py @@ -31,6 +31,7 @@ import eventlet.wsgi # eventlet.patcher.monkey_patch(all=False, socket=True, thread=True) from oslo_config import cfg from oslo_log import log as logging +from oslo_serialization import jsonutils import routes.middleware import six import webob.dec @@ -42,7 +43,6 @@ from tacker import context from tacker.db import api from tacker.openstack.common import excutils from tacker.openstack.common import gettextutils -from tacker.openstack.common import jsonutils from tacker.openstack.common import service as common_service from tacker.openstack.common import systemd