2
0

source.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 whose names may use '_'
  21. self.command_name_exceptions: List[str] = []
  22. # Commands allowed to return a non-dictionary
  23. self.command_returns_exceptions: List[str] = []
  24. # Types whose member names may violate case conventions
  25. self.member_name_exceptions: List[str] = []
  26. class QAPISourceInfo:
  27. T = TypeVar('T', bound='QAPISourceInfo')
  28. def __init__(self, fname: str, line: int,
  29. parent: Optional['QAPISourceInfo']):
  30. self.fname = fname
  31. self.line = line
  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) -> T:
  42. info = copy.copy(self)
  43. info.line += 1
  44. return info
  45. def loc(self) -> str:
  46. if self.fname is None:
  47. return sys.argv[0]
  48. ret = self.fname
  49. if self.line is not None:
  50. ret += ':%d' % self.line
  51. return ret
  52. def in_defn(self) -> str:
  53. if self.defn_name:
  54. return "%s: In %s '%s':\n" % (self.fname,
  55. self.defn_meta, self.defn_name)
  56. return ''
  57. def include_path(self) -> str:
  58. ret = ''
  59. parent = self.parent
  60. while parent:
  61. ret = 'In file included from %s:\n' % parent.loc() + ret
  62. parent = parent.parent
  63. return ret
  64. def __str__(self) -> str:
  65. return self.include_path() + self.in_defn() + self.loc()