source.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #
  2. # QAPI frontend source file info
  3. #
  4. # Copyright (c) 2019 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.
  10. # See the COPYING file in the top-level directory.
  11. import copy
  12. from typing import List, Optional, TypeVar
  13. class QAPISchemaPragma:
  14. # Replace with @dataclass in Python 3.7+
  15. # pylint: disable=too-few-public-methods
  16. def __init__(self) -> None:
  17. # Are documentation comments required?
  18. self.doc_required = False
  19. # Commands whose names may use '_'
  20. self.command_name_exceptions: List[str] = []
  21. # Commands allowed to return a non-dictionary
  22. self.command_returns_exceptions: List[str] = []
  23. # Types, commands, and events with undocumented members
  24. self.documentation_exceptions: List[str] = []
  25. # Types whose member names may violate case conventions
  26. self.member_name_exceptions: List[str] = []
  27. class QAPISourceInfo:
  28. T = TypeVar('T', bound='QAPISourceInfo')
  29. def __init__(self, fname: str, parent: Optional['QAPISourceInfo']):
  30. self.fname = fname
  31. self.line = 1
  32. self.parent = parent
  33. self.pragma: QAPISchemaPragma = (
  34. parent.pragma if parent else QAPISchemaPragma()
  35. )
  36. self.defn_meta: Optional[str] = None
  37. self.defn_name: Optional[str] = None
  38. def set_defn(self, meta: str, name: str) -> None:
  39. self.defn_meta = meta
  40. self.defn_name = name
  41. def next_line(self: T, n: int = 1) -> T:
  42. info = copy.copy(self)
  43. info.line += n
  44. return info
  45. def loc(self) -> str:
  46. return f"{self.fname}:{self.line}"
  47. def in_defn(self) -> str:
  48. if self.defn_name:
  49. return "%s: In %s '%s':\n" % (self.fname,
  50. self.defn_meta, self.defn_name)
  51. return ''
  52. def include_path(self) -> str:
  53. ret = ''
  54. parent = self.parent
  55. while parent:
  56. ret = 'In file included from %s:\n' % parent.loc() + ret
  57. parent = parent.parent
  58. return ret
  59. def __str__(self) -> str:
  60. return self.include_path() + self.in_defn() + self.loc()