| 1 |
# coding: utf-8 |
| 2 |
|
| 3 |
from __future__ import absolute_import, print_function |
| 4 |
|
| 5 |
import warnings |
| 6 |
|
| 7 |
from strictyaml.ruamel.error import MarkedYAMLError, ReusedAnchorWarning |
| 8 |
from strictyaml.ruamel.compat import utf8, nprint, nprintf # NOQA |
| 9 |
|
| 10 |
from strictyaml.ruamel.events import ( |
| 11 |
StreamStartEvent, |
| 12 |
StreamEndEvent, |
| 13 |
MappingStartEvent, |
| 14 |
MappingEndEvent, |
| 15 |
SequenceStartEvent, |
| 16 |
SequenceEndEvent, |
| 17 |
AliasEvent, |
| 18 |
ScalarEvent, |
| 19 |
) |
| 20 |
from strictyaml.ruamel.nodes import MappingNode, ScalarNode, SequenceNode |
| 21 |
|
| 22 |
if False: # MYPY |
| 23 |
from typing import Any, Dict, Optional, List # NOQA |
| 24 |
|
| 25 |
__all__ = ["Composer", "ComposerError"] |
| 26 |
|
| 27 |
|
| 28 |
class ComposerError(MarkedYAMLError): |
| 29 |
pass |
| 30 |
|
| 31 |
|
| 32 |
class Composer(object): |
| 33 |
def __init__(self, loader=None): |
| 34 |
# type: (Any) -> None |
| 35 |
self.loader = loader |
| 36 |
if self.loader is not None and getattr(self.loader, "_composer", None) is None: |
| 37 |
self.loader._composer = self |
| 38 |
self.anchors = {} # type: Dict[Any, Any] |
| 39 |
|
| 40 |
@property |
| 41 |
def parser(self): |
| 42 |
# type: () -> Any |
| 43 |
if hasattr(self.loader, "typ"): |
| 44 |
self.loader.parser |
| 45 |
return self.loader._parser |
| 46 |
|
| 47 |
@property |
| 48 |
def resolver(self): |
| 49 |
# type: () -> Any |
| 50 |
# assert self.loader._resolver is not None |
| 51 |
if hasattr(self.loader, "typ"): |
| 52 |
self.loader.resolver |
| 53 |
return self.loader._resolver |
| 54 |
|
| 55 |
def check_node(self): |
| 56 |
# type: () -> Any |
| 57 |
# Drop the STREAM-START event. |
| 58 |
if self.parser.check_event(StreamStartEvent): |
| 59 |
self.parser.get_event() |
| 60 |
|
| 61 |
# If there are more documents available? |
| 62 |
return not self.parser.check_event(StreamEndEvent) |
| 63 |
|
| 64 |
def get_node(self): |
| 65 |
# type: () -> Any |
| 66 |
# Get the root node of the next document. |
| 67 |
if not self.parser.check_event(StreamEndEvent): |
| 68 |
return self.compose_document() |
| 69 |
|
| 70 |
def get_single_node(self): |
| 71 |
# type: () -> Any |
| 72 |
# Drop the STREAM-START event. |
| 73 |
self.parser.get_event() |
| 74 |
|
| 75 |
# Compose a document if the stream is not empty. |
| 76 |
document = None # type: Any |
| 77 |
if not self.parser.check_event(StreamEndEvent): |
| 78 |
document = self.compose_document() |
| 79 |
|
| 80 |
# Ensure that the stream contains no more documents. |
| 81 |
if not self.parser.check_event(StreamEndEvent): |
| 82 |
event = self.parser.get_event() |
| 83 |
raise ComposerError( |
| 84 |
"expected a single document in the stream", |
| 85 |
document.start_mark, |
| 86 |
"but found another document", |
| 87 |
event.start_mark, |
| 88 |
) |
| 89 |
|
| 90 |
# Drop the STREAM-END event. |
| 91 |
self.parser.get_event() |
| 92 |
|
| 93 |
return document |
| 94 |
|
| 95 |
def compose_document(self): |
| 96 |
# type: (Any) -> Any |
| 97 |
# Drop the DOCUMENT-START event. |
| 98 |
self.parser.get_event() |
| 99 |
|
| 100 |
# Compose the root node. |
| 101 |
node = self.compose_node(None, None) |
| 102 |
|
| 103 |
# Drop the DOCUMENT-END event. |
| 104 |
self.parser.get_event() |
| 105 |
|
| 106 |
self.anchors = {} |
| 107 |
return node |
| 108 |
|
| 109 |
def compose_node(self, parent, index): |
| 110 |
# type: (Any, Any) -> Any |
| 111 |
if self.parser.check_event(AliasEvent): |
| 112 |
event = self.parser.get_event() |
| 113 |
alias = event.anchor |
| 114 |
if alias not in self.anchors: |
| 115 |
raise ComposerError( |
| 116 |
None, |
| 117 |
None, |
| 118 |
"found undefined alias %r" % utf8(alias), |
| 119 |
event.start_mark, |
| 120 |
) |
| 121 |
return self.anchors[alias] |
| 122 |
event = self.parser.peek_event() |
| 123 |
anchor = event.anchor |
| 124 |
if anchor is not None: # have an anchor |
| 125 |
if anchor in self.anchors: |
| 126 |
# raise ComposerError( |
| 127 |
# "found duplicate anchor %r; first occurrence" |
| 128 |
# % utf8(anchor), self.anchors[anchor].start_mark, |
| 129 |
# "second occurrence", event.start_mark) |
| 130 |
ws = ( |
| 131 |
"\nfound duplicate anchor {!r}\nfirst occurrence {}\nsecond occurrence " |
| 132 |
"{}".format( |
| 133 |
(anchor), self.anchors[anchor].start_mark, event.start_mark |
| 134 |
) |
| 135 |
) |
| 136 |
warnings.warn(ws, ReusedAnchorWarning) |
| 137 |
self.resolver.descend_resolver(parent, index) |
| 138 |
if self.parser.check_event(ScalarEvent): |
| 139 |
node = self.compose_scalar_node(anchor) |
| 140 |
elif self.parser.check_event(SequenceStartEvent): |
| 141 |
node = self.compose_sequence_node(anchor) |
| 142 |
elif self.parser.check_event(MappingStartEvent): |
| 143 |
node = self.compose_mapping_node(anchor) |
| 144 |
self.resolver.ascend_resolver() |
| 145 |
return node |
| 146 |
|
| 147 |
def compose_scalar_node(self, anchor): |
| 148 |
# type: (Any) -> Any |
| 149 |
event = self.parser.get_event() |
| 150 |
tag = event.tag |
| 151 |
if tag is None or tag == u"!": |
| 152 |
tag = self.resolver.resolve(ScalarNode, event.value, event.implicit) |
| 153 |
node = ScalarNode( |
| 154 |
tag, |
| 155 |
event.value, |
| 156 |
event.start_mark, |
| 157 |
event.end_mark, |
| 158 |
style=event.style, |
| 159 |
comment=event.comment, |
| 160 |
anchor=anchor, |
| 161 |
) |
| 162 |
if anchor is not None: |
| 163 |
self.anchors[anchor] = node |
| 164 |
return node |
| 165 |
|
| 166 |
def compose_sequence_node(self, anchor): |
| 167 |
# type: (Any) -> Any |
| 168 |
start_event = self.parser.get_event() |
| 169 |
tag = start_event.tag |
| 170 |
if tag is None or tag == u"!": |
| 171 |
tag = self.resolver.resolve(SequenceNode, None, start_event.implicit) |
| 172 |
node = SequenceNode( |
| 173 |
tag, |
| 174 |
[], |
| 175 |
start_event.start_mark, |
| 176 |
None, |
| 177 |
flow_style=start_event.flow_style, |
| 178 |
comment=start_event.comment, |
| 179 |
anchor=anchor, |
| 180 |
) |
| 181 |
if anchor is not None: |
| 182 |
self.anchors[anchor] = node |
| 183 |
index = 0 |
| 184 |
while not self.parser.check_event(SequenceEndEvent): |
| 185 |
node.value.append(self.compose_node(node, index)) |
| 186 |
index += 1 |
| 187 |
end_event = self.parser.get_event() |
| 188 |
if node.flow_style is True and end_event.comment is not None: |
| 189 |
if node.comment is not None: |
| 190 |
nprint( |
| 191 |
"Warning: unexpected end_event commment in sequence " |
| 192 |
"node {}".format(node.flow_style) |
| 193 |
) |
| 194 |
node.comment = end_event.comment |
| 195 |
node.end_mark = end_event.end_mark |
| 196 |
self.check_end_doc_comment(end_event, node) |
| 197 |
return node |
| 198 |
|
| 199 |
def compose_mapping_node(self, anchor): |
| 200 |
# type: (Any) -> Any |
| 201 |
start_event = self.parser.get_event() |
| 202 |
tag = start_event.tag |
| 203 |
if tag is None or tag == u"!": |
| 204 |
tag = self.resolver.resolve(MappingNode, None, start_event.implicit) |
| 205 |
node = MappingNode( |
| 206 |
tag, |
| 207 |
[], |
| 208 |
start_event.start_mark, |
| 209 |
None, |
| 210 |
flow_style=start_event.flow_style, |
| 211 |
comment=start_event.comment, |
| 212 |
anchor=anchor, |
| 213 |
) |
| 214 |
if anchor is not None: |
| 215 |
self.anchors[anchor] = node |
| 216 |
while not self.parser.check_event(MappingEndEvent): |
| 217 |
# key_event = self.parser.peek_event() |
| 218 |
item_key = self.compose_node(node, None) |
| 219 |
# if item_key in node.value: |
| 220 |
# raise ComposerError("while composing a mapping", |
| 221 |
# start_event.start_mark, |
| 222 |
# "found duplicate key", key_event.start_mark) |
| 223 |
item_value = self.compose_node(node, item_key) |
| 224 |
# node.value[item_key] = item_value |
| 225 |
node.value.append((item_key, item_value)) |
| 226 |
end_event = self.parser.get_event() |
| 227 |
if node.flow_style is True and end_event.comment is not None: |
| 228 |
node.comment = end_event.comment |
| 229 |
node.end_mark = end_event.end_mark |
| 230 |
self.check_end_doc_comment(end_event, node) |
| 231 |
return node |
| 232 |
|
| 233 |
def check_end_doc_comment(self, end_event, node): |
| 234 |
# type: (Any, Any) -> None |
| 235 |
if end_event.comment and end_event.comment[1]: |
| 236 |
# pre comments on an end_event, no following to move to |
| 237 |
if node.comment is None: |
| 238 |
node.comment = [None, None] |
| 239 |
assert not isinstance(node, ScalarEvent) |
| 240 |
# this is a post comment on a mapping node, add as third element |
| 241 |
# in the list |
| 242 |
node.comment.append(end_event.comment[1]) |
| 243 |
end_event.comment[1] = None |