test-qapi.py 2.2 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 QAPISchema, QAPISchemaVisitor
16

17 18 19

class QAPISchemaTestVisitor(QAPISchemaVisitor):
    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 30
            print('    member %s: %s optional=%s' % \
                  (m.name, m.type.name, m.optional))
31 32 33
        self._print_variants(variants)

    def visit_alternate_type(self, name, info, variants):
34
        print('alternate %s' % name)
35 36 37
        self._print_variants(variants)

    def visit_command(self, name, info, arg_type, ret_type,
38
                      gen, success_response, boxed):
39 40 41 42
        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))
43

44
    def visit_event(self, name, info, arg_type, boxed):
45 46
        print('event %s %s' % (name, arg_type and arg_type.name))
        print('   boxed=%s' % boxed)
47 48 49 50

    @staticmethod
    def _print_variants(variants):
        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 56

schema = QAPISchema(sys.argv[1])
schema.visit(QAPISchemaTestVisitor())
57 58 59

for doc in schema.docs:
    if doc.symbol:
60
        print('doc symbol=%s' % doc.symbol)
61
    else:
62 63
        print('doc freeform')
    print('    body=\n%s' % doc.body.text)
64
    for arg, section in doc.args.items():
65
        print('    arg=%s\n%s' % (arg, section.text))
66
    for section in doc.sections:
67
        print('    section=%s\n%s' % (section.name, section.text))