Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 5.52 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. From 4e83d6a7408794dbb5424530eb369aed1efd2fa6 Mon Sep 17 00:00:00 2001
  2. From: Ewoud Kohl van Wijngaarden <ewoud@kohlvanwijngaarden.nl>
  3. Date: Wed, 29 Jun 2011 17:00:45 +0200
  4. Subject: [PATCH] Allow a fallback to built-in json library
  5.  
  6. ---
  7.  lib/client/gnt_debug.py    |    7 +++++--
  8.  lib/client/gnt_instance.py |    7 +++++--
  9.  lib/rapi/baserlib.py       |    2 +-
  10.  lib/rapi/client.py         |   10 +++++++---
  11.  lib/serializer.py          |   13 ++++++++-----
  12.  5 files changed, 26 insertions(+), 13 deletions(-)
  13.  
  14. diff --git a/lib/client/gnt_debug.py b/lib/client/gnt_debug.py
  15. index c7f0a7d..fd25421 100644
  16. --- a/lib/client/gnt_debug.py
  17. +++ b/lib/client/gnt_debug.py
  18. @@ -25,7 +25,6 @@
  19.  # W0614: Unused import %s from wildcard import (since we need cli)
  20.  # C0103: Invalid name gnt-backup
  21.  
  22. -import simplejson
  23.  import time
  24.  import socket
  25.  import logging
  26. @@ -38,6 +37,10 @@ from ganeti import utils
  27.  from ganeti import errors
  28.  from ganeti import compat
  29.  
  30. +try:
  31. +    import simplejson as json
  32. +except ImportError:
  33. +    import json
  34.  
  35.  #: Default fields for L{ListLocks}
  36.  _LIST_LOCKS_DEF_FIELDS = [
  37. @@ -90,7 +93,7 @@ def GenericOpCodes(opts, args):
  38.    for job_idx in range(opts.rep_job):
  39.      for fname in args:
  40.        # pylint: disable-msg=W0142
  41. -      op_data = simplejson.loads(utils.ReadFile(fname))
  42. +      op_data = json.loads(utils.ReadFile(fname))
  43.        op_list = [opcodes.OpCode.LoadOpCode(val) for val in op_data]
  44.        op_list = op_list * opts.rep_op
  45.        jex.QueueJob("file %s/%d" % (fname, job_idx), *op_list)
  46. diff --git a/lib/client/gnt_instance.py b/lib/client/gnt_instance.py
  47. index 1d6faa4..8655a3f 100644
  48. --- a/lib/client/gnt_instance.py
  49. +++ b/lib/client/gnt_instance.py
  50. @@ -26,7 +26,6 @@
  51.  # C0103: Invalid name gnt-instance
  52.  
  53.  import itertools
  54. -import simplejson
  55.  import logging
  56.  from cStringIO import StringIO
  57.  
  58. @@ -40,6 +39,10 @@ from ganeti import netutils
  59.  from ganeti import ssh
  60.  from ganeti import objects
  61.  
  62. +try:
  63. +    import simplejson as json
  64. +except ImportError:
  65. +    import json
  66.  
  67.  _SHUTDOWN_CLUSTER = "cluster"
  68.  _SHUTDOWN_NODES_BOTH = "nodes"
  69. @@ -321,7 +324,7 @@ def BatchCreate(opts, args):
  70.  
  71.    json_filename = args[0]
  72.    try:
  73. -    instance_data = simplejson.loads(utils.ReadFile(json_filename))
  74. +    instance_data = json.loads(utils.ReadFile(json_filename))
  75.    except Exception, err: # pylint: disable-msg=W0703
  76.      ToStderr("Can't parse the instance definition file: %s" % str(err))
  77.      return 1
  78. diff --git a/lib/rapi/baserlib.py b/lib/rapi/baserlib.py
  79. index 534ebae..95534a4 100644
  80. --- a/lib/rapi/baserlib.py
  81. +++ b/lib/rapi/baserlib.py
  82. @@ -213,7 +213,7 @@ def FillOpcode(opcls, body, static, rename=None):
  83.  
  84.      params.update(static)
  85.  
  86. -  # Convert keys to strings (simplejson decodes them as unicode)
  87. +  # Convert keys to strings (json decodes them as unicode)
  88.    params = dict((str(key), value) for (key, value) in params.items())
  89.  
  90.    try:
  91. diff --git a/lib/rapi/client.py b/lib/rapi/client.py
  92. index d2aa4ac..dc54db4 100644
  93. --- a/lib/rapi/client.py
  94. +++ b/lib/rapi/client.py
  95. @@ -34,7 +34,6 @@
  96.  # be standalone.
  97.  
  98.  import logging
  99. -import simplejson
  100.  import socket
  101.  import urllib
  102.  import threading
  103. @@ -42,6 +41,11 @@ import pycurl
  104.  import time
  105.  
  106.  try:
  107. +    import simplejson as json
  108. +except ImportError:
  109. +    import json
  110. +
  111. +try:
  112.    from cStringIO import StringIO
  113.  except ImportError:
  114.    from StringIO import StringIO
  115. @@ -262,7 +266,7 @@ class GanetiRapiClient(object): # pylint: disable-msg=R0904
  116.  
  117.    """
  118.    USER_AGENT = "Ganeti RAPI Client"
  119. -  _json_encoder = simplejson.JSONEncoder(sort_keys=True)
  120. +  _json_encoder = json.JSONEncoder(sort_keys=True)
  121.  
  122.    def __init__(self, host, port=GANETI_RAPI_PORT,
  123.                 username=None, password=None, logger=logging,
  124. @@ -439,7 +443,7 @@ class GanetiRapiClient(object): # pylint: disable-msg=R0904
  125.  
  126.      # Was anything written to the response buffer?
  127.      if encoded_resp_body.tell():
  128. -      response_content = simplejson.loads(encoded_resp_body.getvalue())
  129. +      response_content = json.loads(encoded_resp_body.getvalue())
  130.      else:
  131.        response_content = None
  132.  
  133. diff --git a/lib/serializer.py b/lib/serializer.py
  134. index 9a5f1ce..86042a3 100644
  135. --- a/lib/serializer.py
  136. +++ b/lib/serializer.py
  137. @@ -29,19 +29,22 @@ backend (currently json).
  138.  # C0103: Invalid name, since pylint doesn't see that Dump points to a
  139.  # function and not a constant
  140.  
  141. -import simplejson
  142.  import re
  143.  
  144.  from ganeti import errors
  145.  from ganeti import utils
  146.  
  147. +try:
  148. +    import simplejson as json
  149. +except ImportError:
  150. +    import json
  151.  
  152.  _JSON_INDENT = 2
  153.  
  154.  _RE_EOLSP = re.compile('[ \t]+$', re.MULTILINE)
  155.  
  156.  
  157. -def _GetJsonDumpers(_encoder_class=simplejson.JSONEncoder):
  158. +def _GetJsonDumpers(_encoder_class=json.JSONEncoder):
  159.    """Returns two JSON functions to serialize data.
  160.  
  161.    @rtype: (callable, callable)
  162. @@ -51,7 +54,7 @@ def _GetJsonDumpers(_encoder_class=simplejson.JSONEncoder):
  163.    """
  164.    plain_encoder = _encoder_class(sort_keys=True)
  165.  
  166. -  # Check whether the simplejson module supports indentation
  167. +  # Check whether the json module supports indentation
  168.    try:
  169.      indent_encoder = _encoder_class(indent=_JSON_INDENT, sort_keys=True)
  170.    except TypeError:
  171. @@ -68,7 +71,7 @@ def DumpJson(data, indent=True):
  172.    """Serialize a given object.
  173.  
  174.    @param data: the data to serialize
  175. -  @param indent: whether to indent output (depends on simplejson version)
  176. +  @param indent: whether to indent output (depends on json version)
  177.  
  178.    @return: the string representation of data
  179.  
  180. @@ -93,7 +96,7 @@ def LoadJson(txt):
  181.    @return: the original data
  182.  
  183.    """
  184. -  return simplejson.loads(txt)
  185. +  return json.loads(txt)
  186.  
  187.  
  188.  def DumpSignedJson(data, key, salt=None, key_selector=None):
  189. --
  190. 1.7.3.4