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