xref: /openbmc/qemu/tests/qapi-schema/test-qapi.py (revision ad30c0b0)
1#
2# QAPI parser test harness
3#
4# Copyright (c) 2013 Red Hat Inc.
5#
6# Authors:
7#  Markus Armbruster <armbru@redhat.com>
8#
9# This work is licensed under the terms of the GNU GPL, version 2 or later.
10# See the COPYING file in the top-level directory.
11#
12
13from qapi import *
14from pprint import pprint
15import os
16import sys
17
18
19class QAPISchemaTestVisitor(QAPISchemaVisitor):
20    def visit_enum_type(self, name, info, values, prefix):
21        print 'enum %s %s' % (name, values)
22        if prefix:
23            print '    prefix %s' % prefix
24
25    def visit_object_type(self, name, info, base, members, variants):
26        print 'object %s' % name
27        if base:
28            print '    base %s' % base.name
29        for m in members:
30            print '    member %s: %s optional=%s' % \
31                (m.name, m.type.name, m.optional)
32        self._print_variants(variants)
33
34    def visit_alternate_type(self, name, info, variants):
35        print 'alternate %s' % name
36        self._print_variants(variants)
37
38    def visit_command(self, name, info, arg_type, ret_type,
39                      gen, success_response):
40        print 'command %s %s -> %s' % \
41            (name, arg_type and arg_type.name, ret_type and ret_type.name)
42        print '   gen=%s success_response=%s' % (gen, success_response)
43
44    def visit_event(self, name, info, arg_type):
45        print 'event %s %s' % (name, arg_type and arg_type.name)
46
47    @staticmethod
48    def _print_variants(variants):
49        if variants:
50            if variants.tag_name:
51                print '    tag %s' % variants.tag_name
52            for v in variants.variants:
53                print '    case %s: %s' % (v.name, v.type.name)
54
55schema = QAPISchema(sys.argv[1])
56schema.visit(QAPISchemaTestVisitor())
57