# Characteristics of the last emitted character: # - current position. # - is it a whitespace? # - is it an indention character # (indentation space, '-', '?', or ':')?
self.line = 0
self.column = 0
self.whitespace = True
self.indention = True
# Whether the document requires an explicit document indicator
self.open_ended = False
# Formatting details.
self.canonical = canonical
self.allow_unicode = allow_unicode
self.best_indent = 2 if indent and 1 < indent < 10:
self.best_indent = indent
self.best_width = 80 if width and width > self.best_indent*2:
self.best_width = width
self.best_line_break = '\n' if line_break in ['\r', '\n', '\r\n']:
self.best_line_break = line_break
def expect_node(self, root=False, sequence=False, mapping=False,
simple_key=False):
self.root_context = root
self.sequence_context = sequence
self.mapping_context = mapping
self.simple_key_context = simple_key if isinstance(self.event, AliasEvent):
self.expect_alias() elif isinstance(self.event, (ScalarEvent, CollectionStartEvent)):
self.process_anchor('&')
self.process_tag() if isinstance(self.event, ScalarEvent):
self.expect_scalar() elif isinstance(self.event, SequenceStartEvent): if self.flow_level or self.canonical or self.event.flow_style \ or self.check_empty_sequence():
self.expect_flow_sequence() else:
self.expect_block_sequence() elif isinstance(self.event, MappingStartEvent): if self.flow_level or self.canonical or self.event.flow_style \ or self.check_empty_mapping():
self.expect_flow_mapping() else:
self.expect_block_mapping() else: raise EmitterError("expected NodeEvent, but got %s" % self.event)
def expect_alias(self): if self.event.anchor isNone: raise EmitterError("anchor is not specified for alias")
self.process_anchor('*')
self.state = self.states.pop()
def check_empty_sequence(self): return (isinstance(self.event, SequenceStartEvent) and self.events and isinstance(self.events[0], SequenceEndEvent))
def check_empty_mapping(self): return (isinstance(self.event, MappingStartEvent) and self.events and isinstance(self.events[0], MappingEndEvent))
def check_empty_document(self): ifnot isinstance(self.event, DocumentStartEvent) ornot self.events: returnFalse
event = self.events[0] return (isinstance(event, ScalarEvent) and event.anchor isNone and event.tag isNoneand event.implicit and event.value == '')
def check_simple_key(self):
length = 0 if isinstance(self.event, NodeEvent) and self.event.anchor isnotNone: if self.prepared_anchor isNone:
self.prepared_anchor = self.prepare_anchor(self.event.anchor)
length += len(self.prepared_anchor) if isinstance(self.event, (ScalarEvent, CollectionStartEvent)) \ and self.event.tag isnotNone: if self.prepared_tag isNone:
self.prepared_tag = self.prepare_tag(self.event.tag)
length += len(self.prepared_tag) if isinstance(self.event, ScalarEvent): if self.analysis isNone:
self.analysis = self.analyze_scalar(self.event.value)
length += len(self.analysis.scalar) return (length < 128 and (isinstance(self.event, AliasEvent) or (isinstance(self.event, ScalarEvent) andnot self.analysis.empty andnot self.analysis.multiline) or self.check_empty_sequence() or self.check_empty_mapping()))
# Anchor, Tag, and Scalar processors.
def process_anchor(self, indicator): if self.event.anchor isNone:
self.prepared_anchor = None return if self.prepared_anchor isNone:
self.prepared_anchor = self.prepare_anchor(self.event.anchor) if self.prepared_anchor:
self.write_indicator(indicator+self.prepared_anchor, True)
self.prepared_anchor = None
def process_tag(self):
tag = self.event.tag if isinstance(self.event, ScalarEvent): if self.style isNone:
self.style = self.choose_scalar_style() if ((not self.canonical or tag isNone) and
((self.style == ''and self.event.implicit[0]) or (self.style != ''and self.event.implicit[1]))):
self.prepared_tag = None return if self.event.implicit[0] and tag isNone:
tag = '!'
self.prepared_tag = None else: if (not self.canonical or tag isNone) and self.event.implicit:
self.prepared_tag = None return if tag isNone: raise EmitterError("tag is not specified") if self.prepared_tag isNone:
self.prepared_tag = self.prepare_tag(tag) if self.prepared_tag:
self.write_indicator(self.prepared_tag, True)
self.prepared_tag = None
def choose_scalar_style(self): if self.analysis isNone:
self.analysis = self.analyze_scalar(self.event.value) if self.event.style == '"'or self.canonical: return'"' ifnot self.event.style and self.event.implicit[0]: if (not (self.simple_key_context and
(self.analysis.empty or self.analysis.multiline)) and (self.flow_level and self.analysis.allow_flow_plain or (not self.flow_level and self.analysis.allow_block_plain))): return'' if self.event.style and self.event.style in'|>': if (not self.flow_level andnot self.simple_key_context and self.analysis.allow_block): return self.event.style ifnot self.event.style or self.event.style == '\'': if (self.analysis.allow_single_quoted and not (self.simple_key_context and self.analysis.multiline)): return'\'' return'"'
def process_scalar(self): if self.analysis isNone:
self.analysis = self.analyze_scalar(self.event.value) if self.style isNone:
self.style = self.choose_scalar_style()
split = (not self.simple_key_context) #if self.analysis.multiline and split \ # and (not self.style or self.style in '\'\"'): # self.write_indent() if self.style == '"':
self.write_double_quoted(self.analysis.scalar, split) elif self.style == '\'':
self.write_single_quoted(self.analysis.scalar, split) elif self.style == '>':
self.write_folded(self.analysis.scalar) elif self.style == '|':
self.write_literal(self.analysis.scalar) else:
self.write_plain(self.analysis.scalar, split)
self.analysis = None
self.style = None
# Analyzers.
def prepare_version(self, version):
major, minor = version if major != 1: raise EmitterError("unsupported YAML version: %d.%d" % (major, minor)) return'%d.%d' % (major, minor)
def prepare_tag_handle(self, handle): ifnot handle: raise EmitterError("tag handle must not be empty") if handle[0] != '!'or handle[-1] != '!': raise EmitterError("tag handle must start and end with '!': %r" % handle) for ch in handle[1:-1]: ifnot ('0' <= ch <= '9'or'A' <= ch <= 'Z'or'a' <= ch <= 'z' \ or ch in'-_'): raise EmitterError("invalid character %r in the tag handle: %r"
% (ch, handle)) return handle
def prepare_tag_prefix(self, prefix): ifnot prefix: raise EmitterError("tag prefix must not be empty")
chunks = []
start = end = 0 if prefix[0] == '!':
end = 1 while end < len(prefix):
ch = prefix[end] if'0' <= ch <= '9'or'A' <= ch <= 'Z'or'a' <= ch <= 'z' \ or ch in'-;/?!:@&=+$,_.~*\'()[]':
end += 1 else: if start < end:
chunks.append(prefix[start:end])
start = end = end+1
data = ch.encode('utf-8') for ch in data:
chunks.append('%%%02X' % ord(ch)) if start < end:
chunks.append(prefix[start:end]) return''.join(chunks)
def prepare_tag(self, tag): ifnot tag: raise EmitterError("tag must not be empty") if tag == '!': return tag
handle = None
suffix = tag
prefixes = sorted(self.tag_prefixes.keys()) for prefix in prefixes: if tag.startswith(prefix) \ and (prefix == '!'or len(prefix) < len(tag)):
handle = self.tag_prefixes[prefix]
suffix = tag[len(prefix):]
chunks = []
start = end = 0 while end < len(suffix):
ch = suffix[end] if'0' <= ch <= '9'or'A' <= ch <= 'Z'or'a' <= ch <= 'z' \ or ch in'-;/?:@&=+$,_.~*\'()[]' \ or (ch == '!'and handle != '!'):
end += 1 else: if start < end:
chunks.append(suffix[start:end])
start = end = end+1
data = ch.encode('utf-8') for ch in data:
chunks.append('%%%02X' % ch) if start < end:
chunks.append(suffix[start:end])
suffix_text = ''.join(chunks) if handle: return'%s%s' % (handle, suffix_text) else: return'!<%s>' % suffix_text
def prepare_anchor(self, anchor): ifnot anchor: raise EmitterError("anchor must not be empty") for ch in anchor: ifnot ('0' <= ch <= '9'or'A' <= ch <= 'Z'or'a' <= ch <= 'z' \ or ch in'-_'): raise EmitterError("invalid character %r in the anchor: %r"
% (ch, anchor)) return anchor
def analyze_scalar(self, scalar):
# Empty scalar is a special case. ifnot scalar: return ScalarAnalysis(scalar=scalar, empty=True, multiline=False,
allow_flow_plain=False, allow_block_plain=True,
allow_single_quoted=True, allow_double_quoted=True,
allow_block=False)
# Indicators and special characters.
block_indicators = False
flow_indicators = False
line_breaks = False
special_characters = False
# Check document indicators. if scalar.startswith('---') or scalar.startswith('...'):
block_indicators = True
flow_indicators = True
# First character or preceded by a whitespace.
preceded_by_whitespace = True
# Last character or followed by a whitespace.
followed_by_whitespace = (len(scalar) == 1 or
scalar[1] in'\0 \t\r\n\x85\u2028\u2029')
# The previous character is a space.
previous_space = False
# The previous character is a break.
previous_break = False
index = 0 while index < len(scalar):
ch = scalar[index]
# Check for indicators. if index == 0: # Leading indicators are special characters. if ch in'#,[]{}&*!|>\'\"%@`':
flow_indicators = True
block_indicators = True if ch in'?:':
flow_indicators = True if followed_by_whitespace:
block_indicators = True if ch == '-'and followed_by_whitespace:
flow_indicators = True
block_indicators = True else: # Some indicators cannot appear within a scalar as well. if ch in',?[]{}':
flow_indicators = True if ch == ':':
flow_indicators = True if followed_by_whitespace:
block_indicators = True if ch == '#' and preceded_by_whitespace:
flow_indicators = True
block_indicators = True
# Check for line breaks, special, and unicode characters. if ch in'\n\x85\u2028\u2029':
line_breaks = True ifnot (ch == '\n'or'\x20' <= ch <= '\x7E'): if (ch == '\x85'or'\xA0' <= ch <= '\uD7FF' or'\uE000' <= ch <= '\uFFFD' or'\U00010000' <= ch < '\U0010ffff') and ch != '\uFEFF':
unicode_characters = True ifnot self.allow_unicode:
special_characters = True else:
special_characters = True
# Detect important whitespace combinations. if ch == ' ': if index == 0:
leading_space = True if index == len(scalar)-1:
trailing_space = True if previous_break:
break_space = True
previous_space = True
previous_break = False elif ch in'\n\x85\u2028\u2029': if index == 0:
leading_break = True if index == len(scalar)-1:
trailing_break = True if previous_space:
space_break = True
previous_space = False
previous_break = True else:
previous_space = False
previous_break = False
# Prepare for the next character.
index += 1
preceded_by_whitespace = (ch in'\0 \t\r\n\x85\u2028\u2029')
followed_by_whitespace = (index+1 >= len(scalar) or
scalar[index+1] in'\0 \t\r\n\x85\u2028\u2029')
# Leading and trailing whitespaces are bad for plain scalars. if (leading_space or leading_break or trailing_space or trailing_break):
allow_flow_plain = allow_block_plain = False
# We do not permit trailing spaces for block scalars. if trailing_space:
allow_block = False
# Spaces at the beginning of a new line are only acceptable for block # scalars. if break_space:
allow_flow_plain = allow_block_plain = allow_single_quoted = False
# Spaces followed by breaks, as well as special character are only # allowed for double quoted scalars. if space_break or special_characters:
allow_flow_plain = allow_block_plain = \
allow_single_quoted = allow_block = False
# Although the plain scalar writer supports breaks, we never emit # multiline plain scalars. if line_breaks:
allow_flow_plain = allow_block_plain = False
# Flow indicators are forbidden for flow plain scalars. if flow_indicators:
allow_flow_plain = False
# Block indicators are forbidden for block plain scalars. if block_indicators:
allow_block_plain = False
def flush_stream(self): if hasattr(self.stream, 'flush'):
self.stream.flush()
def write_stream_start(self): # Write BOM if needed. if self.encoding and self.encoding.startswith('utf-16'):
self.stream.write('\uFEFF'.encode(self.encoding))
def write_stream_end(self):
self.flush_stream()
def write_indicator(self, indicator, need_whitespace,
whitespace=False, indention=False): if self.whitespace ornot need_whitespace:
data = indicator else:
data = ' '+indicator
self.whitespace = whitespace
self.indention = self.indention and indention
self.column += len(data)
self.open_ended = False if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data)
def write_indent(self):
indent = self.indent or 0 ifnot self.indention or self.column > indent \ or (self.column == indent andnot self.whitespace):
self.write_line_break() if self.column < indent:
self.whitespace = True
data = ' '*(indent-self.column)
self.column = indent if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data)
def write_line_break(self, data=None): if data isNone:
data = self.best_line_break
self.whitespace = True
self.indention = True
self.line += 1
self.column = 0 if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data)
def write_version_directive(self, version_text):
data = '%%YAML %s' % version_text if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data)
self.write_line_break()
def write_tag_directive(self, handle_text, prefix_text):
data = '%%TAG %s %s' % (handle_text, prefix_text) if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data)
self.write_line_break()
# Scalar streams.
def write_single_quoted(self, text, split=True):
self.write_indicator('\'', True)
spaces = False
breaks = False
start = end = 0 while end <= len(text):
ch = None if end < len(text):
ch = text[end] if spaces: if ch isNoneor ch != ' ': if start+1 == end and self.column > self.best_width and split \ and start != 0 and end != len(text):
self.write_indent() else:
data = text[start:end]
self.column += len(data) if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data)
start = end elif breaks: if ch isNoneor ch notin'\n\x85\u2028\u2029': if text[start] == '\n':
self.write_line_break() for br in text[start:end]: if br == '\n':
self.write_line_break() else:
self.write_line_break(br)
self.write_indent()
start = end else: if ch isNoneor ch in' \n\x85\u2028\u2029'or ch == '\'': if start < end:
data = text[start:end]
self.column += len(data) if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data)
start = end if ch == '\'':
data = '\'\''
self.column += 2 if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data)
start = end + 1 if ch isnotNone:
spaces = (ch == ' ')
breaks = (ch in'\n\x85\u2028\u2029')
end += 1
self.write_indicator('\'', False)
def write_double_quoted(self, text, split=True):
self.write_indicator('"', True)
start = end = 0 while end <= len(text):
ch = None if end < len(text):
ch = text[end] if ch isNoneor ch in'"\\\x85\u2028\u2029\uFEFF' \ ornot ('\x20' <= ch <= '\x7E' or (self.allow_unicode and ('\xA0' <= ch <= '\uD7FF' or'\uE000' <= ch <= '\uFFFD'))): if start < end:
data = text[start:end]
self.column += len(data) if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data)
start = end if ch isnotNone: if ch in self.ESCAPE_REPLACEMENTS:
data = '\\'+self.ESCAPE_REPLACEMENTS[ch] elif ch <= '\xFF':
data = '\\x%02X' % ord(ch) elif ch <= '\uFFFF':
data = '\\u%04X' % ord(ch) else:
data = '\\U%08X' % ord(ch)
self.column += len(data) if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data)
start = end+1 if 0 < end < len(text)-1 and (ch == ' 'or start >= end) \ and self.column+(end-start) > self.best_width and split:
data = text[start:end]+'\\' if start < end:
start = end
self.column += len(data) if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data)
self.write_indent()
self.whitespace = False
self.indention = False if text[start] == ' ':
data = '\\'
self.column += len(data) if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data)
end += 1
self.write_indicator('"', False)
def determine_block_hints(self, text):
hints = '' if text: if text[0] in' \n\x85\u2028\u2029':
hints += str(self.best_indent) if text[-1] notin'\n\x85\u2028\u2029':
hints += '-' elif len(text) == 1 or text[-2] in'\n\x85\u2028\u2029':
hints += '+' return hints
def write_folded(self, text):
hints = self.determine_block_hints(text)
self.write_indicator('>'+hints, True) if hints[-1:] == '+':
self.open_ended = True
self.write_line_break()
leading_space = True
spaces = False
breaks = True
start = end = 0 while end <= len(text):
ch = None if end < len(text):
ch = text[end] if breaks: if ch isNoneor ch notin'\n\x85\u2028\u2029': ifnot leading_space and ch isnotNoneand ch != ' ' \ and text[start] == '\n':
self.write_line_break()
leading_space = (ch == ' ') for br in text[start:end]: if br == '\n':
self.write_line_break() else:
self.write_line_break(br) if ch isnotNone:
self.write_indent()
start = end elif spaces: if ch != ' ': if start+1 == end and self.column > self.best_width:
self.write_indent() else:
data = text[start:end]
self.column += len(data) if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data)
start = end else: if ch isNoneor ch in' \n\x85\u2028\u2029':
data = text[start:end]
self.column += len(data) if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data) if ch isNone:
self.write_line_break()
start = end if ch isnotNone:
breaks = (ch in'\n\x85\u2028\u2029')
spaces = (ch == ' ')
end += 1
def write_literal(self, text):
hints = self.determine_block_hints(text)
self.write_indicator('|'+hints, True) if hints[-1:] == '+':
self.open_ended = True
self.write_line_break()
breaks = True
start = end = 0 while end <= len(text):
ch = None if end < len(text):
ch = text[end] if breaks: if ch isNoneor ch notin'\n\x85\u2028\u2029': for br in text[start:end]: if br == '\n':
self.write_line_break() else:
self.write_line_break(br) if ch isnotNone:
self.write_indent()
start = end else: if ch isNoneor ch in'\n\x85\u2028\u2029':
data = text[start:end] if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data) if ch isNone:
self.write_line_break()
start = end if ch isnotNone:
breaks = (ch in'\n\x85\u2028\u2029')
end += 1
def write_plain(self, text, split=True): if self.root_context:
self.open_ended = True ifnot text: return ifnot self.whitespace:
data = ' '
self.column += len(data) if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data)
self.whitespace = False
self.indention = False
spaces = False
breaks = False
start = end = 0 while end <= len(text):
ch = None if end < len(text):
ch = text[end] if spaces: if ch != ' ': if start+1 == end and self.column > self.best_width and split:
self.write_indent()
self.whitespace = False
self.indention = False else:
data = text[start:end]
self.column += len(data) if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data)
start = end elif breaks: if ch notin'\n\x85\u2028\u2029': if text[start] == '\n':
self.write_line_break() for br in text[start:end]: if br == '\n':
self.write_line_break() else:
self.write_line_break(br)
self.write_indent()
self.whitespace = False
self.indention = False
start = end else: if ch isNoneor ch in' \n\x85\u2028\u2029':
data = text[start:end]
self.column += len(data) if self.encoding:
data = data.encode(self.encoding)
self.stream.write(data)
start = end if ch isnotNone:
spaces = (ch == ' ')
breaks = (ch in'\n\x85\u2028\u2029')
end += 1
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.