summaryrefslogtreecommitdiff
path: root/serializer.py
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2018-08-03 22:14:57 +0200
committerAnthon van der Neut <anthon@mnt.org>2018-08-03 22:14:57 +0200
commitdce10fcff1de54121fb8b440b883ef5d3fe2f96a (patch)
tree072b4bd247e6f1cd95c08c7b67fea0fc96f0578e /serializer.py
parent2966a4f215861fa05e0dc7e0cd53350766e794c6 (diff)
downloadruamel.yaml-dce10fcff1de54121fb8b440b883ef5d3fe2f96a.tar.gz
Apply oitnb and mypy 0.620, then make everything work again0.15.48
Diffstat (limited to 'serializer.py')
-rw-r--r--serializer.py103
1 files changed, 70 insertions, 33 deletions
diff --git a/serializer.py b/serializer.py
index c88539d..b2b26bf 100644
--- a/serializer.py
+++ b/serializer.py
@@ -7,13 +7,18 @@ from ruamel.yaml.compat import nprint, DBG_NODE, dbg, string_types
from ruamel.yaml.util import RegExp
from ruamel.yaml.events import (
- StreamStartEvent, StreamEndEvent, MappingStartEvent, MappingEndEvent,
- SequenceStartEvent, SequenceEndEvent, AliasEvent, ScalarEvent,
- DocumentStartEvent, DocumentEndEvent,
-)
-from ruamel.yaml.nodes import (
- MappingNode, ScalarNode, SequenceNode,
+ StreamStartEvent,
+ StreamEndEvent,
+ MappingStartEvent,
+ MappingEndEvent,
+ SequenceStartEvent,
+ SequenceEndEvent,
+ AliasEvent,
+ ScalarEvent,
+ DocumentStartEvent,
+ DocumentEndEvent,
)
+from ruamel.yaml.nodes import MappingNode, ScalarNode, SequenceNode
if False: # MYPY
from typing import Any, Dict, Union, Text # NOQA
@@ -32,9 +37,16 @@ class Serializer(object):
ANCHOR_TEMPLATE = u'id%03d'
ANCHOR_RE = RegExp(u'id(?!000$)\\d{3,}')
- def __init__(self, encoding=None, explicit_start=None, explicit_end=None,
- version=None, tags=None, dumper=None):
- # type: (Any, bool, bool, VersionType, Any, Any) -> None
+ def __init__(
+ self,
+ encoding=None,
+ explicit_start=None,
+ explicit_end=None,
+ version=None,
+ tags=None,
+ dumper=None,
+ ):
+ # type: (Any, Union[None, bool], Union[None, bool], Union[None, VersionType], Any, Any) -> None # NOQA
self.dumper = dumper
if self.dumper is not None:
self.dumper._serializer = self
@@ -56,15 +68,15 @@ class Serializer(object):
def emitter(self):
# type: () -> Any
if hasattr(self.dumper, 'typ'):
- return self.dumper.emitter # type: ignore
- return self.dumper._emitter # type: ignore
+ return self.dumper.emitter
+ return self.dumper._emitter
@property
def resolver(self):
# type: () -> Any
if hasattr(self.dumper, 'typ'):
- self.dumper.resolver # type: ignore
- return self.dumper._resolver # type: ignore
+ self.dumper.resolver
+ return self.dumper._resolver
def open(self):
# type: () -> None
@@ -72,14 +84,14 @@ class Serializer(object):
self.emitter.emit(StreamStartEvent(encoding=self.use_encoding))
self.closed = False
elif self.closed:
- raise SerializerError("serializer is closed")
+ raise SerializerError('serializer is closed')
else:
- raise SerializerError("serializer is already opened")
+ raise SerializerError('serializer is already opened')
def close(self):
# type: () -> None
if self.closed is None:
- raise SerializerError("serializer is not opened")
+ raise SerializerError('serializer is not opened')
elif not self.closed:
self.emitter.emit(StreamEndEvent())
self.closed = True
@@ -93,12 +105,14 @@ class Serializer(object):
nprint('Serializing nodes')
node.dump()
if self.closed is None:
- raise SerializerError("serializer is not opened")
+ raise SerializerError('serializer is not opened')
elif self.closed:
- raise SerializerError("serializer is closed")
- self.emitter.emit(DocumentStartEvent(explicit=self.use_explicit_start,
- version=self.use_version,
- tags=self.use_tags))
+ raise SerializerError('serializer is closed')
+ self.emitter.emit(
+ DocumentStartEvent(
+ explicit=self.use_explicit_start, version=self.use_version, tags=self.use_tags
+ )
+ )
self.anchor_node(node)
self.serialize_node(node, None, None)
self.emitter.emit(DocumentEndEvent(explicit=self.use_explicit_end))
@@ -151,12 +165,23 @@ class Serializer(object):
# if not equal quoting is necessary for strings
detected_tag = self.resolver.resolve(ScalarNode, node.value, (True, False))
default_tag = self.resolver.resolve(ScalarNode, node.value, (False, True))
- implicit = ((node.tag == detected_tag), (node.tag == default_tag),
- node.tag.startswith('tag:yaml.org,2002:'))
- self.emitter.emit(ScalarEvent(alias, node.tag, implicit, node.value,
- style=node.style, comment=node.comment))
+ implicit = (
+ (node.tag == detected_tag),
+ (node.tag == default_tag),
+ node.tag.startswith('tag:yaml.org,2002:'),
+ )
+ self.emitter.emit(
+ ScalarEvent(
+ alias,
+ node.tag,
+ implicit,
+ node.value,
+ style=node.style,
+ comment=node.comment,
+ )
+ )
elif isinstance(node, SequenceNode):
- implicit = (node.tag == self.resolver.resolve(SequenceNode, node.value, True))
+ implicit = node.tag == self.resolver.resolve(SequenceNode, node.value, True)
comment = node.comment
end_comment = None
seq_comment = None
@@ -168,16 +193,22 @@ class Serializer(object):
end_comment = comment[2]
else:
end_comment = None
- self.emitter.emit(SequenceStartEvent(alias, node.tag, implicit,
- flow_style=node.flow_style,
- comment=node.comment))
+ self.emitter.emit(
+ SequenceStartEvent(
+ alias,
+ node.tag,
+ implicit,
+ flow_style=node.flow_style,
+ comment=node.comment,
+ )
+ )
index = 0
for item in node.value:
self.serialize_node(item, node, index)
index += 1
self.emitter.emit(SequenceEndEvent(comment=[seq_comment, end_comment]))
elif isinstance(node, MappingNode):
- implicit = (node.tag == self.resolver.resolve(MappingNode, node.value, True))
+ implicit = node.tag == self.resolver.resolve(MappingNode, node.value, True)
comment = node.comment
end_comment = None
map_comment = None
@@ -187,9 +218,15 @@ class Serializer(object):
# comment[0] = None
if comment and len(comment) > 2:
end_comment = comment[2]
- self.emitter.emit(MappingStartEvent(alias, node.tag, implicit,
- flow_style=node.flow_style,
- comment=node.comment))
+ self.emitter.emit(
+ MappingStartEvent(
+ alias,
+ node.tag,
+ implicit,
+ flow_style=node.flow_style,
+ comment=node.comment,
+ )
+ )
for key, value in node.value:
self.serialize_node(key, node, None)
self.serialize_node(value, node, key)