From d583592222949b10518238124afe7856bb49435e Mon Sep 17 00:00:00 2001 From: Michael Hanselmann <hansmi@google.com> Date: Mon, 8 Oct 2007 10:48:35 +0000 Subject: [PATCH] Use indenting if supported by simplejson. Reviewed-by: iustinp --- lib/objects.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/objects.py b/lib/objects.py index f85de161f..c6b968e41 100644 --- a/lib/objects.py +++ b/lib/objects.py @@ -28,9 +28,9 @@ pass to and from external parties. import simplejson -from cStringIO import StringIO import ConfigParser import re +from cStringIO import StringIO from ganeti import errors from ganeti import constants @@ -40,6 +40,14 @@ __all__ = ["ConfigObject", "ConfigData", "NIC", "Disk", "Instance", "OS", "Node", "Cluster"] +# Check whether the simplejson module supports indentation +_JSON_INDENT = 2 +try: + simplejson.dumps(1, indent=_JSON_INDENT) +except TypeError: + _JSON_INDENT = None + + class ConfigObject(object): """A generic config object. @@ -86,7 +94,11 @@ class ConfigObject(object): """Dump to a file object. """ - simplejson.dump(self.ToDict(), fobj) + data = self.ToDict() + if _JSON_INDENT is None: + simplejson.dump(data, fobj) + else: + simplejson.dump(data, fobj, indent=_JSON_INDENT) @classmethod def Load(cls, fobj): -- GitLab