2
0

source.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. import sys
  13. from typing import List, Optional, TypeVar
  14. class QAPISchemaPragma:
  15. # Replace with @dataclass in Python 3.7+
  16. # pylint: disable=too-few-public-methods
  17. def __init__(self) -> None:
  18. # Are documentation comments required?
  19. self.doc_required = False
  20. # Commands allowed to return a non-dictionary
  21. self.command_returns_exceptions: List[str] = []
  22. # Types whose member names may violate case conventions
  23. self.member_name_exceptions: List[str] = []
  24. class QAPISourceInfo:
  25. T = TypeVar('T', bound='QAPISourceInfo')
  26. def __init__(self, fname: str, line: int,
  27. parent: Optional['QAPISourceInfo']):
  28. self.fname = fname
  29. self.line = line
  30. self.parent = parent
  31. self.pragma: QAPISchemaPragma = (
  32. parent.pragma if parent else QAPISchemaPragma()
  33. )
  34. self.defn_meta: Optional[str] = None
  35. self.defn_name: Optional[str] = None
  36. def set_defn(self, meta: str, name: str) -> None:
  37. self.defn_meta = meta
  38. self.defn_name = name
  39. def next_line(self: T) -> T:
  40. info = copy.copy(self)
  41. info.line += 1
  42. return info
  43. def loc(self) -> str:
  44. if self.fname is None:
  45. return sys.argv[0]
  46. ret = self.fname
  47. if self.line is not None:
  48. ret += ':%d' % self.line
  49. return ret
  50. def in_defn(self) -> str:
  51. if self.defn_name:
  52. return "%s: In %s '%s':\n" % (self.fname,
  53. self.defn_meta, self.defn_name)
  54. return ''
  55. def include_path(self) -> str:
  56. ret = ''
  57. parent = self.parent
  58. while parent:
  59. ret = 'In file included from %s:\n' % parent.loc() + ret
  60. parent = parent.parent
  61. return ret
  62. def __str__(self) -> str:
  63. return self.include_path() + self.in_defn() + self.loc()