From 71344e40e1ce1c30e781c679138b2fff132fe078 Mon Sep 17 00:00:00 2001 From: Kevin Bishop Date: Wed, 10 Jun 2015 17:08:38 -0500 Subject: [PATCH] Replace oslo incubator jsonutils with oslo_serialization This commit is part of a series of commits to replace our oslo incubator code with official oslo packages found on PyPI. Change-Id: Ie0f7bda7962033b53696ee3358ee1f2fdfff40fe Partial-Bug: 1463967 --- barbican/api/__init__.py | 2 +- barbican/api/hooks.py | 3 +- barbican/model/models.py | 2 +- barbican/openstack/common/jsonutils.py | 186 ------------------------- barbican/plugin/crypto/p11_crypto.py | 2 +- barbican/plugin/crypto/pkcs11.py | 2 +- barbican/tests/api/test_init.py | 3 +- requirements.txt | 1 + 8 files changed, 9 insertions(+), 192 deletions(-) delete mode 100644 barbican/openstack/common/jsonutils.py diff --git a/barbican/api/__init__.py b/barbican/api/__init__.py index 3cb2eece4..ce14887e6 100644 --- a/barbican/api/__init__.py +++ b/barbican/api/__init__.py @@ -19,13 +19,13 @@ API handler for Cloudkeep's Barbican import pkgutil from oslo_policy import policy +from oslo_serialization import jsonutils as json import pecan from barbican.common import config from barbican.common import exception from barbican.common import utils from barbican import i18n as u -from barbican.openstack.common import jsonutils as json LOG = utils.getLogger(__name__) diff --git a/barbican/api/hooks.py b/barbican/api/hooks.py index 60277a674..fcb314465 100644 --- a/barbican/api/hooks.py +++ b/barbican/api/hooks.py @@ -15,6 +15,8 @@ import pecan import webob +from oslo_serialization import jsonutils + try: import newrelic.agent newrelic_loaded = True @@ -22,7 +24,6 @@ except ImportError: newrelic_loaded = False from barbican.model import repositories -from barbican.openstack.common import jsonutils class JSONErrorHook(pecan.hooks.PecanHook): diff --git a/barbican/model/models.py b/barbican/model/models.py index e48beedbc..bad2cda56 100644 --- a/barbican/model/models.py +++ b/barbican/model/models.py @@ -18,6 +18,7 @@ Defines database models for Barbican """ import hashlib +from oslo_serialization import jsonutils as json import six import sqlalchemy as sa from sqlalchemy.ext import compiler @@ -29,7 +30,6 @@ from sqlalchemy import types as sql_types from barbican.common import exception from barbican.common import utils from barbican import i18n as u -from barbican.openstack.common import jsonutils as json from barbican.openstack.common import timeutils from barbican.plugin.interface import secret_store diff --git a/barbican/openstack/common/jsonutils.py b/barbican/openstack/common/jsonutils.py deleted file mode 100644 index 3fbc92b95..000000000 --- a/barbican/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 barbican.openstack.common import gettextutils -from barbican.openstack.common import importutils -from barbican.openstack.common import strutils -from barbican.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/barbican/plugin/crypto/p11_crypto.py b/barbican/plugin/crypto/p11_crypto.py index 42979f936..1134a6661 100644 --- a/barbican/plugin/crypto/p11_crypto.py +++ b/barbican/plugin/crypto/p11_crypto.py @@ -13,11 +13,11 @@ import base64 from oslo_config import cfg +from oslo_serialization import jsonutils as json from barbican.common import config from barbican.common import utils from barbican import i18n as u -from barbican.openstack.common import jsonutils as json from barbican.plugin.crypto import crypto as plugin from barbican.plugin.crypto import pkcs11 diff --git a/barbican/plugin/crypto/pkcs11.py b/barbican/plugin/crypto/pkcs11.py index e03092845..17f83e974 100644 --- a/barbican/plugin/crypto/pkcs11.py +++ b/barbican/plugin/crypto/pkcs11.py @@ -16,11 +16,11 @@ import textwrap import cffi from cryptography.hazmat.primitives import padding +from oslo_serialization import jsonutils as json from barbican.common import exception from barbican.common import utils from barbican import i18n as u -from barbican.openstack.common import jsonutils as json LOG = utils.getLogger(__name__) diff --git a/barbican/tests/api/test_init.py b/barbican/tests/api/test_init.py index cdb2d8964..d0fe4fbbe 100644 --- a/barbican/tests/api/test_init.py +++ b/barbican/tests/api/test_init.py @@ -16,9 +16,10 @@ This test module tests the barbican.api.__init__.py module functionality. """ import mock +from oslo_serialization import jsonutils as json + from barbican import api from barbican.common import exception -from barbican.openstack.common import jsonutils as json from barbican.plugin.interface import secret_store from barbican.tests import utils diff --git a/requirements.txt b/requirements.txt index 344030c03..e0fbfb3b5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,6 +16,7 @@ oslo.i18n>=1.5.0 # Apache-2.0 oslo.messaging>=1.8.0 # Apache-2.0 oslo.log>=1.2.0 # Apache-2.0 oslo.policy>=0.5.0 # Apache-2.0 +oslo.serialization>=1.4.0 # Apache-2.0 Paste PasteDeploy>=1.5.0 pbr>=0.11,<2.0