test-qapi.py 3.4 KB

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