Use jsonutils from oslo.serialization

Sahara should use jsonutils from oslo_serialization because
it support all functions from json. Also added
hacking check for that.

Change-Id: I8c286ad947b92d6552b17cfa7ef80ba1beb5b51b
Closes-bug: 1442120
This commit is contained in:
Vitaly Gridnev 2015-04-09 17:21:29 +03:00
parent 603b30f90a
commit efc639caff
23 changed files with 62 additions and 25 deletions

View File

@ -44,3 +44,9 @@ Logs
- [S373] LOG.debug never used for translations
- [S374] You used a deprecated log level
Importing json
--------------
- [S375] It's more preferable to use ``jsonutils`` from ``oslo_serialization``
instead of ``json`` for operating with ``json`` objects.

View File

@ -13,12 +13,12 @@
# limitations under the License.
import copy
import json
import os
import uuid
import jsonschema
from oslo_config import cfg
from oslo_serialization import jsonutils as json
import six
from sahara import conductor

View File

@ -23,13 +23,13 @@
# We also change some importings to use Sahara inherited classes.
import cookielib
import json
import posixpath
import types
import urllib
import urllib2
from oslo_log import log as logging
from oslo_serialization import jsonutils as json
import six
from sahara.i18n import _LW

View File

@ -22,12 +22,12 @@
# To satisfy the pep8 and python3 tests, we did some changes to the codes.
# We also change some importings to use Sahara inherited classes.
import json
import posixpath
import socket
import urllib2
from oslo_log import log as logging
from oslo_serialization import jsonutils as json
import six
from sahara import context

View File

@ -22,8 +22,7 @@
# To satisfy the pep8 and python3 tests, we did some changes to the codes.
# We also change some importings to use Sahara inherited classes.
import json
from oslo_serialization import jsonutils as json
import six
from sahara.plugins.cdh.client import role_config_groups

View File

@ -24,9 +24,9 @@
import copy
import datetime
import json
import time
from oslo_serialization import jsonutils as json
import six
from sahara import context

View File

@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from oslo_serialization import jsonutils as json
from sahara.plugins import provisioning as p
from sahara.utils import files as f

View File

@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from oslo_serialization import jsonutils as json
from sahara.plugins.cdh.client import api_client

View File

@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from oslo_serialization import jsonutils as json
from sahara.plugins import provisioning as p
from sahara.utils import files as f

View File

@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from oslo_serialization import jsonutils as json
from sahara.plugins.cdh.client import api_client

View File

@ -13,10 +13,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from oslo_config import cfg
from oslo_log import log as logging
from oslo_serialization import jsonutils as json
import pkg_resources as pkg
from sahara import context

View File

@ -13,10 +13,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from oslo_config import cfg
from oslo_log import log as logging
from oslo_serialization import jsonutils as json
import pkg_resources as pkg
import six

View File

@ -13,10 +13,10 @@
# under the License.
import json
import random
from oslo_log import log as logging
from oslo_serialization import jsonutils as json
from oslo_utils import timeutils
from sahara import context

View File

@ -13,7 +13,7 @@
# under the License.
import json
from oslo_serialization import jsonutils as json
from sahara import context
import sahara.exceptions as e

View File

@ -13,9 +13,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import re
from oslo_serialization import jsonutils as json
from six.moves.urllib import parse as urlparse
import sahara.exceptions as ex

View File

@ -13,10 +13,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from oslo_config import cfg
from oslo_log import log as logging
from oslo_serialization import jsonutils as json
import six
from sahara import conductor as c

View File

@ -12,13 +12,13 @@
# License for the specific language governing permissions and limitations
# under the License.
import json
import random
import string
import time
import uuid
import fixtures
from oslo_serialization import jsonutils as json
import six
from sahara.tests.integration.tests import base as b

View File

@ -16,7 +16,6 @@
from __future__ import print_function
import functools
import glob
import json
import logging
import os
import sys
@ -24,6 +23,7 @@ import time
import traceback
import fixtures
from oslo_serialization import jsonutils as json
from oslo_utils import timeutils
import prettytable
import six

View File

@ -13,12 +13,12 @@
# limitations under the License.
import copy
import json
import tempfile
import uuid
import jsonschema
import mock
from oslo_serialization import jsonutils as json
from oslo_utils import uuidutils
from sahara import context

View File

@ -14,10 +14,10 @@
# limitations under the License.
import itertools
import json
import os
import uuid
from oslo_serialization import jsonutils as json
import testtools
from sahara.service.validations.edp import data_source

View File

@ -49,3 +49,18 @@ class HackingTestCase(testtools.TestCase):
self.assertEqual(0, len(list(checks.dict_constructor_with_list_copy(
" self._render_dict(xml, data_el, data.__dict__)"))))
def test_use_jsonutils(self):
self.assertEqual(0, len(list(checks.use_jsonutils(
"import json # noqa", "path"))))
self.assertEqual(0, len(list(checks.use_jsonutils(
"from oslo_serialization import jsonutils as json", "path"))))
self.assertEqual(0, len(list(checks.use_jsonutils(
"import jsonschema", "path"))))
self.assertEqual(0, len(list(checks.use_jsonutils(
"import json", "sahara/openstack/common/utils.py"))))
self.assertEqual(1, len(list(checks.use_jsonutils(
"import json", "path"))))
self.assertEqual(1, len(list(checks.use_jsonutils(
"import json as jsonutils", "path"))))

View File

@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pep8
import re
import tokenize
@ -97,6 +98,25 @@ def dict_constructor_with_list_copy(logical_line):
'constructor with a sequence of key-value pairs.')
def use_jsonutils(logical_line, filename):
"""Check to prevent importing json in sahara code.
S375
"""
if pep8.noqa(logical_line):
return
ignore_dirs = ["sahara/openstack/common"]
for dir in ignore_dirs:
if dir in filename:
return
invalid_line = re.compile(r"(import\s+json)")
valid_line = re.compile(r"(import\s+jsonschema)")
if (re.match(invalid_line, logical_line) and
not re.match(valid_line, logical_line)):
yield(0, "S375: Use jsonutils from oslo_serialization instead"
" of json")
def factory(register):
register(import_db_only_in_conductor)
register(hacking_no_author_attr)
@ -109,3 +129,4 @@ def factory(register):
register(logging_checks.validate_log_translations)
register(logging_checks.no_translate_debug_logs)
register(logging_checks.accepted_log_levels)
register(use_jsonutils)

View File

@ -13,9 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from oslo_config import cfg
from oslo_serialization import jsonutils as json
from six.moves.urllib import parse as urlparse
from sahara import context