test-qapi.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_object_type(self, name, info, ifcond, base, members, variants):
  29. print('object %s' % name)
  30. if base:
  31. print(' base %s' % base.name)
  32. for m in members:
  33. print(' member %s: %s optional=%s'
  34. % (m.name, m.type.name, m.optional))
  35. self._print_variants(variants)
  36. self._print_if(ifcond)
  37. def visit_alternate_type(self, name, info, ifcond, variants):
  38. print('alternate %s' % name)
  39. self._print_variants(variants)
  40. self._print_if(ifcond)
  41. def visit_command(self, name, info, ifcond, arg_type, ret_type, gen,
  42. success_response, boxed, allow_oob, allow_preconfig):
  43. print('command %s %s -> %s'
  44. % (name, arg_type and arg_type.name,
  45. ret_type and ret_type.name))
  46. print(' gen=%s success_response=%s boxed=%s oob=%s preconfig=%s'
  47. % (gen, success_response, boxed, allow_oob, allow_preconfig))
  48. self._print_if(ifcond)
  49. def visit_event(self, name, info, ifcond, arg_type, boxed):
  50. print('event %s %s' % (name, arg_type and arg_type.name))
  51. print(' boxed=%s' % boxed)
  52. self._print_if(ifcond)
  53. @staticmethod
  54. def _print_variants(variants):
  55. if variants:
  56. print(' tag %s' % variants.tag_member.name)
  57. for v in variants.variants:
  58. print(' case %s: %s' % (v.name, v.type.name))
  59. @staticmethod
  60. def _print_if(ifcond, indent=4):
  61. if ifcond:
  62. print('%sif %s' % (' ' * indent, ifcond))
  63. try:
  64. schema = QAPISchema(sys.argv[1])
  65. except QAPIError as err:
  66. print(err, file=sys.stderr)
  67. exit(1)
  68. schema.visit(QAPISchemaTestVisitor())
  69. for doc in schema.docs:
  70. if doc.symbol:
  71. print('doc symbol=%s' % doc.symbol)
  72. else:
  73. print('doc freeform')
  74. print(' body=\n%s' % doc.body.text)
  75. for arg, section in doc.args.items():
  76. print(' arg=%s\n%s' % (arg, section.text))
  77. for section in doc.sections:
  78. print(' section=%s\n%s' % (section.name, section.text))