From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:42411) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TMmWE-0005pC-1I for qemu-devel@nongnu.org; Fri, 12 Oct 2012 17:11:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TMmWC-00060p-EX for qemu-devel@nongnu.org; Fri, 12 Oct 2012 17:11:41 -0400 Received: from mail-ia0-f173.google.com ([209.85.210.173]:53532) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TMmWC-0005rN-9A for qemu-devel@nongnu.org; Fri, 12 Oct 2012 17:11:40 -0400 Received: by mail-ia0-f173.google.com with SMTP id m10so2313014iam.4 for ; Fri, 12 Oct 2012 14:11:40 -0700 (PDT) Sender: fluxion From: Michael Roth Date: Fri, 12 Oct 2012 16:10:55 -0500 Message-Id: <1350076268-18461-14-git-send-email-mdroth@linux.vnet.ibm.com> In-Reply-To: <1350076268-18461-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1350076268-18461-1-git-send-email-mdroth@linux.vnet.ibm.com> Subject: [Qemu-devel] [PATCH v4 13/26] qapi: ordereddict, add to_json() method List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, peter.maydell@linaro.org, aliguori@us.ibm.com, blauwirbel@gmail.com, pbonzini@redhat.com This allows OrderedDict() instances to be [pretty-]printed to QAPI-compatible JSON structures with field ordering preserved. This is useful for QIDL, which generates schemas programatically and exposes them to users in various ways. Signed-off-by: Michael Roth --- scripts/ordereddict.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/scripts/ordereddict.py b/scripts/ordereddict.py index 7242b50..f1f8c71 100644 --- a/scripts/ordereddict.py +++ b/scripts/ordereddict.py @@ -23,6 +23,21 @@ from UserDict import DictMixin class OrderedDict(dict, DictMixin): + class Indent(object): + def __init__(self, enable=True, initial_spacing=4, initial_level=0): + self.enable = enable + self.level = initial_level + self.spacing = initial_spacing + def inc(self, n=1): + self.level += n + return self.level + def dec(self, n=1): + self.level -= n + return self.level + def prefix(self): + if self.enable: + return " " * (self.spacing * self.level) + return "" def __init__(self, *args, **kwds): if len(args) > 1: @@ -125,3 +140,39 @@ class OrderedDict(dict, DictMixin): def __ne__(self, other): return not self == other + + def __obj_to_json(self, node, pretty=True, indent_spacing=4, indent_level=0): + i = self.Indent(pretty, indent_spacing, indent_level) + text = "" + + if isinstance(node, OrderedDict): + if indent_level == 0: + text += "%s{\n" % i.prefix() + else: + text += "{\n" + i.inc() + for (k, v) in node.items(): + text += "%s'%s': %s" % (i.prefix(), k, + self.__obj_to_json(v, pretty, i.spacing, i.level)) + i.dec() + text += "%s}" % i.prefix() + elif isinstance(node, list): + text += "[\n" + i.inc() + for e in node: + text += "%s%s" % (i.prefix(), + self.__obj_to_json(e, pretty, i.spacing, i.level)) + i.dec() + text += "%s]" % i.prefix() + else: + text += repr(node) + + if indent_level > 0: + text += ",\n" + + if not pretty: + return text.replace("\n", " ") + return text + + def to_json(self, pretty=True, indent_spacing=4, indent_level=0): + return self.__obj_to_json(self) -- 1.7.9.5