test-qapi.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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, values, prefix):
  21. print('enum %s %s' % (name, values))
  22. if prefix:
  23. print(' prefix %s' % prefix)
  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. print(' member %s: %s optional=%s' % \
  30. (m.name, m.type.name, m.optional))
  31. self._print_variants(variants)
  32. def visit_alternate_type(self, name, info, variants):
  33. print('alternate %s' % name)
  34. self._print_variants(variants)
  35. def visit_command(self, name, info, arg_type, ret_type,
  36. gen, success_response, boxed, allow_oob):
  37. print('command %s %s -> %s' % \
  38. (name, arg_type and arg_type.name, ret_type and ret_type.name))
  39. print(' gen=%s success_response=%s boxed=%s' % \
  40. (gen, success_response, boxed))
  41. def visit_event(self, name, info, arg_type, boxed):
  42. print('event %s %s' % (name, arg_type and arg_type.name))
  43. print(' boxed=%s' % boxed)
  44. @staticmethod
  45. def _print_variants(variants):
  46. if variants:
  47. print(' tag %s' % variants.tag_member.name)
  48. for v in variants.variants:
  49. print(' case %s: %s' % (v.name, v.type.name))
  50. try:
  51. schema = QAPISchema(sys.argv[1])
  52. except QAPIError as err:
  53. print(err, file=sys.stderr)
  54. exit(1)
  55. schema.visit(QAPISchemaTestVisitor())
  56. for doc in schema.docs:
  57. if doc.symbol:
  58. print('doc symbol=%s' % doc.symbol)
  59. else:
  60. print('doc freeform')
  61. print(' body=\n%s' % doc.body.text)
  62. for arg, section in doc.args.items():
  63. print(' arg=%s\n%s' % (arg, section.text))
  64. for section in doc.sections:
  65. print(' section=%s\n%s' % (section.name, section.text))