2 # QAPI parser test harness
4 # Copyright (c) 2013 Red Hat Inc.
7 # Markus Armbruster <armbru@redhat.com>
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.
13 from __future__
import print_function
15 from qapi
.common
import QAPIError
, QAPISchema
, QAPISchemaVisitor
18 class QAPISchemaTestVisitor(QAPISchemaVisitor
):
20 def visit_module(self
, name
):
21 print('module %s' % name
)
23 def visit_include(self
, name
, info
):
24 print('include %s' % name
)
26 def visit_enum_type(self
, name
, info
, ifcond
, members
, prefix
):
27 print('enum %s' % name
)
29 print(' prefix %s' % prefix
)
31 print(' member %s' % m
.name
)
32 self
._print
_if
(m
.ifcond
, indent
=8)
33 self
._print
_if
(ifcond
)
35 def visit_array_type(self
, name
, info
, ifcond
, element_type
):
37 return # suppress built-in arrays
38 print('array %s %s' % (name
, element_type
.name
))
39 self
._print
_if
(ifcond
)
41 def visit_object_type(self
, name
, info
, ifcond
, base
, members
, variants
,
43 print('object %s' % name
)
45 print(' base %s' % base
.name
)
47 print(' member %s: %s optional=%s'
48 % (m
.name
, m
.type.name
, m
.optional
))
49 self
._print
_if
(m
.ifcond
, 8)
50 self
._print
_variants
(variants
)
51 self
._print
_if
(ifcond
)
54 print(' feature %s' % f
.name
)
55 self
._print
_if
(f
.ifcond
, 8)
57 def visit_alternate_type(self
, name
, info
, ifcond
, variants
):
58 print('alternate %s' % name
)
59 self
._print
_variants
(variants
)
60 self
._print
_if
(ifcond
)
62 def visit_command(self
, name
, info
, ifcond
, arg_type
, ret_type
, gen
,
63 success_response
, boxed
, allow_oob
, allow_preconfig
):
64 print('command %s %s -> %s'
65 % (name
, arg_type
and arg_type
.name
,
66 ret_type
and ret_type
.name
))
67 print(' gen=%s success_response=%s boxed=%s oob=%s preconfig=%s'
68 % (gen
, success_response
, boxed
, allow_oob
, allow_preconfig
))
69 self
._print
_if
(ifcond
)
71 def visit_event(self
, name
, info
, ifcond
, arg_type
, boxed
):
72 print('event %s %s' % (name
, arg_type
and arg_type
.name
))
73 print(' boxed=%s' % boxed
)
74 self
._print
_if
(ifcond
)
77 def _print_variants(variants
):
79 print(' tag %s' % variants
.tag_member
.name
)
80 for v
in variants
.variants
:
81 print(' case %s: %s' % (v
.name
, v
.type.name
))
82 QAPISchemaTestVisitor
._print
_if
(v
.ifcond
, indent
=8)
85 def _print_if(ifcond
, indent
=4):
87 print('%sif %s' % (' ' * indent
, ifcond
))
91 schema
= QAPISchema(sys
.argv
[1])
92 except QAPIError
as err
:
93 print(err
, file=sys
.stderr
)
96 schema
.visit(QAPISchemaTestVisitor())
98 for doc
in schema
.docs
:
100 print('doc symbol=%s' % doc
.symbol
)
102 print('doc freeform')
103 print(' body=\n%s' % doc
.body
.text
)
104 for arg
, section
in doc
.args
.items():
105 print(' arg=%s\n%s' % (arg
, section
.text
))
106 for section
in doc
.sections
:
107 print(' section=%s\n%s' % (section
.name
, section
.text
))