source.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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, parent: Optional['QAPISourceInfo']):
  28. self.fname = fname
  29. self.line = 1
  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. return f"{self.fname}:{self.line}"
  45. def in_defn(self) -> str:
  46. if self.defn_name:
  47. return "%s: In %s '%s':\n" % (self.fname,
  48. self.defn_meta, self.defn_name)
  49. return ''
  50. def include_path(self) -> str:
  51. ret = ''
  52. parent = self.parent
  53. while parent:
  54. ret = 'In file included from %s:\n' % parent.loc() + ret
  55. parent = parent.parent
  56. return ret
  57. def __str__(self) -> str:
  58. return self.include_path() + self.in_defn() + self.loc()