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