From 25b7008eb7721763fe0ea10cc23abeed2c1ef780 Mon Sep 17 00:00:00 2001 From: Anthon van der Neut Date: Thu, 27 Dec 2018 23:37:32 +0100 Subject: roundtrip anchors/aliases on str, int, float --- scalarstring.py | 51 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 18 deletions(-) (limited to 'scalarstring.py') diff --git a/scalarstring.py b/scalarstring.py index 0566164..bf3cbab 100644 --- a/scalarstring.py +++ b/scalarstring.py @@ -14,6 +14,7 @@ __all__ = [ 'FoldedScalarString', 'SingleQuotedScalarString', 'DoubleQuotedScalarString', + 'PlainScalarString', # PreservedScalarString is the old name, as it was the first to be preserved on rt, # use LiteralScalarString instead 'PreservedScalarString', @@ -25,7 +26,11 @@ class ScalarString(text_type): def __new__(cls, *args, **kw): # type: (Any, Any) -> Any - return text_type.__new__(cls, *args, **kw) # type: ignore + anchor = kw.pop('anchor', None) # type: ignore + ret_val = text_type.__new__(cls, *args, **kw) # type: ignore + if anchor is not None: + ret_val.yaml_set_anchor(anchor, always_dump=True) + return ret_val def replace(self, old, new, maxreplace=-1): # type: (Any, Any, int) -> Any @@ -38,13 +43,13 @@ class ScalarString(text_type): setattr(self, Anchor.attrib, Anchor()) return getattr(self, Anchor.attrib) - def yaml_anchor(self): - # type: () -> Any + def yaml_anchor(self, any=False): + # type: (bool) -> Any if not hasattr(self, Anchor.attrib): return None - if not self.anchor.always_dump: - return None - return self.anchor + if any or self.anchor.always_dump: + return self.anchor + return None def yaml_set_anchor(self, value, always_dump=False): # type: (Any, bool) -> None @@ -57,9 +62,9 @@ class LiteralScalarString(ScalarString): style = '|' - def __new__(cls, value): - # type: (Text) -> Any - return ScalarString.__new__(cls, value) + def __new__(cls, value, anchor=None): + # type: (Text, Any) -> Any + return ScalarString.__new__(cls, value, anchor=anchor) PreservedScalarString = LiteralScalarString @@ -70,9 +75,9 @@ class FoldedScalarString(ScalarString): style = '>' - def __new__(cls, value): - # type: (Text) -> Any - return ScalarString.__new__(cls, value) + def __new__(cls, value, anchor=None): + # type: (Text, Any) -> Any + return ScalarString.__new__(cls, value, anchor=anchor) class SingleQuotedScalarString(ScalarString): @@ -80,9 +85,9 @@ class SingleQuotedScalarString(ScalarString): style = "'" - def __new__(cls, value): - # type: (Text) -> Any - return ScalarString.__new__(cls, value) + def __new__(cls, value, anchor=None): + # type: (Text, Any) -> Any + return ScalarString.__new__(cls, value, anchor=anchor) class DoubleQuotedScalarString(ScalarString): @@ -90,9 +95,19 @@ class DoubleQuotedScalarString(ScalarString): style = '"' - def __new__(cls, value): - # type: (Text) -> Any - return ScalarString.__new__(cls, value) + def __new__(cls, value, anchor=None): + # type: (Text, Any) -> Any + return ScalarString.__new__(cls, value, anchor=anchor) + + +class PlainScalarString(ScalarString): + __slots__ = () + + style = '' + + def __new__(cls, value, anchor=None): + # type: (Text, Any) -> Any + return ScalarString.__new__(cls, value, anchor=anchor) def preserve_literal(s): -- cgit v1.2.1