test-qapi.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 QAPISchema, QAPISchemaVisitor
  15. class QAPISchemaTestVisitor(QAPISchemaVisitor):
  16. def visit_enum_type(self, name, info, values, prefix):
  17. print('enum %s %s' % (name, values))
  18. if prefix:
  19. print(' prefix %s' % prefix)
  20. def visit_object_type(self, name, info, base, members, variants):
  21. print('object %s' % name)
  22. if base:
  23. print(' base %s' % base.name)
  24. for m in members:
  25. print(' member %s: %s optional=%s' % \
  26. (m.name, m.type.name, m.optional))
  27. self._print_variants(variants)
  28. def visit_alternate_type(self, name, info, variants):
  29. print('alternate %s' % name)
  30. self._print_variants(variants)
  31. def visit_command(self, name, info, arg_type, ret_type,
  32. gen, success_response, boxed):
  33. print('command %s %s -> %s' % \
  34. (name, arg_type and arg_type.name, ret_type and ret_type.name))
  35. print(' gen=%s success_response=%s boxed=%s' % \
  36. (gen, success_response, boxed))
  37. def visit_event(self, name, info, arg_type, boxed):
  38. print('event %s %s' % (name, arg_type and arg_type.name))
  39. print(' boxed=%s' % boxed)
  40. @staticmethod
  41. def _print_variants(variants):
  42. if variants:
  43. print(' tag %s' % variants.tag_member.name)
  44. for v in variants.variants:
  45. print(' case %s: %s' % (v.name, v.type.name))
  46. schema = QAPISchema(sys.argv[1])
  47. schema.visit(QAPISchemaTestVisitor())
  48. for doc in schema.docs:
  49. if doc.symbol:
  50. print('doc symbol=%s' % doc.symbol)
  51. else:
  52. print('doc freeform')
  53. print(' body=\n%s' % doc.body.text)
  54. for arg, section in doc.args.items():
  55. print(' arg=%s\n%s' % (arg, section.text))
  56. for section in doc.sections:
  57. print(' section=%s\n%s' % (section.name, section.text))