| 1 |
# coding: utf-8 |
| 2 |
|
| 3 |
from __future__ import absolute_import |
| 4 |
from __future__ import print_function |
| 5 |
|
| 6 |
# Emitter expects events obeying the following grammar: |
| 7 |
# stream ::= STREAM-START document* STREAM-END |
| 8 |
# document ::= DOCUMENT-START node DOCUMENT-END |
| 9 |
# node ::= SCALAR | sequence | mapping |
| 10 |
# sequence ::= SEQUENCE-START node* SEQUENCE-END |
| 11 |
# mapping ::= MAPPING-START (node node)* MAPPING-END |
| 12 |
|
| 13 |
import sys |
| 14 |
from strictyaml.ruamel.error import YAMLError, YAMLStreamError |
| 15 |
from strictyaml.ruamel.events import * # NOQA |
| 16 |
|
| 17 |
# fmt: off |
| 18 |
from strictyaml.ruamel.compat import utf8, text_type, PY2, nprint, dbg, DBG_EVENT, \ |
| 19 |
check_anchorname_char |
| 20 |
# fmt: on |
| 21 |
|
| 22 |
if False: # MYPY |
| 23 |
from typing import Any, Dict, List, Union, Text, Tuple, Optional # NOQA |
| 24 |
from strictyaml.ruamel.compat import StreamType # NOQA |
| 25 |
|
| 26 |
__all__ = ["Emitter", "EmitterError"] |
| 27 |
|
| 28 |
|
| 29 |
class EmitterError(YAMLError): |
| 30 |
pass |
| 31 |
|
| 32 |
|
| 33 |
class ScalarAnalysis(object): |
| 34 |
def __init__( |
| 35 |
self, |
| 36 |
scalar, |
| 37 |
empty, |
| 38 |
multiline, |
| 39 |
allow_flow_plain, |
| 40 |
allow_block_plain, |
| 41 |
allow_single_quoted, |
| 42 |
allow_double_quoted, |
| 43 |
allow_block, |
| 44 |
): |
| 45 |
# type: (Any, Any, Any, bool, bool, bool, bool, bool) -> None |
| 46 |
self.scalar = scalar |
| 47 |
self.empty = empty |
| 48 |
self.multiline = multiline |
| 49 |
self.allow_flow_plain = allow_flow_plain |
| 50 |
self.allow_block_plain = allow_block_plain |
| 51 |
self.allow_single_quoted = allow_single_quoted |
| 52 |
self.allow_double_quoted = allow_double_quoted |
| 53 |
self.allow_block = allow_block |
| 54 |
|
| 55 |
|
| 56 |
class Indents(object): |
| 57 |
# replacement for the list based stack of None/int |
| 58 |
def __init__(self): |
| 59 |
# type: () -> None |
| 60 |
self.values = [] # type: List[Tuple[int, bool]] |
| 61 |
|
| 62 |
def append(self, val, seq): |
| 63 |
# type: (Any, Any) -> None |
| 64 |
self.values.append((val, seq)) |
| 65 |
|
| 66 |
def pop(self): |
| 67 |
# type: () -> Any |
| 68 |
return self.values.pop()[0] |
| 69 |
|
| 70 |
def last_seq(self): |
| 71 |
# type: () -> bool |
| 72 |
# return the seq(uence) value for the element added before the last one |
| 73 |
# in increase_indent() |
| 74 |
try: |
| 75 |
return self.values[-2][1] |
| 76 |
except IndexError: |
| 77 |
return False |
| 78 |
|
| 79 |
def seq_flow_align(self, seq_indent, column): |
| 80 |
# type: (int, int) -> int |
| 81 |
# extra spaces because of dash |
| 82 |
if len(self.values) < 2 or not self.values[-1][1]: |
| 83 |
return 0 |
| 84 |
# -1 for the dash |
| 85 |
base = self.values[-1][0] if self.values[-1][0] is not None else 0 |
| 86 |
return base + seq_indent - column - 1 |
| 87 |
|
| 88 |
def __len__(self): |
| 89 |
# type: () -> int |
| 90 |
return len(self.values) |
| 91 |
|
| 92 |
|
| 93 |
class Emitter(object): |
| 94 |
# fmt: off |
| 95 |
DEFAULT_TAG_PREFIXES = { |
| 96 |
u'!': u'!', |
| 97 |
u'tag:yaml.org,2002:': u'!!', |
| 98 |
} |
| 99 |
# fmt: on |
| 100 |
|
| 101 |
MAX_SIMPLE_KEY_LENGTH = 128 |
| 102 |
|
| 103 |
def __init__( |
| 104 |
self, |
| 105 |
stream, |
| 106 |
canonical=None, |
| 107 |
indent=None, |
| 108 |
width=None, |
| 109 |
allow_unicode=None, |
| 110 |
line_break=None, |
| 111 |
block_seq_indent=None, |
| 112 |
top_level_colon_align=None, |
| 113 |
prefix_colon=None, |
| 114 |
brace_single_entry_mapping_in_flow_sequence=None, |
| 115 |
dumper=None, |
| 116 |
): |
| 117 |
# type: (StreamType, Any, Optional[int], Optional[int], Optional[bool], Any, Optional[int], Optional[bool], Any, Optional[bool], Any) -> None # NOQA |
| 118 |
self.dumper = dumper |
| 119 |
if self.dumper is not None and getattr(self.dumper, "_emitter", None) is None: |
| 120 |
self.dumper._emitter = self |
| 121 |
self.stream = stream |
| 122 |
|
| 123 |
# Encoding can be overriden by STREAM-START. |
| 124 |
self.encoding = None # type: Optional[Text] |
| 125 |
self.allow_space_break = None |
| 126 |
|
| 127 |
# Emitter is a state machine with a stack of states to handle nested |
| 128 |
# structures. |
| 129 |
self.states = [] # type: List[Any] |
| 130 |
self.state = self.expect_stream_start # type: Any |
| 131 |
|
| 132 |
# Current event and the event queue. |
| 133 |
self.events = [] # type: List[Any] |
| 134 |
self.event = None # type: Any |
| 135 |
|
| 136 |
# The current indentation level and the stack of previous indents. |
| 137 |
self.indents = Indents() |
| 138 |
self.indent = None # type: Optional[int] |
| 139 |
|
| 140 |
# flow_context is an expanding/shrinking list consisting of '{' and '[' |
| 141 |
# for each unclosed flow context. If empty list that means block context |
| 142 |
self.flow_context = [] # type: List[Text] |
| 143 |
|
| 144 |
# Contexts. |
| 145 |
self.root_context = False |
| 146 |
self.sequence_context = False |
| 147 |
self.mapping_context = False |
| 148 |
self.simple_key_context = False |
| 149 |
|
| 150 |
# Characteristics of the last emitted character: |
| 151 |
# - current position. |
| 152 |
# - is it a whitespace? |
| 153 |
# - is it an indention character |
| 154 |
# (indentation space, '-', '?', or ':')? |
| 155 |
self.line = 0 |
| 156 |
self.column = 0 |
| 157 |
self.whitespace = True |
| 158 |
self.indention = True |
| 159 |
self.compact_seq_seq = True # dash after dash |
| 160 |
self.compact_seq_map = True # key after dash |
| 161 |
# self.compact_ms = False # dash after key, only when excplicit key with ? |
| 162 |
self.no_newline = None # type: Optional[bool] # set if directly after `- ` |
| 163 |
|
| 164 |
# Whether the document requires an explicit document end indicator |
| 165 |
self.open_ended = False |
| 166 |
|
| 167 |
# colon handling |
| 168 |
self.colon = u":" |
| 169 |
self.prefixed_colon = ( |
| 170 |
self.colon if prefix_colon is None else prefix_colon + self.colon |
| 171 |
) |
| 172 |
# single entry mappings in flow sequence |
| 173 |
self.brace_single_entry_mapping_in_flow_sequence = ( |
| 174 |
brace_single_entry_mapping_in_flow_sequence # NOQA |
| 175 |
) |
| 176 |
|
| 177 |
# Formatting details. |
| 178 |
self.canonical = canonical |
| 179 |
self.allow_unicode = allow_unicode |
| 180 |
# set to False to get "\Uxxxxxxxx" for non-basic unicode like emojis |
| 181 |
self.unicode_supplementary = sys.maxunicode > 0xFFFF |
| 182 |
self.sequence_dash_offset = block_seq_indent if block_seq_indent else 0 |
| 183 |
self.top_level_colon_align = top_level_colon_align |
| 184 |
self.best_sequence_indent = 2 |
| 185 |
self.requested_indent = indent # specific for literal zero indent |
| 186 |
if indent and 1 < indent < 10: |
| 187 |
self.best_sequence_indent = indent |
| 188 |
self.best_map_indent = self.best_sequence_indent |
| 189 |
# if self.best_sequence_indent < self.sequence_dash_offset + 1: |
| 190 |
# self.best_sequence_indent = self.sequence_dash_offset + 1 |
| 191 |
self.best_width = 80 |
| 192 |
if width and width > self.best_sequence_indent * 2: |
| 193 |
self.best_width = width |
| 194 |
self.best_line_break = u"\n" # type: Any |
| 195 |
if line_break in [u"\r", u"\n", u"\r\n"]: |
| 196 |
self.best_line_break = line_break |
| 197 |
|
| 198 |
# Tag prefixes. |
| 199 |
self.tag_prefixes = None # type: Any |
| 200 |
|
| 201 |
# Prepared anchor and tag. |
| 202 |
self.prepared_anchor = None # type: Any |
| 203 |
self.prepared_tag = None # type: Any |
| 204 |
|
| 205 |
# Scalar analysis and style. |
| 206 |
self.analysis = None # type: Any |
| 207 |
self.style = None # type: Any |
| 208 |
|
| 209 |
self.scalar_after_indicator = True # write a scalar on the same line as `---` |
| 210 |
|
| 211 |
self.alt_null = "null" |
| 212 |
|
| 213 |
@property |
| 214 |
def stream(self): |
| 215 |
# type: () -> Any |
| 216 |
try: |
| 217 |
return self._stream |
| 218 |
except AttributeError: |
| 219 |
raise YAMLStreamError("output stream needs to specified") |
| 220 |
|
| 221 |
@stream.setter |
| 222 |
def stream(self, val): |
| 223 |
# type: (Any) -> None |
| 224 |
if val is None: |
| 225 |
return |
| 226 |
if not hasattr(val, "write"): |
| 227 |
raise YAMLStreamError("stream argument needs to have a write() method") |
| 228 |
self._stream = val |
| 229 |
|
| 230 |
@property |
| 231 |
def serializer(self): |
| 232 |
# type: () -> Any |
| 233 |
try: |
| 234 |
if hasattr(self.dumper, "typ"): |
| 235 |
return self.dumper.serializer |
| 236 |
return self.dumper._serializer |
| 237 |
except AttributeError: |
| 238 |
return self # cyaml |
| 239 |
|
| 240 |
@property |
| 241 |
def flow_level(self): |
| 242 |
# type: () -> int |
| 243 |
return len(self.flow_context) |
| 244 |
|
| 245 |
def dispose(self): |
| 246 |
# type: () -> None |
| 247 |
# Reset the state attributes (to clear self-references) |
| 248 |
self.states = [] |
| 249 |
self.state = None |
| 250 |
|
| 251 |
def emit(self, event): |
| 252 |
# type: (Any) -> None |
| 253 |
if dbg(DBG_EVENT): |
| 254 |
nprint(event) |
| 255 |
self.events.append(event) |
| 256 |
while not self.need_more_events(): |
| 257 |
self.event = self.events.pop(0) |
| 258 |
self.state() |
| 259 |
self.event = None |
| 260 |
|
| 261 |
# In some cases, we wait for a few next events before emitting. |
| 262 |
|
| 263 |
def need_more_events(self): |
| 264 |
# type: () -> bool |
| 265 |
if not self.events: |
| 266 |
return True |
| 267 |
event = self.events[0] |
| 268 |
if isinstance(event, DocumentStartEvent): |
| 269 |
return self.need_events(1) |
| 270 |
elif isinstance(event, SequenceStartEvent): |
| 271 |
return self.need_events(2) |
| 272 |
elif isinstance(event, MappingStartEvent): |
| 273 |
return self.need_events(3) |
| 274 |
else: |
| 275 |
return False |
| 276 |
|
| 277 |
def need_events(self, count): |
| 278 |
# type: (int) -> bool |
| 279 |
level = 0 |
| 280 |
for event in self.events[1:]: |
| 281 |
if isinstance(event, (DocumentStartEvent, CollectionStartEvent)): |
| 282 |
level += 1 |
| 283 |
elif isinstance(event, (DocumentEndEvent, CollectionEndEvent)): |
| 284 |
level -= 1 |
| 285 |
elif isinstance(event, StreamEndEvent): |
| 286 |
level = -1 |
| 287 |
if level < 0: |
| 288 |
return False |
| 289 |
return len(self.events) < count + 1 |
| 290 |
|
| 291 |
def increase_indent(self, flow=False, sequence=None, indentless=False): |
| 292 |
# type: (bool, Optional[bool], bool) -> None |
| 293 |
self.indents.append(self.indent, sequence) |
| 294 |
if self.indent is None: # top level |
| 295 |
if flow: |
| 296 |
# self.indent = self.best_sequence_indent if self.indents.last_seq() else \ |
| 297 |
# self.best_map_indent |
| 298 |
# self.indent = self.best_sequence_indent |
| 299 |
self.indent = self.requested_indent |
| 300 |
else: |
| 301 |
self.indent = 0 |
| 302 |
elif not indentless: |
| 303 |
self.indent += ( |
| 304 |
self.best_sequence_indent |
| 305 |
if self.indents.last_seq() |
| 306 |
else self.best_map_indent |
| 307 |
) |
| 308 |
# if self.indents.last_seq(): |
| 309 |
# if self.indent == 0: # top level block sequence |
| 310 |
# self.indent = self.best_sequence_indent - self.sequence_dash_offset |
| 311 |
# else: |
| 312 |
# self.indent += self.best_sequence_indent |
| 313 |
# else: |
| 314 |
# self.indent += self.best_map_indent |
| 315 |
|
| 316 |
# States. |
| 317 |
|
| 318 |
# Stream handlers. |
| 319 |
|
| 320 |
def expect_stream_start(self): |
| 321 |
# type: () -> None |
| 322 |
if isinstance(self.event, StreamStartEvent): |
| 323 |
if PY2: |
| 324 |
if self.event.encoding and not getattr(self.stream, "encoding", None): |
| 325 |
self.encoding = self.event.encoding |
| 326 |
else: |
| 327 |
if self.event.encoding and not hasattr(self.stream, "encoding"): |
| 328 |
self.encoding = self.event.encoding |
| 329 |
self.write_stream_start() |
| 330 |
self.state = self.expect_first_document_start |
| 331 |
else: |
| 332 |
raise EmitterError("expected StreamStartEvent, but got %s" % (self.event,)) |
| 333 |
|
| 334 |
def expect_nothing(self): |
| 335 |
# type: () -> None |
| 336 |
raise EmitterError("expected nothing, but got %s" % (self.event,)) |
| 337 |
|
| 338 |
# Document handlers. |
| 339 |
|
| 340 |
def expect_first_document_start(self): |
| 341 |
# type: () -> Any |
| 342 |
return self.expect_document_start(first=True) |
| 343 |
|
| 344 |
def expect_document_start(self, first=False): |
| 345 |
# type: (bool) -> None |
| 346 |
if isinstance(self.event, DocumentStartEvent): |
| 347 |
if (self.event.version or self.event.tags) and self.open_ended: |
| 348 |
self.write_indicator(u"...", True) |
| 349 |
self.write_indent() |
| 350 |
if self.event.version: |
| 351 |
version_text = self.prepare_version(self.event.version) |
| 352 |
self.write_version_directive(version_text) |
| 353 |
self.tag_prefixes = self.DEFAULT_TAG_PREFIXES.copy() |
| 354 |
if self.event.tags: |
| 355 |
handles = sorted(self.event.tags.keys()) |
| 356 |
for handle in handles: |
| 357 |
prefix = self.event.tags[handle] |
| 358 |
self.tag_prefixes[prefix] = handle |
| 359 |
handle_text = self.prepare_tag_handle(handle) |
| 360 |
prefix_text = self.prepare_tag_prefix(prefix) |
| 361 |
self.write_tag_directive(handle_text, prefix_text) |
| 362 |
implicit = ( |
| 363 |
first |
| 364 |
and not self.event.explicit |
| 365 |
and not self.canonical |
| 366 |
and not self.event.version |
| 367 |
and not self.event.tags |
| 368 |
and not self.check_empty_document() |
| 369 |
) |
| 370 |
if not implicit: |
| 371 |
self.write_indent() |
| 372 |
self.write_indicator(u"---", True) |
| 373 |
if self.canonical: |
| 374 |
self.write_indent() |
| 375 |
self.state = self.expect_document_root |
| 376 |
elif isinstance(self.event, StreamEndEvent): |
| 377 |
if self.open_ended: |
| 378 |
self.write_indicator(u"...", True) |
| 379 |
self.write_indent() |
| 380 |
self.write_stream_end() |
| 381 |
self.state = self.expect_nothing |
| 382 |
else: |
| 383 |
raise EmitterError( |
| 384 |
"expected DocumentStartEvent, but got %s" % (self.event,) |
| 385 |
) |
| 386 |
|
| 387 |
def expect_document_end(self): |
| 388 |
# type: () -> None |
| 389 |
if isinstance(self.event, DocumentEndEvent): |
| 390 |
self.write_indent() |
| 391 |
if self.event.explicit: |
| 392 |
self.write_indicator(u"...", True) |
| 393 |
self.write_indent() |
| 394 |
self.flush_stream() |
| 395 |
self.state = self.expect_document_start |
| 396 |
else: |
| 397 |
raise EmitterError("expected DocumentEndEvent, but got %s" % (self.event,)) |
| 398 |
|
| 399 |
def expect_document_root(self): |
| 400 |
# type: () -> None |
| 401 |
self.states.append(self.expect_document_end) |
| 402 |
self.expect_node(root=True) |
| 403 |
|
| 404 |
# Node handlers. |
| 405 |
|
| 406 |
def expect_node(self, root=False, sequence=False, mapping=False, simple_key=False): |
| 407 |
# type: (bool, bool, bool, bool) -> None |
| 408 |
self.root_context = root |
| 409 |
self.sequence_context = sequence # not used in PyYAML |
| 410 |
self.mapping_context = mapping |
| 411 |
self.simple_key_context = simple_key |
| 412 |
if isinstance(self.event, AliasEvent): |
| 413 |
self.expect_alias() |
| 414 |
elif isinstance(self.event, (ScalarEvent, CollectionStartEvent)): |
| 415 |
if ( |
| 416 |
self.process_anchor(u"&") |
| 417 |
and isinstance(self.event, ScalarEvent) |
| 418 |
and self.sequence_context |
| 419 |
): |
| 420 |
self.sequence_context = False |
| 421 |
if ( |
| 422 |
root |
| 423 |
and isinstance(self.event, ScalarEvent) |
| 424 |
and not self.scalar_after_indicator |
| 425 |
): |
| 426 |
self.write_indent() |
| 427 |
self.process_tag() |
| 428 |
if isinstance(self.event, ScalarEvent): |
| 429 |
# nprint('@', self.indention, self.no_newline, self.column) |
| 430 |
self.expect_scalar() |
| 431 |
elif isinstance(self.event, SequenceStartEvent): |
| 432 |
# nprint('@', self.indention, self.no_newline, self.column) |
| 433 |
i2, n2 = self.indention, self.no_newline # NOQA |
| 434 |
if self.event.comment: |
| 435 |
if self.event.flow_style is False and self.event.comment: |
| 436 |
if self.write_post_comment(self.event): |
| 437 |
self.indention = False |
| 438 |
self.no_newline = True |
| 439 |
if self.write_pre_comment(self.event): |
| 440 |
self.indention = i2 |
| 441 |
self.no_newline = not self.indention |
| 442 |
if ( |
| 443 |
self.flow_level |
| 444 |
or self.canonical |
| 445 |
or self.event.flow_style |
| 446 |
or self.check_empty_sequence() |
| 447 |
): |
| 448 |
self.expect_flow_sequence() |
| 449 |
else: |
| 450 |
self.expect_block_sequence() |
| 451 |
elif isinstance(self.event, MappingStartEvent): |
| 452 |
if self.event.flow_style is False and self.event.comment: |
| 453 |
self.write_post_comment(self.event) |
| 454 |
if self.event.comment and self.event.comment[1]: |
| 455 |
self.write_pre_comment(self.event) |
| 456 |
if ( |
| 457 |
self.flow_level |
| 458 |
or self.canonical |
| 459 |
or self.event.flow_style |
| 460 |
or self.check_empty_mapping() |
| 461 |
): |
| 462 |
self.expect_flow_mapping(single=self.event.nr_items == 1) |
| 463 |
else: |
| 464 |
self.expect_block_mapping() |
| 465 |
else: |
| 466 |
raise EmitterError("expected NodeEvent, but got %s" % (self.event,)) |
| 467 |
|
| 468 |
def expect_alias(self): |
| 469 |
# type: () -> None |
| 470 |
if self.event.anchor is None: |
| 471 |
raise EmitterError("anchor is not specified for alias") |
| 472 |
self.process_anchor(u"*") |
| 473 |
self.state = self.states.pop() |
| 474 |
|
| 475 |
def expect_scalar(self): |
| 476 |
# type: () -> None |
| 477 |
self.increase_indent(flow=True) |
| 478 |
self.process_scalar() |
| 479 |
self.indent = self.indents.pop() |
| 480 |
self.state = self.states.pop() |
| 481 |
|
| 482 |
# Flow sequence handlers. |
| 483 |
|
| 484 |
def expect_flow_sequence(self): |
| 485 |
# type: () -> None |
| 486 |
ind = self.indents.seq_flow_align(self.best_sequence_indent, self.column) |
| 487 |
self.write_indicator(u" " * ind + u"[", True, whitespace=True) |
| 488 |
self.increase_indent(flow=True, sequence=True) |
| 489 |
self.flow_context.append("[") |
| 490 |
self.state = self.expect_first_flow_sequence_item |
| 491 |
|
| 492 |
def expect_first_flow_sequence_item(self): |
| 493 |
# type: () -> None |
| 494 |
if isinstance(self.event, SequenceEndEvent): |
| 495 |
self.indent = self.indents.pop() |
| 496 |
popped = self.flow_context.pop() |
| 497 |
assert popped == "[" |
| 498 |
self.write_indicator(u"]", False) |
| 499 |
if self.event.comment and self.event.comment[0]: |
| 500 |
# eol comment on empty flow sequence |
| 501 |
self.write_post_comment(self.event) |
| 502 |
elif self.flow_level == 0: |
| 503 |
self.write_line_break() |
| 504 |
self.state = self.states.pop() |
| 505 |
else: |
| 506 |
if self.canonical or self.column > self.best_width: |
| 507 |
self.write_indent() |
| 508 |
self.states.append(self.expect_flow_sequence_item) |
| 509 |
self.expect_node(sequence=True) |
| 510 |
|
| 511 |
def expect_flow_sequence_item(self): |
| 512 |
# type: () -> None |
| 513 |
if isinstance(self.event, SequenceEndEvent): |
| 514 |
self.indent = self.indents.pop() |
| 515 |
popped = self.flow_context.pop() |
| 516 |
assert popped == "[" |
| 517 |
if self.canonical: |
| 518 |
self.write_indicator(u",", False) |
| 519 |
self.write_indent() |
| 520 |
self.write_indicator(u"]", False) |
| 521 |
if self.event.comment and self.event.comment[0]: |
| 522 |
# eol comment on flow sequence |
| 523 |
self.write_post_comment(self.event) |
| 524 |
else: |
| 525 |
self.no_newline = False |
| 526 |
self.state = self.states.pop() |
| 527 |
else: |
| 528 |
self.write_indicator(u",", False) |
| 529 |
if self.canonical or self.column > self.best_width: |
| 530 |
self.write_indent() |
| 531 |
self.states.append(self.expect_flow_sequence_item) |
| 532 |
self.expect_node(sequence=True) |
| 533 |
|
| 534 |
# Flow mapping handlers. |
| 535 |
|
| 536 |
def expect_flow_mapping(self, single=False): |
| 537 |
# type: (Optional[bool]) -> None |
| 538 |
ind = self.indents.seq_flow_align(self.best_sequence_indent, self.column) |
| 539 |
map_init = u"{" |
| 540 |
if ( |
| 541 |
single |
| 542 |
and self.flow_level |
| 543 |
and self.flow_context[-1] == "[" |
| 544 |
and not self.canonical |
| 545 |
and not self.brace_single_entry_mapping_in_flow_sequence |
| 546 |
): |
| 547 |
# single map item with flow context, no curly braces necessary |
| 548 |
map_init = u"" |
| 549 |
self.write_indicator(u" " * ind + map_init, True, whitespace=True) |
| 550 |
self.flow_context.append(map_init) |
| 551 |
self.increase_indent(flow=True, sequence=False) |
| 552 |
self.state = self.expect_first_flow_mapping_key |
| 553 |
|
| 554 |
def expect_first_flow_mapping_key(self): |
| 555 |
# type: () -> None |
| 556 |
if isinstance(self.event, MappingEndEvent): |
| 557 |
self.indent = self.indents.pop() |
| 558 |
popped = self.flow_context.pop() |
| 559 |
assert popped == "{" # empty flow mapping |
| 560 |
self.write_indicator(u"}", False) |
| 561 |
if self.event.comment and self.event.comment[0]: |
| 562 |
# eol comment on empty mapping |
| 563 |
self.write_post_comment(self.event) |
| 564 |
elif self.flow_level == 0: |
| 565 |
self.write_line_break() |
| 566 |
self.state = self.states.pop() |
| 567 |
else: |
| 568 |
if self.canonical or self.column > self.best_width: |
| 569 |
self.write_indent() |
| 570 |
if not self.canonical and self.check_simple_key(): |
| 571 |
self.states.append(self.expect_flow_mapping_simple_value) |
| 572 |
self.expect_node(mapping=True, simple_key=True) |
| 573 |
else: |
| 574 |
self.write_indicator(u"?", True) |
| 575 |
self.states.append(self.expect_flow_mapping_value) |
| 576 |
self.expect_node(mapping=True) |
| 577 |
|
| 578 |
def expect_flow_mapping_key(self): |
| 579 |
# type: () -> None |
| 580 |
if isinstance(self.event, MappingEndEvent): |
| 581 |
# if self.event.comment and self.event.comment[1]: |
| 582 |
# self.write_pre_comment(self.event) |
| 583 |
self.indent = self.indents.pop() |
| 584 |
popped = self.flow_context.pop() |
| 585 |
assert popped in [u"{", u""] |
| 586 |
if self.canonical: |
| 587 |
self.write_indicator(u",", False) |
| 588 |
self.write_indent() |
| 589 |
if popped != u"": |
| 590 |
self.write_indicator(u"}", False) |
| 591 |
if self.event.comment and self.event.comment[0]: |
| 592 |
# eol comment on flow mapping, never reached on empty mappings |
| 593 |
self.write_post_comment(self.event) |
| 594 |
else: |
| 595 |
self.no_newline = False |
| 596 |
self.state = self.states.pop() |
| 597 |
else: |
| 598 |
self.write_indicator(u",", False) |
| 599 |
if self.canonical or self.column > self.best_width: |
| 600 |
self.write_indent() |
| 601 |
if not self.canonical and self.check_simple_key(): |
| 602 |
self.states.append(self.expect_flow_mapping_simple_value) |
| 603 |
self.expect_node(mapping=True, simple_key=True) |
| 604 |
else: |
| 605 |
self.write_indicator(u"?", True) |
| 606 |
self.states.append(self.expect_flow_mapping_value) |
| 607 |
self.expect_node(mapping=True) |
| 608 |
|
| 609 |
def expect_flow_mapping_simple_value(self): |
| 610 |
# type: () -> None |
| 611 |
self.write_indicator(self.prefixed_colon, False) |
| 612 |
self.states.append(self.expect_flow_mapping_key) |
| 613 |
self.expect_node(mapping=True) |
| 614 |
|
| 615 |
def expect_flow_mapping_value(self): |
| 616 |
# type: () -> None |
| 617 |
if self.canonical or self.column > self.best_width: |
| 618 |
self.write_indent() |
| 619 |
self.write_indicator(self.prefixed_colon, True) |
| 620 |
self.states.append(self.expect_flow_mapping_key) |
| 621 |
self.expect_node(mapping=True) |
| 622 |
|
| 623 |
# Block sequence handlers. |
| 624 |
|
| 625 |
def expect_block_sequence(self): |
| 626 |
# type: () -> None |
| 627 |
if self.mapping_context: |
| 628 |
indentless = not self.indention |
| 629 |
else: |
| 630 |
indentless = False |
| 631 |
if not self.compact_seq_seq and self.column != 0: |
| 632 |
self.write_line_break() |
| 633 |
self.increase_indent(flow=False, sequence=True, indentless=indentless) |
| 634 |
self.state = self.expect_first_block_sequence_item |
| 635 |
|
| 636 |
def expect_first_block_sequence_item(self): |
| 637 |
# type: () -> Any |
| 638 |
return self.expect_block_sequence_item(first=True) |
| 639 |
|
| 640 |
def expect_block_sequence_item(self, first=False): |
| 641 |
# type: (bool) -> None |
| 642 |
if not first and isinstance(self.event, SequenceEndEvent): |
| 643 |
if self.event.comment and self.event.comment[1]: |
| 644 |
# final comments on a block list e.g. empty line |
| 645 |
self.write_pre_comment(self.event) |
| 646 |
self.indent = self.indents.pop() |
| 647 |
self.state = self.states.pop() |
| 648 |
self.no_newline = False |
| 649 |
else: |
| 650 |
if self.event.comment and self.event.comment[1]: |
| 651 |
self.write_pre_comment(self.event) |
| 652 |
nonl = self.no_newline if self.column == 0 else False |
| 653 |
self.write_indent() |
| 654 |
ind = self.sequence_dash_offset # if len(self.indents) > 1 else 0 |
| 655 |
self.write_indicator(u" " * ind + u"-", True, indention=True) |
| 656 |
if nonl or self.sequence_dash_offset + 2 > self.best_sequence_indent: |
| 657 |
self.no_newline = True |
| 658 |
self.states.append(self.expect_block_sequence_item) |
| 659 |
self.expect_node(sequence=True) |
| 660 |
|
| 661 |
# Block mapping handlers. |
| 662 |
|
| 663 |
def expect_block_mapping(self): |
| 664 |
# type: () -> None |
| 665 |
if not self.mapping_context and not (self.compact_seq_map or self.column == 0): |
| 666 |
self.write_line_break() |
| 667 |
self.increase_indent(flow=False, sequence=False) |
| 668 |
self.state = self.expect_first_block_mapping_key |
| 669 |
|
| 670 |
def expect_first_block_mapping_key(self): |
| 671 |
# type: () -> None |
| 672 |
return self.expect_block_mapping_key(first=True) |
| 673 |
|
| 674 |
def expect_block_mapping_key(self, first=False): |
| 675 |
# type: (Any) -> None |
| 676 |
if not first and isinstance(self.event, MappingEndEvent): |
| 677 |
if self.event.comment and self.event.comment[1]: |
| 678 |
# final comments from a doc |
| 679 |
self.write_pre_comment(self.event) |
| 680 |
self.indent = self.indents.pop() |
| 681 |
self.state = self.states.pop() |
| 682 |
else: |
| 683 |
if self.event.comment and self.event.comment[1]: |
| 684 |
# final comments from a doc |
| 685 |
self.write_pre_comment(self.event) |
| 686 |
self.write_indent() |
| 687 |
if self.check_simple_key(): |
| 688 |
if not isinstance( |
| 689 |
self.event, (SequenceStartEvent, MappingStartEvent) |
| 690 |
): # sequence keys |
| 691 |
try: |
| 692 |
if self.event.style == "?": |
| 693 |
self.write_indicator(u"?", True, indention=True) |
| 694 |
except AttributeError: # aliases have no style |
| 695 |
pass |
| 696 |
self.states.append(self.expect_block_mapping_simple_value) |
| 697 |
self.expect_node(mapping=True, simple_key=True) |
| 698 |
if isinstance(self.event, AliasEvent): |
| 699 |
self.stream.write(u" ") |
| 700 |
else: |
| 701 |
self.write_indicator(u"?", True, indention=True) |
| 702 |
self.states.append(self.expect_block_mapping_value) |
| 703 |
self.expect_node(mapping=True) |
| 704 |
|
| 705 |
def expect_block_mapping_simple_value(self): |
| 706 |
# type: () -> None |
| 707 |
if getattr(self.event, "style", None) != "?": |
| 708 |
# prefix = u'' |
| 709 |
if self.indent == 0 and self.top_level_colon_align is not None: |
| 710 |
# write non-prefixed colon |
| 711 |
c = u" " * (self.top_level_colon_align - self.column) + self.colon |
| 712 |
else: |
| 713 |
c = self.prefixed_colon |
| 714 |
self.write_indicator(c, False) |
| 715 |
self.states.append(self.expect_block_mapping_key) |
| 716 |
self.expect_node(mapping=True) |
| 717 |
|
| 718 |
def expect_block_mapping_value(self): |
| 719 |
# type: () -> None |
| 720 |
self.write_indent() |
| 721 |
self.write_indicator(self.prefixed_colon, True, indention=True) |
| 722 |
self.states.append(self.expect_block_mapping_key) |
| 723 |
self.expect_node(mapping=True) |
| 724 |
|
| 725 |
# Checkers. |
| 726 |
|
| 727 |
def check_empty_sequence(self): |
| 728 |
# type: () -> bool |
| 729 |
return ( |
| 730 |
isinstance(self.event, SequenceStartEvent) |
| 731 |
and bool(self.events) |
| 732 |
and isinstance(self.events[0], SequenceEndEvent) |
| 733 |
) |
| 734 |
|
| 735 |
def check_empty_mapping(self): |
| 736 |
# type: () -> bool |
| 737 |
return ( |
| 738 |
isinstance(self.event, MappingStartEvent) |
| 739 |
and bool(self.events) |
| 740 |
and isinstance(self.events[0], MappingEndEvent) |
| 741 |
) |
| 742 |
|
| 743 |
def check_empty_document(self): |
| 744 |
# type: () -> bool |
| 745 |
if not isinstance(self.event, DocumentStartEvent) or not self.events: |
| 746 |
return False |
| 747 |
event = self.events[0] |
| 748 |
return ( |
| 749 |
isinstance(event, ScalarEvent) |
| 750 |
and event.anchor is None |
| 751 |
and event.tag is None |
| 752 |
and event.implicit |
| 753 |
and event.value == "" |
| 754 |
) |
| 755 |
|
| 756 |
def check_simple_key(self): |
| 757 |
# type: () -> bool |
| 758 |
length = 0 |
| 759 |
if isinstance(self.event, NodeEvent) and self.event.anchor is not None: |
| 760 |
if self.prepared_anchor is None: |
| 761 |
self.prepared_anchor = self.prepare_anchor(self.event.anchor) |
| 762 |
length += len(self.prepared_anchor) |
| 763 |
if ( |
| 764 |
isinstance(self.event, (ScalarEvent, CollectionStartEvent)) |
| 765 |
and self.event.tag is not None |
| 766 |
): |
| 767 |
if self.prepared_tag is None: |
| 768 |
self.prepared_tag = self.prepare_tag(self.event.tag) |
| 769 |
length += len(self.prepared_tag) |
| 770 |
if isinstance(self.event, ScalarEvent): |
| 771 |
if self.analysis is None: |
| 772 |
self.analysis = self.analyze_scalar(self.event.value) |
| 773 |
length += len(self.analysis.scalar) |
| 774 |
return length < self.MAX_SIMPLE_KEY_LENGTH and ( |
| 775 |
isinstance(self.event, AliasEvent) |
| 776 |
or ( |
| 777 |
isinstance(self.event, SequenceStartEvent) |
| 778 |
and self.event.flow_style is True |
| 779 |
) |
| 780 |
or ( |
| 781 |
isinstance(self.event, MappingStartEvent) |
| 782 |
and self.event.flow_style is True |
| 783 |
) |
| 784 |
or ( |
| 785 |
isinstance(self.event, ScalarEvent) |
| 786 |
# if there is an explicit style for an empty string, it is a simple key |
| 787 |
and not (self.analysis.empty and self.style and self.style not in "'\"") |
| 788 |
and not self.analysis.multiline |
| 789 |
) |
| 790 |
or self.check_empty_sequence() |
| 791 |
or self.check_empty_mapping() |
| 792 |
) |
| 793 |
|
| 794 |
# Anchor, Tag, and Scalar processors. |
| 795 |
|
| 796 |
def process_anchor(self, indicator): |
| 797 |
# type: (Any) -> bool |
| 798 |
if self.event.anchor is None: |
| 799 |
self.prepared_anchor = None |
| 800 |
return False |
| 801 |
if self.prepared_anchor is None: |
| 802 |
self.prepared_anchor = self.prepare_anchor(self.event.anchor) |
| 803 |
if self.prepared_anchor: |
| 804 |
self.write_indicator(indicator + self.prepared_anchor, True) |
| 805 |
# issue 288 |
| 806 |
self.no_newline = False |
| 807 |
self.prepared_anchor = None |
| 808 |
return True |
| 809 |
|
| 810 |
def process_tag(self): |
| 811 |
# type: () -> None |
| 812 |
tag = self.event.tag |
| 813 |
if isinstance(self.event, ScalarEvent): |
| 814 |
if self.style is None: |
| 815 |
self.style = self.choose_scalar_style() |
| 816 |
if ( |
| 817 |
self.event.value == "" |
| 818 |
and self.style == "'" |
| 819 |
and tag == "tag:yaml.org,2002:null" |
| 820 |
and self.alt_null is not None |
| 821 |
): |
| 822 |
self.event.value = self.alt_null |
| 823 |
self.analysis = None |
| 824 |
self.style = self.choose_scalar_style() |
| 825 |
if (not self.canonical or tag is None) and ( |
| 826 |
(self.style == "" and self.event.implicit[0]) |
| 827 |
or (self.style != "" and self.event.implicit[1]) |
| 828 |
): |
| 829 |
self.prepared_tag = None |
| 830 |
return |
| 831 |
if self.event.implicit[0] and tag is None: |
| 832 |
tag = u"!" |
| 833 |
self.prepared_tag = None |
| 834 |
else: |
| 835 |
if (not self.canonical or tag is None) and self.event.implicit: |
| 836 |
self.prepared_tag = None |
| 837 |
return |
| 838 |
if tag is None: |
| 839 |
raise EmitterError("tag is not specified") |
| 840 |
if self.prepared_tag is None: |
| 841 |
self.prepared_tag = self.prepare_tag(tag) |
| 842 |
if self.prepared_tag: |
| 843 |
self.write_indicator(self.prepared_tag, True) |
| 844 |
if ( |
| 845 |
self.sequence_context |
| 846 |
and not self.flow_level |
| 847 |
and isinstance(self.event, ScalarEvent) |
| 848 |
): |
| 849 |
self.no_newline = True |
| 850 |
self.prepared_tag = None |
| 851 |
|
| 852 |
def choose_scalar_style(self): |
| 853 |
# type: () -> Any |
| 854 |
if self.analysis is None: |
| 855 |
self.analysis = self.analyze_scalar(self.event.value) |
| 856 |
if self.event.style == '"' or self.canonical: |
| 857 |
return '"' |
| 858 |
if (not self.event.style or self.event.style == "?") and ( |
| 859 |
self.event.implicit[0] or not self.event.implicit[2] |
| 860 |
): |
| 861 |
if not ( |
| 862 |
self.simple_key_context |
| 863 |
and (self.analysis.empty or self.analysis.multiline) |
| 864 |
) and ( |
| 865 |
self.flow_level |
| 866 |
and self.analysis.allow_flow_plain |
| 867 |
or (not self.flow_level and self.analysis.allow_block_plain) |
| 868 |
): |
| 869 |
return "" |
| 870 |
self.analysis.allow_block = True |
| 871 |
if self.event.style and self.event.style in "|>": |
| 872 |
if ( |
| 873 |
not self.flow_level |
| 874 |
and not self.simple_key_context |
| 875 |
and self.analysis.allow_block |
| 876 |
): |
| 877 |
return self.event.style |
| 878 |
if not self.event.style and self.analysis.allow_double_quoted: |
| 879 |
if "'" in self.event.value or "\n" in self.event.value: |
| 880 |
return '"' |
| 881 |
if not self.event.style or self.event.style == "'": |
| 882 |
if self.analysis.allow_single_quoted and not ( |
| 883 |
self.simple_key_context and self.analysis.multiline |
| 884 |
): |
| 885 |
return "'" |
| 886 |
return '"' |
| 887 |
|
| 888 |
def process_scalar(self): |
| 889 |
# type: () -> None |
| 890 |
if self.analysis is None: |
| 891 |
self.analysis = self.analyze_scalar(self.event.value) |
| 892 |
if self.style is None: |
| 893 |
self.style = self.choose_scalar_style() |
| 894 |
split = not self.simple_key_context |
| 895 |
# if self.analysis.multiline and split \ |
| 896 |
# and (not self.style or self.style in '\'\"'): |
| 897 |
# self.write_indent() |
| 898 |
# nprint('xx', self.sequence_context, self.flow_level) |
| 899 |
if self.sequence_context and not self.flow_level: |
| 900 |
self.write_indent() |
| 901 |
if self.style == '"': |
| 902 |
self.write_double_quoted(self.analysis.scalar, split) |
| 903 |
elif self.style == "'": |
| 904 |
self.write_single_quoted(self.analysis.scalar, split) |
| 905 |
elif self.style == ">": |
| 906 |
self.write_folded(self.analysis.scalar) |
| 907 |
elif self.style == "|": |
| 908 |
self.write_literal(self.analysis.scalar, self.event.comment) |
| 909 |
else: |
| 910 |
self.write_plain(self.analysis.scalar, split) |
| 911 |
self.analysis = None |
| 912 |
self.style = None |
| 913 |
if self.event.comment: |
| 914 |
self.write_post_comment(self.event) |
| 915 |
|
| 916 |
# Analyzers. |
| 917 |
|
| 918 |
def prepare_version(self, version): |
| 919 |
# type: (Any) -> Any |
| 920 |
major, minor = version |
| 921 |
if major != 1: |
| 922 |
raise EmitterError("unsupported YAML version: %d.%d" % (major, minor)) |
| 923 |
return u"%d.%d" % (major, minor) |
| 924 |
|
| 925 |
def prepare_tag_handle(self, handle): |
| 926 |
# type: (Any) -> Any |
| 927 |
if not handle: |
| 928 |
raise EmitterError("tag handle must not be empty") |
| 929 |
if handle[0] != u"!" or handle[-1] != u"!": |
| 930 |
raise EmitterError( |
| 931 |
"tag handle must start and end with '!': %r" % (utf8(handle)) |
| 932 |
) |
| 933 |
for ch in handle[1:-1]: |
| 934 |
if not ( |
| 935 |
u"0" <= ch <= u"9" |
| 936 |
or u"A" <= ch <= u"Z" |
| 937 |
or u"a" <= ch <= u"z" |
| 938 |
or ch in u"-_" |
| 939 |
): |
| 940 |
raise EmitterError( |
| 941 |
"invalid character %r in the tag handle: %r" |
| 942 |
% (utf8(ch), utf8(handle)) |
| 943 |
) |
| 944 |
return handle |
| 945 |
|
| 946 |
def prepare_tag_prefix(self, prefix): |
| 947 |
# type: (Any) -> Any |
| 948 |
if not prefix: |
| 949 |
raise EmitterError("tag prefix must not be empty") |
| 950 |
chunks = [] # type: List[Any] |
| 951 |
start = end = 0 |
| 952 |
if prefix[0] == u"!": |
| 953 |
end = 1 |
| 954 |
ch_set = u"-;/?:@&=+$,_.~*'()[]" |
| 955 |
if self.dumper: |
| 956 |
version = getattr(self.dumper, "version", (1, 2)) |
| 957 |
if version is None or version >= (1, 2): |
| 958 |
ch_set += u"#" |
| 959 |
while end < len(prefix): |
| 960 |
ch = prefix[end] |
| 961 |
if ( |
| 962 |
u"0" <= ch <= u"9" |
| 963 |
or u"A" <= ch <= u"Z" |
| 964 |
or u"a" <= ch <= u"z" |
| 965 |
or ch in ch_set |
| 966 |
): |
| 967 |
end += 1 |
| 968 |
else: |
| 969 |
if start < end: |
| 970 |
chunks.append(prefix[start:end]) |
| 971 |
start = end = end + 1 |
| 972 |
data = utf8(ch) |
| 973 |
for ch in data: |
| 974 |
chunks.append(u"%%%02X" % ord(ch)) |
| 975 |
if start < end: |
| 976 |
chunks.append(prefix[start:end]) |
| 977 |
return "".join(chunks) |
| 978 |
|
| 979 |
def prepare_tag(self, tag): |
| 980 |
# type: (Any) -> Any |
| 981 |
if not tag: |
| 982 |
raise EmitterError("tag must not be empty") |
| 983 |
if tag == u"!": |
| 984 |
return tag |
| 985 |
handle = None |
| 986 |
suffix = tag |
| 987 |
prefixes = sorted(self.tag_prefixes.keys()) |
| 988 |
for prefix in prefixes: |
| 989 |
if tag.startswith(prefix) and (prefix == u"!" or len(prefix) < len(tag)): |
| 990 |
handle = self.tag_prefixes[prefix] |
| 991 |
suffix = tag[len(prefix) :] |
| 992 |
chunks = [] # type: List[Any] |
| 993 |
start = end = 0 |
| 994 |
ch_set = u"-;/?:@&=+$,_.~*'()[]" |
| 995 |
if self.dumper: |
| 996 |
version = getattr(self.dumper, "version", (1, 2)) |
| 997 |
if version is None or version >= (1, 2): |
| 998 |
ch_set += u"#" |
| 999 |
while end < len(suffix): |
| 1000 |
ch = suffix[end] |
| 1001 |
if ( |
| 1002 |
u"0" <= ch <= u"9" |
| 1003 |
or u"A" <= ch <= u"Z" |
| 1004 |
or u"a" <= ch <= u"z" |
| 1005 |
or ch in ch_set |
| 1006 |
or (ch == u"!" and handle != u"!") |
| 1007 |
): |
| 1008 |
end += 1 |
| 1009 |
else: |
| 1010 |
if start < end: |
| 1011 |
chunks.append(suffix[start:end]) |
| 1012 |
start = end = end + 1 |
| 1013 |
data = utf8(ch) |
| 1014 |
for ch in data: |
| 1015 |
chunks.append(u"%%%02X" % ord(ch)) |
| 1016 |
if start < end: |
| 1017 |
chunks.append(suffix[start:end]) |
| 1018 |
suffix_text = "".join(chunks) |
| 1019 |
if handle: |
| 1020 |
return u"%s%s" % (handle, suffix_text) |
| 1021 |
else: |
| 1022 |
return u"!<%s>" % suffix_text |
| 1023 |
|
| 1024 |
def prepare_anchor(self, anchor): |
| 1025 |
# type: (Any) -> Any |
| 1026 |
if not anchor: |
| 1027 |
raise EmitterError("anchor must not be empty") |
| 1028 |
for ch in anchor: |
| 1029 |
if not check_anchorname_char(ch): |
| 1030 |
raise EmitterError( |
| 1031 |
"invalid character %r in the anchor: %r" % (utf8(ch), utf8(anchor)) |
| 1032 |
) |
| 1033 |
return anchor |
| 1034 |
|
| 1035 |
def analyze_scalar(self, scalar): |
| 1036 |
# type: (Any) -> Any |
| 1037 |
# Empty scalar is a special case. |
| 1038 |
if not scalar: |
| 1039 |
return ScalarAnalysis( |
| 1040 |
scalar=scalar, |
| 1041 |
empty=True, |
| 1042 |
multiline=False, |
| 1043 |
allow_flow_plain=False, |
| 1044 |
allow_block_plain=True, |
| 1045 |
allow_single_quoted=True, |
| 1046 |
allow_double_quoted=True, |
| 1047 |
allow_block=False, |
| 1048 |
) |
| 1049 |
|
| 1050 |
# Indicators and special characters. |
| 1051 |
block_indicators = False |
| 1052 |
flow_indicators = False |
| 1053 |
line_breaks = False |
| 1054 |
special_characters = False |
| 1055 |
|
| 1056 |
# Important whitespace combinations. |
| 1057 |
leading_space = False |
| 1058 |
leading_break = False |
| 1059 |
trailing_space = False |
| 1060 |
trailing_break = False |
| 1061 |
break_space = False |
| 1062 |
space_break = False |
| 1063 |
|
| 1064 |
# Check document indicators. |
| 1065 |
if scalar.startswith(u"---") or scalar.startswith(u"..."): |
| 1066 |
block_indicators = True |
| 1067 |
flow_indicators = True |
| 1068 |
|
| 1069 |
# First character or preceded by a whitespace. |
| 1070 |
preceeded_by_whitespace = True |
| 1071 |
|
| 1072 |
# Last character or followed by a whitespace. |
| 1073 |
followed_by_whitespace = ( |
| 1074 |
len(scalar) == 1 or scalar[1] in u"\0 \t\r\n\x85\u2028\u2029" |
| 1075 |
) |
| 1076 |
|
| 1077 |
# The previous character is a space. |
| 1078 |
previous_space = False |
| 1079 |
|
| 1080 |
# The previous character is a break. |
| 1081 |
previous_break = False |
| 1082 |
|
| 1083 |
index = 0 |
| 1084 |
while index < len(scalar): |
| 1085 |
ch = scalar[index] |
| 1086 |
|
| 1087 |
# Check for indicators. |
| 1088 |
if index == 0: |
| 1089 |
# Leading indicators are special characters. |
| 1090 |
if ch in u"#,[]{}&*!|>'\"%@`": |
| 1091 |
flow_indicators = True |
| 1092 |
block_indicators = True |
| 1093 |
if ch in u"?:": # ToDo |
| 1094 |
if self.serializer.use_version == (1, 1): |
| 1095 |
flow_indicators = True |
| 1096 |
elif len(scalar) == 1: # single character |
| 1097 |
flow_indicators = True |
| 1098 |
if followed_by_whitespace: |
| 1099 |
block_indicators = True |
| 1100 |
if ch == u"-" and followed_by_whitespace: |
| 1101 |
flow_indicators = True |
| 1102 |
block_indicators = True |
| 1103 |
else: |
| 1104 |
# Some indicators cannot appear within a scalar as well. |
| 1105 |
if ch in u",[]{}": # http://yaml.org/spec/1.2/spec.html#id2788859 |
| 1106 |
flow_indicators = True |
| 1107 |
if ch == u"?" and self.serializer.use_version == (1, 1): |
| 1108 |
flow_indicators = True |
| 1109 |
if ch == u":": |
| 1110 |
if followed_by_whitespace: |
| 1111 |
flow_indicators = True |
| 1112 |
block_indicators = True |
| 1113 |
if ch == u"#" and preceeded_by_whitespace: |
| 1114 |
flow_indicators = True |
| 1115 |
block_indicators = True |
| 1116 |
|
| 1117 |
# Check for line breaks, special, and unicode characters. |
| 1118 |
if ch in u"\n\x85\u2028\u2029": |
| 1119 |
line_breaks = True |
| 1120 |
if not (ch == u"\n" or u"\x20" <= ch <= u"\x7E"): |
| 1121 |
if ( |
| 1122 |
ch == u"\x85" |
| 1123 |
or u"\xA0" <= ch <= u"\uD7FF" |
| 1124 |
or u"\uE000" <= ch <= u"\uFFFD" |
| 1125 |
or ( |
| 1126 |
self.unicode_supplementary |
| 1127 |
and (u"\U00010000" <= ch <= u"\U0010FFFF") |
| 1128 |
) |
| 1129 |
) and ch != u"\uFEFF": |
| 1130 |
# unicode_characters = True |
| 1131 |
if not self.allow_unicode: |
| 1132 |
special_characters = True |
| 1133 |
else: |
| 1134 |
special_characters = True |
| 1135 |
|
| 1136 |
# Detect important whitespace combinations. |
| 1137 |
if ch == u" ": |
| 1138 |
if index == 0: |
| 1139 |
leading_space = True |
| 1140 |
if index == len(scalar) - 1: |
| 1141 |
trailing_space = True |
| 1142 |
if previous_break: |
| 1143 |
break_space = True |
| 1144 |
previous_space = True |
| 1145 |
previous_break = False |
| 1146 |
elif ch in u"\n\x85\u2028\u2029": |
| 1147 |
if index == 0: |
| 1148 |
leading_break = True |
| 1149 |
if index == len(scalar) - 1: |
| 1150 |
trailing_break = True |
| 1151 |
if previous_space: |
| 1152 |
space_break = True |
| 1153 |
previous_space = False |
| 1154 |
previous_break = True |
| 1155 |
else: |
| 1156 |
previous_space = False |
| 1157 |
previous_break = False |
| 1158 |
|
| 1159 |
# Prepare for the next character. |
| 1160 |
index += 1 |
| 1161 |
preceeded_by_whitespace = ch in u"\0 \t\r\n\x85\u2028\u2029" |
| 1162 |
followed_by_whitespace = ( |
| 1163 |
index + 1 >= len(scalar) |
| 1164 |
or scalar[index + 1] in u"\0 \t\r\n\x85\u2028\u2029" |
| 1165 |
) |
| 1166 |
|
| 1167 |
# Let's decide what styles are allowed. |
| 1168 |
allow_flow_plain = True |
| 1169 |
allow_block_plain = True |
| 1170 |
allow_single_quoted = True |
| 1171 |
allow_double_quoted = True |
| 1172 |
allow_block = True |
| 1173 |
|
| 1174 |
# Leading and trailing whitespaces are bad for plain scalars. |
| 1175 |
if leading_space or leading_break or trailing_space or trailing_break: |
| 1176 |
allow_flow_plain = allow_block_plain = False |
| 1177 |
|
| 1178 |
# We do not permit trailing spaces for block scalars. |
| 1179 |
if trailing_space: |
| 1180 |
allow_block = False |
| 1181 |
|
| 1182 |
# Spaces at the beginning of a new line are only acceptable for block |
| 1183 |
# scalars. |
| 1184 |
if break_space: |
| 1185 |
allow_flow_plain = allow_block_plain = allow_single_quoted = False |
| 1186 |
|
| 1187 |
# Spaces followed by breaks, as well as special character are only |
| 1188 |
# allowed for double quoted scalars. |
| 1189 |
if special_characters: |
| 1190 |
allow_flow_plain = ( |
| 1191 |
allow_block_plain |
| 1192 |
) = allow_single_quoted = allow_block = False |
| 1193 |
elif space_break: |
| 1194 |
allow_flow_plain = allow_block_plain = allow_single_quoted = False |
| 1195 |
if not self.allow_space_break: |
| 1196 |
allow_block = False |
| 1197 |
|
| 1198 |
# Although the plain scalar writer supports breaks, we never emit |
| 1199 |
# multiline plain scalars. |
| 1200 |
if line_breaks: |
| 1201 |
allow_flow_plain = allow_block_plain = False |
| 1202 |
|
| 1203 |
# Flow indicators are forbidden for flow plain scalars. |
| 1204 |
if flow_indicators: |
| 1205 |
allow_flow_plain = False |
| 1206 |
|
| 1207 |
# Block indicators are forbidden for block plain scalars. |
| 1208 |
if block_indicators: |
| 1209 |
allow_block_plain = False |
| 1210 |
|
| 1211 |
return ScalarAnalysis( |
| 1212 |
scalar=scalar, |
| 1213 |
empty=False, |
| 1214 |
multiline=line_breaks, |
| 1215 |
allow_flow_plain=allow_flow_plain, |
| 1216 |
allow_block_plain=allow_block_plain, |
| 1217 |
allow_single_quoted=allow_single_quoted, |
| 1218 |
allow_double_quoted=allow_double_quoted, |
| 1219 |
allow_block=allow_block, |
| 1220 |
) |
| 1221 |
|
| 1222 |
# Writers. |
| 1223 |
|
| 1224 |
def flush_stream(self): |
| 1225 |
# type: () -> None |
| 1226 |
if hasattr(self.stream, "flush"): |
| 1227 |
self.stream.flush() |
| 1228 |
|
| 1229 |
def write_stream_start(self): |
| 1230 |
# type: () -> None |
| 1231 |
# Write BOM if needed. |
| 1232 |
if self.encoding and self.encoding.startswith("utf-16"): |
| 1233 |
self.stream.write(u"\uFEFF".encode(self.encoding)) |
| 1234 |
|
| 1235 |
def write_stream_end(self): |
| 1236 |
# type: () -> None |
| 1237 |
self.flush_stream() |
| 1238 |
|
| 1239 |
def write_indicator( |
| 1240 |
self, indicator, need_whitespace, whitespace=False, indention=False |
| 1241 |
): |
| 1242 |
# type: (Any, Any, bool, bool) -> None |
| 1243 |
if self.whitespace or not need_whitespace: |
| 1244 |
data = indicator |
| 1245 |
else: |
| 1246 |
data = u" " + indicator |
| 1247 |
self.whitespace = whitespace |
| 1248 |
self.indention = self.indention and indention |
| 1249 |
self.column += len(data) |
| 1250 |
self.open_ended = False |
| 1251 |
if bool(self.encoding): |
| 1252 |
data = data.encode(self.encoding) |
| 1253 |
self.stream.write(data) |
| 1254 |
|
| 1255 |
def write_indent(self): |
| 1256 |
# type: () -> None |
| 1257 |
indent = self.indent or 0 |
| 1258 |
if ( |
| 1259 |
not self.indention |
| 1260 |
or self.column > indent |
| 1261 |
or (self.column == indent and not self.whitespace) |
| 1262 |
): |
| 1263 |
if bool(self.no_newline): |
| 1264 |
self.no_newline = False |
| 1265 |
else: |
| 1266 |
self.write_line_break() |
| 1267 |
if self.column < indent: |
| 1268 |
self.whitespace = True |
| 1269 |
data = u" " * (indent - self.column) |
| 1270 |
self.column = indent |
| 1271 |
if self.encoding: |
| 1272 |
data = data.encode(self.encoding) |
| 1273 |
self.stream.write(data) |
| 1274 |
|
| 1275 |
def write_line_break(self, data=None): |
| 1276 |
# type: (Any) -> None |
| 1277 |
if data is None: |
| 1278 |
data = self.best_line_break |
| 1279 |
self.whitespace = True |
| 1280 |
self.indention = True |
| 1281 |
self.line += 1 |
| 1282 |
self.column = 0 |
| 1283 |
if bool(self.encoding): |
| 1284 |
data = data.encode(self.encoding) |
| 1285 |
self.stream.write(data) |
| 1286 |
|
| 1287 |
def write_version_directive(self, version_text): |
| 1288 |
# type: (Any) -> None |
| 1289 |
data = u"%%YAML %s" % version_text |
| 1290 |
if self.encoding: |
| 1291 |
data = data.encode(self.encoding) |
| 1292 |
self.stream.write(data) |
| 1293 |
self.write_line_break() |
| 1294 |
|
| 1295 |
def write_tag_directive(self, handle_text, prefix_text): |
| 1296 |
# type: (Any, Any) -> None |
| 1297 |
data = u"%%TAG %s %s" % (handle_text, prefix_text) |
| 1298 |
if self.encoding: |
| 1299 |
data = data.encode(self.encoding) |
| 1300 |
self.stream.write(data) |
| 1301 |
self.write_line_break() |
| 1302 |
|
| 1303 |
# Scalar streams. |
| 1304 |
|
| 1305 |
def write_single_quoted(self, text, split=True): |
| 1306 |
# type: (Any, Any) -> None |
| 1307 |
if self.root_context: |
| 1308 |
if self.requested_indent is not None: |
| 1309 |
self.write_line_break() |
| 1310 |
if self.requested_indent != 0: |
| 1311 |
self.write_indent() |
| 1312 |
self.write_indicator(u"'", True) |
| 1313 |
spaces = False |
| 1314 |
breaks = False |
| 1315 |
start = end = 0 |
| 1316 |
while end <= len(text): |
| 1317 |
ch = None |
| 1318 |
if end < len(text): |
| 1319 |
ch = text[end] |
| 1320 |
if spaces: |
| 1321 |
if ch is None or ch != u" ": |
| 1322 |
if ( |
| 1323 |
start + 1 == end |
| 1324 |
and self.column > self.best_width |
| 1325 |
and split |
| 1326 |
and start != 0 |
| 1327 |
and end != len(text) |
| 1328 |
): |
| 1329 |
self.write_indent() |
| 1330 |
else: |
| 1331 |
data = text[start:end] |
| 1332 |
self.column += len(data) |
| 1333 |
if bool(self.encoding): |
| 1334 |
data = data.encode(self.encoding) |
| 1335 |
self.stream.write(data) |
| 1336 |
start = end |
| 1337 |
elif breaks: |
| 1338 |
if ch is None or ch not in u"\n\x85\u2028\u2029": |
| 1339 |
if text[start] == u"\n": |
| 1340 |
self.write_line_break() |
| 1341 |
for br in text[start:end]: |
| 1342 |
if br == u"\n": |
| 1343 |
self.write_line_break() |
| 1344 |
else: |
| 1345 |
self.write_line_break(br) |
| 1346 |
self.write_indent() |
| 1347 |
start = end |
| 1348 |
else: |
| 1349 |
if ch is None or ch in u" \n\x85\u2028\u2029" or ch == u"'": |
| 1350 |
if start < end: |
| 1351 |
data = text[start:end] |
| 1352 |
self.column += len(data) |
| 1353 |
if bool(self.encoding): |
| 1354 |
data = data.encode(self.encoding) |
| 1355 |
self.stream.write(data) |
| 1356 |
start = end |
| 1357 |
if ch == u"'": |
| 1358 |
data = u"''" |
| 1359 |
self.column += 2 |
| 1360 |
if bool(self.encoding): |
| 1361 |
data = data.encode(self.encoding) |
| 1362 |
self.stream.write(data) |
| 1363 |
start = end + 1 |
| 1364 |
if ch is not None: |
| 1365 |
spaces = ch == u" " |
| 1366 |
breaks = ch in u"\n\x85\u2028\u2029" |
| 1367 |
end += 1 |
| 1368 |
self.write_indicator(u"'", False) |
| 1369 |
|
| 1370 |
ESCAPE_REPLACEMENTS = { |
| 1371 |
u"\0": u"0", |
| 1372 |
u"\x07": u"a", |
| 1373 |
u"\x08": u"b", |
| 1374 |
u"\x09": u"t", |
| 1375 |
u"\x0A": u"n", |
| 1376 |
u"\x0B": u"v", |
| 1377 |
u"\x0C": u"f", |
| 1378 |
u"\x0D": u"r", |
| 1379 |
u"\x1B": u"e", |
| 1380 |
u'"': u'"', |
| 1381 |
u"\\": u"\\", |
| 1382 |
u"\x85": u"N", |
| 1383 |
u"\xA0": u"_", |
| 1384 |
u"\u2028": u"L", |
| 1385 |
u"\u2029": u"P", |
| 1386 |
} |
| 1387 |
|
| 1388 |
def write_double_quoted(self, text, split=True): |
| 1389 |
# type: (Any, Any) -> None |
| 1390 |
if self.root_context: |
| 1391 |
if self.requested_indent is not None: |
| 1392 |
self.write_line_break() |
| 1393 |
if self.requested_indent != 0: |
| 1394 |
self.write_indent() |
| 1395 |
self.write_indicator(u'"', True) |
| 1396 |
start = end = 0 |
| 1397 |
while end <= len(text): |
| 1398 |
ch = None |
| 1399 |
if end < len(text): |
| 1400 |
ch = text[end] |
| 1401 |
if ( |
| 1402 |
ch is None |
| 1403 |
or ch in u'"\\\x85\u2028\u2029\uFEFF' |
| 1404 |
or not ( |
| 1405 |
u"\x20" <= ch <= u"\x7E" |
| 1406 |
or ( |
| 1407 |
self.allow_unicode |
| 1408 |
and (u"\xA0" <= ch <= u"\uD7FF" or u"\uE000" <= ch <= u"\uFFFD") |
| 1409 |
) |
| 1410 |
) |
| 1411 |
): |
| 1412 |
if start < end: |
| 1413 |
data = text[start:end] |
| 1414 |
self.column += len(data) |
| 1415 |
if bool(self.encoding): |
| 1416 |
data = data.encode(self.encoding) |
| 1417 |
self.stream.write(data) |
| 1418 |
start = end |
| 1419 |
if ch is not None: |
| 1420 |
if ch in self.ESCAPE_REPLACEMENTS: |
| 1421 |
data = u"\\" + self.ESCAPE_REPLACEMENTS[ch] |
| 1422 |
elif ch <= u"\xFF": |
| 1423 |
data = u"\\x%02X" % ord(ch) |
| 1424 |
elif ch <= u"\uFFFF": |
| 1425 |
data = u"\\u%04X" % ord(ch) |
| 1426 |
else: |
| 1427 |
data = u"\\U%08X" % ord(ch) |
| 1428 |
self.column += len(data) |
| 1429 |
if bool(self.encoding): |
| 1430 |
data = data.encode(self.encoding) |
| 1431 |
self.stream.write(data) |
| 1432 |
start = end + 1 |
| 1433 |
if ( |
| 1434 |
0 < end < len(text) - 1 |
| 1435 |
and (ch == u" " or start >= end) |
| 1436 |
and self.column + (end - start) > self.best_width |
| 1437 |
and split |
| 1438 |
): |
| 1439 |
data = text[start:end] + u"\\" |
| 1440 |
if start < end: |
| 1441 |
start = end |
| 1442 |
self.column += len(data) |
| 1443 |
if bool(self.encoding): |
| 1444 |
data = data.encode(self.encoding) |
| 1445 |
self.stream.write(data) |
| 1446 |
self.write_indent() |
| 1447 |
self.whitespace = False |
| 1448 |
self.indention = False |
| 1449 |
if text[start] == u" ": |
| 1450 |
data = u"\\" |
| 1451 |
self.column += len(data) |
| 1452 |
if bool(self.encoding): |
| 1453 |
data = data.encode(self.encoding) |
| 1454 |
self.stream.write(data) |
| 1455 |
end += 1 |
| 1456 |
self.write_indicator(u'"', False) |
| 1457 |
|
| 1458 |
def determine_block_hints(self, text): |
| 1459 |
# type: (Any) -> Any |
| 1460 |
indent = 0 |
| 1461 |
indicator = u"" |
| 1462 |
hints = u"" |
| 1463 |
if text: |
| 1464 |
if text[0] in u" \n\x85\u2028\u2029": |
| 1465 |
indent = self.best_sequence_indent |
| 1466 |
hints += text_type(indent) |
| 1467 |
elif self.root_context: |
| 1468 |
for end in ["\n---", "\n..."]: |
| 1469 |
pos = 0 |
| 1470 |
while True: |
| 1471 |
pos = text.find(end, pos) |
| 1472 |
if pos == -1: |
| 1473 |
break |
| 1474 |
try: |
| 1475 |
if text[pos + 4] in " \r\n": |
| 1476 |
break |
| 1477 |
except IndexError: |
| 1478 |
pass |
| 1479 |
pos += 1 |
| 1480 |
if pos > -1: |
| 1481 |
break |
| 1482 |
if pos > 0: |
| 1483 |
indent = self.best_sequence_indent |
| 1484 |
if text[-1] not in u"\n\x85\u2028\u2029": |
| 1485 |
indicator = u"-" |
| 1486 |
elif len(text) == 1 or text[-2] in u"\n\x85\u2028\u2029": |
| 1487 |
indicator = u"+" |
| 1488 |
hints += indicator |
| 1489 |
return hints, indent, indicator |
| 1490 |
|
| 1491 |
def write_folded(self, text): |
| 1492 |
# type: (Any) -> None |
| 1493 |
hints, _indent, _indicator = self.determine_block_hints(text) |
| 1494 |
self.write_indicator(u">" + hints, True) |
| 1495 |
if _indicator == u"+": |
| 1496 |
self.open_ended = True |
| 1497 |
self.write_line_break() |
| 1498 |
leading_space = True |
| 1499 |
spaces = False |
| 1500 |
breaks = True |
| 1501 |
start = end = 0 |
| 1502 |
while end <= len(text): |
| 1503 |
ch = None |
| 1504 |
if end < len(text): |
| 1505 |
ch = text[end] |
| 1506 |
if breaks: |
| 1507 |
if ch is None or ch not in u"\n\x85\u2028\u2029\a": |
| 1508 |
if ( |
| 1509 |
not leading_space |
| 1510 |
and ch is not None |
| 1511 |
and ch != u" " |
| 1512 |
and text[start] == u"\n" |
| 1513 |
): |
| 1514 |
self.write_line_break() |
| 1515 |
leading_space = ch == u" " |
| 1516 |
for br in text[start:end]: |
| 1517 |
if br == u"\n": |
| 1518 |
self.write_line_break() |
| 1519 |
else: |
| 1520 |
self.write_line_break(br) |
| 1521 |
if ch is not None: |
| 1522 |
self.write_indent() |
| 1523 |
start = end |
| 1524 |
elif spaces: |
| 1525 |
if ch != u" ": |
| 1526 |
if start + 1 == end and self.column > self.best_width: |
| 1527 |
self.write_indent() |
| 1528 |
else: |
| 1529 |
data = text[start:end] |
| 1530 |
self.column += len(data) |
| 1531 |
if bool(self.encoding): |
| 1532 |
data = data.encode(self.encoding) |
| 1533 |
self.stream.write(data) |
| 1534 |
start = end |
| 1535 |
else: |
| 1536 |
if ch is None or ch in u" \n\x85\u2028\u2029\a": |
| 1537 |
data = text[start:end] |
| 1538 |
self.column += len(data) |
| 1539 |
if bool(self.encoding): |
| 1540 |
data = data.encode(self.encoding) |
| 1541 |
self.stream.write(data) |
| 1542 |
if ch == u"\a": |
| 1543 |
if end < (len(text) - 1) and not text[end + 2].isspace(): |
| 1544 |
self.write_line_break() |
| 1545 |
self.write_indent() |
| 1546 |
end += 2 # \a and the space that is inserted on the fold |
| 1547 |
else: |
| 1548 |
raise EmitterError( |
| 1549 |
"unexcpected fold indicator \\a before space" |
| 1550 |
) |
| 1551 |
if ch is None: |
| 1552 |
self.write_line_break() |
| 1553 |
start = end |
| 1554 |
if ch is not None: |
| 1555 |
breaks = ch in u"\n\x85\u2028\u2029" |
| 1556 |
spaces = ch == u" " |
| 1557 |
end += 1 |
| 1558 |
|
| 1559 |
def write_literal(self, text, comment=None): |
| 1560 |
# type: (Any, Any) -> None |
| 1561 |
hints, _indent, _indicator = self.determine_block_hints(text) |
| 1562 |
self.write_indicator(u"|" + hints, True) |
| 1563 |
try: |
| 1564 |
comment = comment[1][0] |
| 1565 |
if comment: |
| 1566 |
self.stream.write(comment) |
| 1567 |
except (TypeError, IndexError): |
| 1568 |
pass |
| 1569 |
if _indicator == u"+": |
| 1570 |
self.open_ended = True |
| 1571 |
self.write_line_break() |
| 1572 |
breaks = True |
| 1573 |
start = end = 0 |
| 1574 |
while end <= len(text): |
| 1575 |
ch = None |
| 1576 |
if end < len(text): |
| 1577 |
ch = text[end] |
| 1578 |
if breaks: |
| 1579 |
if ch is None or ch not in u"\n\x85\u2028\u2029": |
| 1580 |
for br in text[start:end]: |
| 1581 |
if br == u"\n": |
| 1582 |
self.write_line_break() |
| 1583 |
else: |
| 1584 |
self.write_line_break(br) |
| 1585 |
if ch is not None: |
| 1586 |
if self.root_context: |
| 1587 |
idnx = self.indent if self.indent is not None else 0 |
| 1588 |
self.stream.write(u" " * (_indent + idnx)) |
| 1589 |
else: |
| 1590 |
self.write_indent() |
| 1591 |
start = end |
| 1592 |
else: |
| 1593 |
if ch is None or ch in u"\n\x85\u2028\u2029": |
| 1594 |
data = text[start:end] |
| 1595 |
if bool(self.encoding): |
| 1596 |
data = data.encode(self.encoding) |
| 1597 |
self.stream.write(data) |
| 1598 |
if ch is None: |
| 1599 |
self.write_line_break() |
| 1600 |
start = end |
| 1601 |
if ch is not None: |
| 1602 |
breaks = ch in u"\n\x85\u2028\u2029" |
| 1603 |
end += 1 |
| 1604 |
|
| 1605 |
def write_plain(self, text, split=True): |
| 1606 |
# type: (Any, Any) -> None |
| 1607 |
if self.root_context: |
| 1608 |
if self.requested_indent is not None: |
| 1609 |
self.write_line_break() |
| 1610 |
if self.requested_indent != 0: |
| 1611 |
self.write_indent() |
| 1612 |
else: |
| 1613 |
self.open_ended = True |
| 1614 |
if not text: |
| 1615 |
return |
| 1616 |
if not self.whitespace: |
| 1617 |
data = u" " |
| 1618 |
self.column += len(data) |
| 1619 |
if self.encoding: |
| 1620 |
data = data.encode(self.encoding) |
| 1621 |
self.stream.write(data) |
| 1622 |
self.whitespace = False |
| 1623 |
self.indention = False |
| 1624 |
spaces = False |
| 1625 |
breaks = False |
| 1626 |
start = end = 0 |
| 1627 |
while end <= len(text): |
| 1628 |
ch = None |
| 1629 |
if end < len(text): |
| 1630 |
ch = text[end] |
| 1631 |
if spaces: |
| 1632 |
if ch != u" ": |
| 1633 |
if start + 1 == end and self.column > self.best_width and split: |
| 1634 |
self.write_indent() |
| 1635 |
self.whitespace = False |
| 1636 |
self.indention = False |
| 1637 |
else: |
| 1638 |
data = text[start:end] |
| 1639 |
self.column += len(data) |
| 1640 |
if self.encoding: |
| 1641 |
data = data.encode(self.encoding) |
| 1642 |
self.stream.write(data) |
| 1643 |
start = end |
| 1644 |
elif breaks: |
| 1645 |
if ch not in u"\n\x85\u2028\u2029": # type: ignore |
| 1646 |
if text[start] == u"\n": |
| 1647 |
self.write_line_break() |
| 1648 |
for br in text[start:end]: |
| 1649 |
if br == u"\n": |
| 1650 |
self.write_line_break() |
| 1651 |
else: |
| 1652 |
self.write_line_break(br) |
| 1653 |
self.write_indent() |
| 1654 |
self.whitespace = False |
| 1655 |
self.indention = False |
| 1656 |
start = end |
| 1657 |
else: |
| 1658 |
if ch is None or ch in u" \n\x85\u2028\u2029": |
| 1659 |
data = text[start:end] |
| 1660 |
self.column += len(data) |
| 1661 |
if self.encoding: |
| 1662 |
data = data.encode(self.encoding) |
| 1663 |
try: |
| 1664 |
self.stream.write(data) |
| 1665 |
except: # NOQA |
| 1666 |
sys.stdout.write(repr(data) + "\n") |
| 1667 |
raise |
| 1668 |
start = end |
| 1669 |
if ch is not None: |
| 1670 |
spaces = ch == u" " |
| 1671 |
breaks = ch in u"\n\x85\u2028\u2029" |
| 1672 |
end += 1 |
| 1673 |
|
| 1674 |
def write_comment(self, comment, pre=False): |
| 1675 |
# type: (Any, bool) -> None |
| 1676 |
value = comment.value |
| 1677 |
# nprintf('{:02d} {:02d} {!r}'.format(self.column, comment.start_mark.column, value)) |
| 1678 |
if not pre and value[-1] == "\n": |
| 1679 |
value = value[:-1] |
| 1680 |
try: |
| 1681 |
# get original column position |
| 1682 |
col = comment.start_mark.column |
| 1683 |
if comment.value and comment.value.startswith("\n"): |
| 1684 |
# never inject extra spaces if the comment starts with a newline |
| 1685 |
# and not a real comment (e.g. if you have an empty line following a key-value |
| 1686 |
col = self.column |
| 1687 |
elif col < self.column + 1: |
| 1688 |
ValueError |
| 1689 |
except ValueError: |
| 1690 |
col = self.column + 1 |
| 1691 |
# nprint('post_comment', self.line, self.column, value) |
| 1692 |
try: |
| 1693 |
# at least one space if the current column >= the start column of the comment |
| 1694 |
# but not at the start of a line |
| 1695 |
nr_spaces = col - self.column |
| 1696 |
if self.column and value.strip() and nr_spaces < 1 and value[0] != "\n": |
| 1697 |
nr_spaces = 1 |
| 1698 |
value = " " * nr_spaces + value |
| 1699 |
try: |
| 1700 |
if bool(self.encoding): |
| 1701 |
value = value.encode(self.encoding) |
| 1702 |
except UnicodeDecodeError: |
| 1703 |
pass |
| 1704 |
self.stream.write(value) |
| 1705 |
except TypeError: |
| 1706 |
raise |
| 1707 |
if not pre: |
| 1708 |
self.write_line_break() |
| 1709 |
|
| 1710 |
def write_pre_comment(self, event): |
| 1711 |
# type: (Any) -> bool |
| 1712 |
comments = event.comment[1] |
| 1713 |
if comments is None: |
| 1714 |
return False |
| 1715 |
try: |
| 1716 |
start_events = (MappingStartEvent, SequenceStartEvent) |
| 1717 |
for comment in comments: |
| 1718 |
if isinstance(event, start_events) and getattr( |
| 1719 |
comment, "pre_done", None |
| 1720 |
): |
| 1721 |
continue |
| 1722 |
if self.column != 0: |
| 1723 |
self.write_line_break() |
| 1724 |
self.write_comment(comment, pre=True) |
| 1725 |
if isinstance(event, start_events): |
| 1726 |
comment.pre_done = True |
| 1727 |
except TypeError: |
| 1728 |
sys.stdout.write("eventtt {} {}".format(type(event), event)) |
| 1729 |
raise |
| 1730 |
return True |
| 1731 |
|
| 1732 |
def write_post_comment(self, event): |
| 1733 |
# type: (Any) -> bool |
| 1734 |
if self.event.comment[0] is None: |
| 1735 |
return False |
| 1736 |
comment = event.comment[0] |
| 1737 |
self.write_comment(comment) |
| 1738 |
return True |