- From 4e83d6a7408794dbb5424530eb369aed1efd2fa6 Mon Sep 17 00:00:00 2001
- From: Ewoud Kohl van Wijngaarden <ewoud@kohlvanwijngaarden.nl>
- Date: Wed, 29 Jun 2011 17:00:45 +0200
- Subject: [PATCH] Allow a fallback to built-in json library
- ---
- lib/client/gnt_debug.py | 7 +++++--
- lib/client/gnt_instance.py | 7 +++++--
- lib/rapi/baserlib.py | 2 +-
- lib/rapi/client.py | 10 +++++++---
- lib/serializer.py | 13 ++++++++-----
- 5 files changed, 26 insertions(+), 13 deletions(-)
- diff --git a/lib/client/gnt_debug.py b/lib/client/gnt_debug.py
- index c7f0a7d..fd25421 100644
- --- a/lib/client/gnt_debug.py
- +++ b/lib/client/gnt_debug.py
- @@ -25,7 +25,6 @@
- # W0614: Unused import %s from wildcard import (since we need cli)
- # C0103: Invalid name gnt-backup
- -import simplejson
- import time
- import socket
- import logging
- @@ -38,6 +37,10 @@ from ganeti import utils
- from ganeti import errors
- from ganeti import compat
- +try:
- + import simplejson as json
- +except ImportError:
- + import json
- #: Default fields for L{ListLocks}
- _LIST_LOCKS_DEF_FIELDS = [
- @@ -90,7 +93,7 @@ def GenericOpCodes(opts, args):
- for job_idx in range(opts.rep_job):
- for fname in args:
- # pylint: disable-msg=W0142
- - op_data = simplejson.loads(utils.ReadFile(fname))
- + op_data = json.loads(utils.ReadFile(fname))
- op_list = [opcodes.OpCode.LoadOpCode(val) for val in op_data]
- op_list = op_list * opts.rep_op
- jex.QueueJob("file %s/%d" % (fname, job_idx), *op_list)
- diff --git a/lib/client/gnt_instance.py b/lib/client/gnt_instance.py
- index 1d6faa4..8655a3f 100644
- --- a/lib/client/gnt_instance.py
- +++ b/lib/client/gnt_instance.py
- @@ -26,7 +26,6 @@
- # C0103: Invalid name gnt-instance
- import itertools
- -import simplejson
- import logging
- from cStringIO import StringIO
- @@ -40,6 +39,10 @@ from ganeti import netutils
- from ganeti import ssh
- from ganeti import objects
- +try:
- + import simplejson as json
- +except ImportError:
- + import json
- _SHUTDOWN_CLUSTER = "cluster"
- _SHUTDOWN_NODES_BOTH = "nodes"
- @@ -321,7 +324,7 @@ def BatchCreate(opts, args):
- json_filename = args[0]
- try:
- - instance_data = simplejson.loads(utils.ReadFile(json_filename))
- + instance_data = json.loads(utils.ReadFile(json_filename))
- except Exception, err: # pylint: disable-msg=W0703
- ToStderr("Can't parse the instance definition file: %s" % str(err))
- return 1
- diff --git a/lib/rapi/baserlib.py b/lib/rapi/baserlib.py
- index 534ebae..95534a4 100644
- --- a/lib/rapi/baserlib.py
- +++ b/lib/rapi/baserlib.py
- @@ -213,7 +213,7 @@ def FillOpcode(opcls, body, static, rename=None):
- params.update(static)
- - # Convert keys to strings (simplejson decodes them as unicode)
- + # Convert keys to strings (json decodes them as unicode)
- params = dict((str(key), value) for (key, value) in params.items())
- try:
- diff --git a/lib/rapi/client.py b/lib/rapi/client.py
- index d2aa4ac..dc54db4 100644
- --- a/lib/rapi/client.py
- +++ b/lib/rapi/client.py
- @@ -34,7 +34,6 @@
- # be standalone.
- import logging
- -import simplejson
- import socket
- import urllib
- import threading
- @@ -42,6 +41,11 @@ import pycurl
- import time
- try:
- + import simplejson as json
- +except ImportError:
- + import json
- +
- +try:
- from cStringIO import StringIO
- except ImportError:
- from StringIO import StringIO
- @@ -262,7 +266,7 @@ class GanetiRapiClient(object): # pylint: disable-msg=R0904
- """
- USER_AGENT = "Ganeti RAPI Client"
- - _json_encoder = simplejson.JSONEncoder(sort_keys=True)
- + _json_encoder = json.JSONEncoder(sort_keys=True)
- def __init__(self, host, port=GANETI_RAPI_PORT,
- username=None, password=None, logger=logging,
- @@ -439,7 +443,7 @@ class GanetiRapiClient(object): # pylint: disable-msg=R0904
- # Was anything written to the response buffer?
- if encoded_resp_body.tell():
- - response_content = simplejson.loads(encoded_resp_body.getvalue())
- + response_content = json.loads(encoded_resp_body.getvalue())
- else:
- response_content = None
- diff --git a/lib/serializer.py b/lib/serializer.py
- index 9a5f1ce..86042a3 100644
- --- a/lib/serializer.py
- +++ b/lib/serializer.py
- @@ -29,19 +29,22 @@ backend (currently json).
- # C0103: Invalid name, since pylint doesn't see that Dump points to a
- # function and not a constant
- -import simplejson
- import re
- from ganeti import errors
- from ganeti import utils
- +try:
- + import simplejson as json
- +except ImportError:
- + import json
- _JSON_INDENT = 2
- _RE_EOLSP = re.compile('[ \t]+$', re.MULTILINE)
- -def _GetJsonDumpers(_encoder_class=simplejson.JSONEncoder):
- +def _GetJsonDumpers(_encoder_class=json.JSONEncoder):
- """Returns two JSON functions to serialize data.
- @rtype: (callable, callable)
- @@ -51,7 +54,7 @@ def _GetJsonDumpers(_encoder_class=simplejson.JSONEncoder):
- """
- plain_encoder = _encoder_class(sort_keys=True)
- - # Check whether the simplejson module supports indentation
- + # Check whether the json module supports indentation
- try:
- indent_encoder = _encoder_class(indent=_JSON_INDENT, sort_keys=True)
- except TypeError:
- @@ -68,7 +71,7 @@ def DumpJson(data, indent=True):
- """Serialize a given object.
- @param data: the data to serialize
- - @param indent: whether to indent output (depends on simplejson version)
- + @param indent: whether to indent output (depends on json version)
- @return: the string representation of data
- @@ -93,7 +96,7 @@ def LoadJson(txt):
- @return: the original data
- """
- - return simplejson.loads(txt)
- + return json.loads(txt)
- def DumpSignedJson(data, key, salt=None, key_selector=None):
- --
- 1.7.3.4