events.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # coding: utf-8
  2. # Abstract classes.
  3. if False: # MYPY
  4. from typing import Any, Dict, Optional, List # NOQA
  5. def CommentCheck():
  6. # type: () -> None
  7. pass
  8. class Event(object):
  9. __slots__ = 'start_mark', 'end_mark', 'comment'
  10. def __init__(self, start_mark=None, end_mark=None, comment=CommentCheck):
  11. # type: (Any, Any, Any) -> None
  12. self.start_mark = start_mark
  13. self.end_mark = end_mark
  14. # assert comment is not CommentCheck
  15. if comment is CommentCheck:
  16. comment = None
  17. self.comment = comment
  18. def __repr__(self):
  19. # type: () -> Any
  20. attributes = [
  21. key
  22. for key in ['anchor', 'tag', 'implicit', 'value', 'flow_style', 'style']
  23. if hasattr(self, key)
  24. ]
  25. arguments = ', '.join(['%s=%r' % (key, getattr(self, key)) for key in attributes])
  26. if self.comment not in [None, CommentCheck]:
  27. arguments += ', comment={!r}'.format(self.comment)
  28. return '%s(%s)' % (self.__class__.__name__, arguments)
  29. class NodeEvent(Event):
  30. __slots__ = ('anchor',)
  31. def __init__(self, anchor, start_mark=None, end_mark=None, comment=None):
  32. # type: (Any, Any, Any, Any) -> None
  33. Event.__init__(self, start_mark, end_mark, comment)
  34. self.anchor = anchor
  35. class CollectionStartEvent(NodeEvent):
  36. __slots__ = 'tag', 'implicit', 'flow_style', 'nr_items'
  37. def __init__(
  38. self,
  39. anchor,
  40. tag,
  41. implicit,
  42. start_mark=None,
  43. end_mark=None,
  44. flow_style=None,
  45. comment=None,
  46. nr_items=None,
  47. ):
  48. # type: (Any, Any, Any, Any, Any, Any, Any, Optional[int]) -> None
  49. NodeEvent.__init__(self, anchor, start_mark, end_mark, comment)
  50. self.tag = tag
  51. self.implicit = implicit
  52. self.flow_style = flow_style
  53. self.nr_items = nr_items
  54. class CollectionEndEvent(Event):
  55. __slots__ = ()
  56. # Implementations.
  57. class StreamStartEvent(Event):
  58. __slots__ = ('encoding',)
  59. def __init__(self, start_mark=None, end_mark=None, encoding=None, comment=None):
  60. # type: (Any, Any, Any, Any) -> None
  61. Event.__init__(self, start_mark, end_mark, comment)
  62. self.encoding = encoding
  63. class StreamEndEvent(Event):
  64. __slots__ = ()
  65. class DocumentStartEvent(Event):
  66. __slots__ = 'explicit', 'version', 'tags'
  67. def __init__(
  68. self,
  69. start_mark=None,
  70. end_mark=None,
  71. explicit=None,
  72. version=None,
  73. tags=None,
  74. comment=None,
  75. ):
  76. # type: (Any, Any, Any, Any, Any, Any) -> None
  77. Event.__init__(self, start_mark, end_mark, comment)
  78. self.explicit = explicit
  79. self.version = version
  80. self.tags = tags
  81. class DocumentEndEvent(Event):
  82. __slots__ = ('explicit',)
  83. def __init__(self, start_mark=None, end_mark=None, explicit=None, comment=None):
  84. # type: (Any, Any, Any, Any) -> None
  85. Event.__init__(self, start_mark, end_mark, comment)
  86. self.explicit = explicit
  87. class AliasEvent(NodeEvent):
  88. __slots__ = ()
  89. class ScalarEvent(NodeEvent):
  90. __slots__ = 'tag', 'implicit', 'value', 'style'
  91. def __init__(
  92. self,
  93. anchor,
  94. tag,
  95. implicit,
  96. value,
  97. start_mark=None,
  98. end_mark=None,
  99. style=None,
  100. comment=None,
  101. ):
  102. # type: (Any, Any, Any, Any, Any, Any, Any, Any) -> None
  103. NodeEvent.__init__(self, anchor, start_mark, end_mark, comment)
  104. self.tag = tag
  105. self.implicit = implicit
  106. self.value = value
  107. self.style = style
  108. class SequenceStartEvent(CollectionStartEvent):
  109. __slots__ = ()
  110. class SequenceEndEvent(CollectionEndEvent):
  111. __slots__ = ()
  112. class MappingStartEvent(CollectionStartEvent):
  113. __slots__ = ()
  114. class MappingEndEvent(CollectionEndEvent):
  115. __slots__ = ()