From patchwork Fri Oct 19 02:42:07 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 192510 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 35E4C2C007A for ; Fri, 19 Oct 2012 14:47:36 +1100 (EST) Received: from localhost ([::1]:58057 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TP2Z4-0006da-7a for incoming@patchwork.ozlabs.org; Thu, 18 Oct 2012 22:43:58 -0400 Received: from eggs.gnu.org ([208.118.235.92]:42586) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TP2Y1-0004Pm-El for qemu-devel@nongnu.org; Thu, 18 Oct 2012 22:42:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TP2Xz-0002tl-N6 for qemu-devel@nongnu.org; Thu, 18 Oct 2012 22:42:53 -0400 Received: from mail-ob0-f173.google.com ([209.85.214.173]:58994) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TP2Xz-0002eS-Fj for qemu-devel@nongnu.org; Thu, 18 Oct 2012 22:42:51 -0400 Received: by mail-ob0-f173.google.com with SMTP id wc18so3049obb.4 for ; Thu, 18 Oct 2012 19:42:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=I3MlaxbtyLy4da2GYSZJMr0vXMhix0tFv4SuD/QyZzE=; b=BVwPEvvAuZZxnHagW3XBBsWuNrDfZuJUYyWaMcVeuVh0dbrx59d4mdUhPF4lsjmDUX llkfIKeUlC4G7ISLWY6Y4QIpYFh0UOqQchT0/ETLQBm1LnRtAfRNOmlVN1wqI0jFNk7n s6wXDzdcmurLs1wAx66auSDsMv7d2dpsU11tLNA4mKSqNklV1YVRnxi/sYHR3rHwlten ypuNKXyeBhkCA3jmGkbGbgCxoQJM9MnaGXZ0fHGPCwcVy8mIIc8e6Pu51mUgqfeIxfT1 FX2vstpwLwfQeWNhjVZmwgMT6Ff9VgUeSpegnlcDCblMzW08UCT0GBsUoyJepI6KPk6K jTVQ== Received: by 10.60.172.171 with SMTP id bd11mr2513930oec.4.1350614571175; Thu, 18 Oct 2012 19:42:51 -0700 (PDT) Received: from loki.austin.ibm.com ([32.97.110.59]) by mx.google.com with ESMTPS id m6sm475144obk.3.2012.10.18.19.42.49 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 18 Oct 2012 19:42:50 -0700 (PDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Thu, 18 Oct 2012 21:42:07 -0500 Message-Id: <1350614540-28583-14-git-send-email-mdroth@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1350614540-28583-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1350614540-28583-1-git-send-email-mdroth@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.214.173 Cc: kwolf@redhat.com, peter.maydell@linaro.org, aliguori@us.ibm.com, blauwirbel@gmail.com, pbonzini@redhat.com Subject: [Qemu-devel] [PATCH 13/26] qapi: ordereddict, add to_json() method X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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)