test-qapi.py 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#
# QAPI parser test harness
#
# Copyright (c) 2013 Red Hat Inc.
#
# Authors:
#  Markus Armbruster <armbru@redhat.com>
#
# This work is licensed under the terms of the GNU GPL, version 2 or later.
# See the COPYING file in the top-level directory.
#

13
from __future__ import print_function
14
import sys
15
from qapi.common import QAPIError, QAPISchema, QAPISchemaVisitor
16

17 18

class QAPISchemaTestVisitor(QAPISchemaVisitor):
19 20 21 22 23 24 25

    def visit_module(self, name):
        print('module %s' % name)

    def visit_include(self, name, info):
        print('include %s' % name)

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 37
            print('    member %s: %s optional=%s' % \
                  (m.name, m.type.name, m.optional))
38 39 40
        self._print_variants(variants)

    def visit_alternate_type(self, name, info, variants):
41
        print('alternate %s' % name)
42 43 44
        self._print_variants(variants)

    def visit_command(self, name, info, arg_type, ret_type,
45
                      gen, success_response, boxed):
46 47 48 49
        print('command %s %s -> %s' % \
              (name, arg_type and arg_type.name, ret_type and ret_type.name))
        print('   gen=%s success_response=%s boxed=%s' % \
              (gen, success_response, boxed))
50

51
    def visit_event(self, name, info, arg_type, boxed):
52 53
        print('event %s %s' % (name, arg_type and arg_type.name))
        print('   boxed=%s' % boxed)
54 55 56 57

    @staticmethod
    def _print_variants(variants):
        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 63 64 65 66 67 68

try:
    schema = QAPISchema(sys.argv[1])
except QAPIError as err:
    print(err, file=sys.stderr)
    exit(1)

69
schema.visit(QAPISchemaTestVisitor())
70 71 72

for doc in schema.docs:
    if doc.symbol:
73
        print('doc symbol=%s' % doc.symbol)
74
    else:
75 76
        print('doc freeform')
    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))