source.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 whose member names may violate case conventions
  24. self.member_name_exceptions: List[str] = []
  25. class QAPISourceInfo:
  26. T = TypeVar('T', bound='QAPISourceInfo')
  27. def __init__(self, fname: str, line: int,
  28. parent: Optional['QAPISourceInfo']):
  29. self.fname = fname
  30. self.line = line
  31. self.parent = parent
  32. self.pragma: QAPISchemaPragma = (
  33. parent.pragma if parent else QAPISchemaPragma()
  34. )
  35. self.defn_meta: Optional[str] = None
  36. self.defn_name: Optional[str] = None
  37. def set_defn(self, meta: str, name: str) -> None:
  38. self.defn_meta = meta
  39. self.defn_name = name
  40. def next_line(self: T) -> T:
  41. info = copy.copy(self)
  42. info.line += 1
  43. return info
  44. def loc(self) -> str:
  45. ret = self.fname
  46. if self.line is not None:
  47. ret += ':%d' % self.line
  48. return ret
  49. def in_defn(self) -> str:
  50. if self.defn_name:
  51. return "%s: In %s '%s':\n" % (self.fname,
  52. self.defn_meta, self.defn_name)
  53. return ''
  54. def include_path(self) -> str:
  55. ret = ''
  56. parent = self.parent
  57. while parent:
  58. ret = 'In file included from %s:\n' % parent.loc() + ret
  59. parent = parent.parent
  60. return ret
  61. def __str__(self) -> str:
  62. return self.include_path() + self.in_defn() + self.loc()