You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

267 line
7.1 KiB

  1. from __future__ import annotations
  2. # Abstract classes.
  3. if False: # MYPY
  4. from typing import Any, Dict, Optional, List # NOQA
  5. from ruamel.yaml.tag import Tag
  6. SHOW_LINES = False
  7. def CommentCheck() -> None:
  8. pass
  9. class Event:
  10. __slots__ = 'start_mark', 'end_mark', 'comment'
  11. crepr = 'Unspecified Event'
  12. def __init__(
  13. self, start_mark: Any = None, end_mark: Any = None, comment: Any = CommentCheck,
  14. ) -> None:
  15. self.start_mark = start_mark
  16. self.end_mark = end_mark
  17. # assert comment is not CommentCheck
  18. if comment is CommentCheck:
  19. comment = None
  20. self.comment = comment
  21. def __repr__(self) -> Any:
  22. if True:
  23. arguments = []
  24. if hasattr(self, 'value'):
  25. # if you use repr(getattr(self, 'value')) then flake8 complains about
  26. # abuse of getattr with a constant. When you change to self.value
  27. # then mypy throws an error
  28. arguments.append(repr(self.value))
  29. for key in ['anchor', 'tag', 'implicit', 'flow_style', 'style']:
  30. v = getattr(self, key, None)
  31. if v is not None:
  32. arguments.append(f'{key!s}={v!r}')
  33. if self.comment not in [None, CommentCheck]:
  34. arguments.append(f'comment={self.comment!r}')
  35. if SHOW_LINES:
  36. arguments.append(
  37. f'({self.start_mark.line}:{self.start_mark.column}/'
  38. f'{self.end_mark.line}:{self.end_mark.column})',
  39. )
  40. arguments = ', '.join(arguments) # type: ignore
  41. else:
  42. attributes = [
  43. key
  44. for key in ['anchor', 'tag', 'implicit', 'value', 'flow_style', 'style']
  45. if hasattr(self, key)
  46. ]
  47. arguments = ', '.join([f'{key!s}={getattr(self, key)!r}' for key in attributes])
  48. if self.comment not in [None, CommentCheck]:
  49. arguments += f', comment={self.comment!r}'
  50. return f'{self.__class__.__name__!s}({arguments!s})'
  51. def compact_repr(self) -> str:
  52. return f'{self.crepr}'
  53. class NodeEvent(Event):
  54. __slots__ = ('anchor',)
  55. def __init__(
  56. self, anchor: Any, start_mark: Any = None, end_mark: Any = None, comment: Any = None,
  57. ) -> None:
  58. Event.__init__(self, start_mark, end_mark, comment)
  59. self.anchor = anchor
  60. class CollectionStartEvent(NodeEvent):
  61. __slots__ = 'ctag', 'implicit', 'flow_style', 'nr_items'
  62. def __init__(
  63. self,
  64. anchor: Any,
  65. tag: Any,
  66. implicit: Any,
  67. start_mark: Any = None,
  68. end_mark: Any = None,
  69. flow_style: Any = None,
  70. comment: Any = None,
  71. nr_items: Optional[int] = None,
  72. ) -> None:
  73. NodeEvent.__init__(self, anchor, start_mark, end_mark, comment)
  74. self.ctag = tag
  75. self.implicit = implicit
  76. self.flow_style = flow_style
  77. self.nr_items = nr_items
  78. @property
  79. def tag(self) -> Optional[str]:
  80. return None if self.ctag is None else str(self.ctag)
  81. class CollectionEndEvent(Event):
  82. __slots__ = ()
  83. # Implementations.
  84. class StreamStartEvent(Event):
  85. __slots__ = ('encoding',)
  86. crepr = '+STR'
  87. def __init__(
  88. self,
  89. start_mark: Any = None,
  90. end_mark: Any = None,
  91. encoding: Any = None,
  92. comment: Any = None,
  93. ) -> None:
  94. Event.__init__(self, start_mark, end_mark, comment)
  95. self.encoding = encoding
  96. class StreamEndEvent(Event):
  97. __slots__ = ()
  98. crepr = '-STR'
  99. class DocumentStartEvent(Event):
  100. __slots__ = 'explicit', 'version', 'tags'
  101. crepr = '+DOC'
  102. def __init__(
  103. self,
  104. start_mark: Any = None,
  105. end_mark: Any = None,
  106. explicit: Any = None,
  107. version: Any = None,
  108. tags: Any = None,
  109. comment: Any = None,
  110. ) -> None:
  111. Event.__init__(self, start_mark, end_mark, comment)
  112. self.explicit = explicit
  113. self.version = version
  114. self.tags = tags
  115. def compact_repr(self) -> str:
  116. start = ' ---' if self.explicit else ''
  117. return f'{self.crepr}{start}'
  118. class DocumentEndEvent(Event):
  119. __slots__ = ('explicit',)
  120. crepr = '-DOC'
  121. def __init__(
  122. self,
  123. start_mark: Any = None,
  124. end_mark: Any = None,
  125. explicit: Any = None,
  126. comment: Any = None,
  127. ) -> None:
  128. Event.__init__(self, start_mark, end_mark, comment)
  129. self.explicit = explicit
  130. def compact_repr(self) -> str:
  131. end = ' ...' if self.explicit else ''
  132. return f'{self.crepr}{end}'
  133. class AliasEvent(NodeEvent):
  134. __slots__ = 'style'
  135. crepr = '=ALI'
  136. def __init__(
  137. self,
  138. anchor: Any,
  139. start_mark: Any = None,
  140. end_mark: Any = None,
  141. style: Any = None,
  142. comment: Any = None,
  143. ) -> None:
  144. NodeEvent.__init__(self, anchor, start_mark, end_mark, comment)
  145. self.style = style
  146. def compact_repr(self) -> str:
  147. return f'{self.crepr} *{self.anchor}'
  148. class ScalarEvent(NodeEvent):
  149. __slots__ = 'ctag', 'implicit', 'value', 'style'
  150. crepr = '=VAL'
  151. def __init__(
  152. self,
  153. anchor: Any,
  154. tag: Any,
  155. implicit: Any,
  156. value: Any,
  157. start_mark: Any = None,
  158. end_mark: Any = None,
  159. style: Any = None,
  160. comment: Any = None,
  161. ) -> None:
  162. NodeEvent.__init__(self, anchor, start_mark, end_mark, comment)
  163. self.ctag = tag
  164. self.implicit = implicit
  165. self.value = value
  166. self.style = style
  167. @property
  168. def tag(self) -> Optional[str]:
  169. return None if self.ctag is None else str(self.ctag)
  170. @tag.setter
  171. def tag(self, val: Any) -> None:
  172. if isinstance(val, str):
  173. val = Tag(suffix=val)
  174. self.ctag = val
  175. def compact_repr(self) -> str:
  176. style = ':' if self.style is None else self.style
  177. anchor = f'&{self.anchor} ' if self.anchor else ''
  178. tag = f'<{self.tag!s}> ' if self.tag else ''
  179. value = self.value
  180. for ch, rep in [
  181. ('\\', '\\\\'),
  182. ('\t', '\\t'),
  183. ('\n', '\\n'),
  184. ('\a', ''), # remove from folded
  185. ('\r', '\\r'),
  186. ('\b', '\\b'),
  187. ]:
  188. value = value.replace(ch, rep)
  189. return f'{self.crepr} {anchor}{tag}{style}{value}'
  190. class SequenceStartEvent(CollectionStartEvent):
  191. __slots__ = ()
  192. crepr = '+SEQ'
  193. def compact_repr(self) -> str:
  194. flow = ' []' if self.flow_style else ''
  195. anchor = f' &{self.anchor}' if self.anchor else ''
  196. tag = f' <{self.tag!s}>' if self.tag else ''
  197. return f'{self.crepr}{flow}{anchor}{tag}'
  198. class SequenceEndEvent(CollectionEndEvent):
  199. __slots__ = ()
  200. crepr = '-SEQ'
  201. class MappingStartEvent(CollectionStartEvent):
  202. __slots__ = ()
  203. crepr = '+MAP'
  204. def compact_repr(self) -> str:
  205. flow = ' {}' if self.flow_style else ''
  206. anchor = f' &{self.anchor}' if self.anchor else ''
  207. tag = f' <{self.tag!s}>' if self.tag else ''
  208. return f'{self.crepr}{flow}{anchor}{tag}'
  209. class MappingEndEvent(CollectionEndEvent):
  210. __slots__ = ()
  211. crepr = '-MAP'