events.py 7.1 KB

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