xref: /qemu/tests/qapi-schema/test-qapi.py (revision e7b3af81)
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 __future__ import print_function
14import sys
15from qapi.common import QAPIError, QAPISchema, QAPISchemaVisitor
16
17
18class QAPISchemaTestVisitor(QAPISchemaVisitor):
19
20    def visit_module(self, name):
21        print('module %s' % name)
22
23    def visit_include(self, name, info):
24        print('include %s' % name)
25
26    def visit_enum_type(self, name, info, values, prefix):
27        print('enum %s %s' % (name, values))
28        if prefix:
29            print('    prefix %s' % prefix)
30
31    def visit_object_type(self, name, info, base, members, variants):
32        print('object %s' % name)
33        if base:
34            print('    base %s' % base.name)
35        for m in members:
36            print('    member %s: %s optional=%s' % \
37                  (m.name, m.type.name, m.optional))
38        self._print_variants(variants)
39
40    def visit_alternate_type(self, name, info, variants):
41        print('alternate %s' % name)
42        self._print_variants(variants)
43
44    def visit_command(self, name, info, arg_type, ret_type, gen,
45                      success_response, boxed, allow_oob, allow_preconfig):
46        print('command %s %s -> %s' % \
47              (name, arg_type and arg_type.name, ret_type and ret_type.name))
48        print('   gen=%s success_response=%s boxed=%s oob=%s preconfig=%s' % \
49              (gen, success_response, boxed, allow_oob, allow_preconfig))
50
51    def visit_event(self, name, info, arg_type, boxed):
52        print('event %s %s' % (name, arg_type and arg_type.name))
53        print('   boxed=%s' % boxed)
54
55    @staticmethod
56    def _print_variants(variants):
57        if variants:
58            print('    tag %s' % variants.tag_member.name)
59            for v in variants.variants:
60                print('    case %s: %s' % (v.name, v.type.name))
61
62
63try:
64    schema = QAPISchema(sys.argv[1])
65except QAPIError as err:
66    print(err, file=sys.stderr)
67    exit(1)
68
69schema.visit(QAPISchemaTestVisitor())
70
71for doc in schema.docs:
72    if doc.symbol:
73        print('doc symbol=%s' % doc.symbol)
74    else:
75        print('doc freeform')
76    print('    body=\n%s' % doc.body.text)
77    for arg, section in doc.args.items():
78        print('    arg=%s\n%s' % (arg, section.text))
79    for section in doc.sections:
80        print('    section=%s\n%s' % (section.name, section.text))
81