diff --git a/heatclient/common/http.py b/heatclient/common/http.py index 39240104..1659f71d 100644 --- a/heatclient/common/http.py +++ b/heatclient/common/http.py @@ -23,9 +23,10 @@ import requests import six from six.moves.urllib import parse +from oslo.serialization import jsonutils + from heatclient import exc from heatclient.openstack.common import importutils -from heatclient.openstack.common import jsonutils from heatclient.openstack.common import strutils LOG = logging.getLogger(__name__) diff --git a/heatclient/common/template_utils.py b/heatclient/common/template_utils.py index b7a6a703..bf55c91f 100644 --- a/heatclient/common/template_utils.py +++ b/heatclient/common/template_utils.py @@ -21,10 +21,11 @@ from six.moves.urllib import error from six.moves.urllib import parse from six.moves.urllib import request +from oslo.serialization import jsonutils + from heatclient.common import environment_format from heatclient.common import template_format from heatclient import exc -from heatclient.openstack.common import jsonutils def get_template_contents(template_file=None, template_url=None, diff --git a/heatclient/common/utils.py b/heatclient/common/utils.py index 06df8971..7c322fe7 100644 --- a/heatclient/common/utils.py +++ b/heatclient/common/utils.py @@ -21,10 +21,11 @@ import textwrap import uuid import yaml +from oslo.serialization import jsonutils + from heatclient import exc from heatclient.openstack.common import cliutils from heatclient.openstack.common import importutils -from heatclient.openstack.common import jsonutils supported_formats = { "json": lambda x: jsonutils.dumps(x, indent=2), diff --git a/heatclient/exc.py b/heatclient/exc.py index fd2f334b..1c9883d8 100644 --- a/heatclient/exc.py +++ b/heatclient/exc.py @@ -12,7 +12,7 @@ import sys -from heatclient.openstack.common import jsonutils +from oslo.serialization import jsonutils verbose = 0 diff --git a/heatclient/openstack/common/jsonutils.py b/heatclient/openstack/common/jsonutils.py deleted file mode 100644 index 18915c18..00000000 --- a/heatclient/openstack/common/jsonutils.py +++ /dev/null @@ -1,182 +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 datetime -import functools -import inspect -import itertools -import json -try: - import xmlrpclib -except ImportError: - # NOTE(jaypipes): xmlrpclib was renamed to xmlrpc.client in Python3 - # however the function and object call signatures - # remained the same. This whole try/except block should - # be removed and replaced with a call to six.moves once - # six 1.4.2 is released. See http://bit.ly/1bqrVzu - import xmlrpc.client as xmlrpclib - -import six - -from heatclient.openstack.common import gettextutils -from heatclient.openstack.common import importutils -from heatclient.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): - return json.loads(s) - - -def load(s): - return json.load(s) - - -try: - import anyjson -except ImportError: - pass -else: - anyjson._modules.append((__name__, 'dumps', TypeError, - 'loads', ValueError, 'load')) - anyjson.force_implementation(__name__) diff --git a/heatclient/tests/fakes.py b/heatclient/tests/fakes.py index f345a602..955056cd 100644 --- a/heatclient/tests/fakes.py +++ b/heatclient/tests/fakes.py @@ -11,9 +11,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +from oslo.serialization import jsonutils + from heatclient.common import http from heatclient import exc -from heatclient.openstack.common import jsonutils def script_heat_list(url=None, show_nested=False): diff --git a/heatclient/tests/keystone_client_fixtures.py b/heatclient/tests/keystone_client_fixtures.py index 726ea217..7497a12b 100644 --- a/heatclient/tests/keystone_client_fixtures.py +++ b/heatclient/tests/keystone_client_fixtures.py @@ -10,11 +10,12 @@ # License for the specific language governing permissions and limitations # under the License. -from keystoneclient.fixture import v2 as ks_v2_fixture -from keystoneclient.fixture import v3 as ks_v3_fixture import uuid -from heatclient.openstack.common import jsonutils +from oslo.serialization import jsonutils + +from keystoneclient.fixture import v2 as ks_v2_fixture +from keystoneclient.fixture import v3 as ks_v3_fixture # these are copied from python-keystoneclient tests BASE_HOST = 'http://keystone.example.com' diff --git a/heatclient/tests/test_shell.py b/heatclient/tests/test_shell.py index c2dbb9e5..2bd83b25 100644 --- a/heatclient/tests/test_shell.py +++ b/heatclient/tests/test_shell.py @@ -11,26 +11,26 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json +import fixtures +import httpretty import os +from oslotest import mockpatch import re +import requests import six from six.moves.urllib import parse from six.moves.urllib import request import sys -import uuid - -import fixtures -import httpretty -from keystoneclient.fixture import v2 as ks_v2_fixture -from keystoneclient.fixture import v3 as ks_v3_fixture -from oslotest import mockpatch -import requests import tempfile import testscenarios import testtools +import uuid + +from oslo.serialization import jsonutils + +from keystoneclient.fixture import v2 as ks_v2_fixture +from keystoneclient.fixture import v3 as ks_v3_fixture -from heatclient.openstack.common import jsonutils from heatclient.openstack.common import strutils from mox3 import mox @@ -1301,8 +1301,8 @@ class ShellTestUserPass(ShellBase): def test_stack_update_enable_rollback(self): self.register_keystone_auth_fixture() template_file = os.path.join(TEST_VAR_DIR, 'minimal.template') - with open(template_file) as f: - template_data = json.load(f) + with open(template_file, 'rb') as f: + template_data = jsonutils.load(f) expected_data = {'files': {}, 'environment': {}, 'template': template_data, @@ -1343,8 +1343,8 @@ class ShellTestUserPass(ShellBase): def test_stack_update_disable_rollback(self): self.register_keystone_auth_fixture() template_file = os.path.join(TEST_VAR_DIR, 'minimal.template') - with open(template_file) as f: - template_data = json.load(f) + with open(template_file, 'rb') as f: + template_data = jsonutils.load(f) expected_data = {'files': {}, 'environment': {}, 'template': template_data, @@ -1396,8 +1396,8 @@ class ShellTestUserPass(ShellBase): def test_stack_update_rollback_default(self): self.register_keystone_auth_fixture() template_file = os.path.join(TEST_VAR_DIR, 'minimal.template') - with open(template_file) as f: - template_data = json.load(f) + with open(template_file, 'rb') as f: + template_data = jsonutils.load(f) expected_data = {'files': {}, 'environment': {}, 'template': template_data, diff --git a/heatclient/v1/shell.py b/heatclient/v1/shell.py index 410aa4b6..9730a18d 100644 --- a/heatclient/v1/shell.py +++ b/heatclient/v1/shell.py @@ -18,9 +18,10 @@ import six from six.moves.urllib import request import yaml +from oslo.serialization import jsonutils + from heatclient.common import template_utils from heatclient.common import utils -from heatclient.openstack.common import jsonutils from heatclient.openstack.common import strutils import heatclient.exc as exc diff --git a/requirements.txt b/requirements.txt index 33643ee6..3651bbcb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,6 +6,7 @@ pbr>=0.6,!=0.7,<1.0 argparse iso8601>=0.1.9 PrettyTable>=0.7,<0.8 +oslo.serialization>=1.0.0 # Apache-2.0 python-keystoneclient>=0.11.1 PyYAML>=3.1.0 requests>=2.2.0,!=2.4.0