summaryrefslogtreecommitdiff
path: root/scalarstring.py
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2018-12-27 23:37:32 +0100
committerAnthon van der Neut <anthon@mnt.org>2018-12-27 23:37:32 +0100
commit25b7008eb7721763fe0ea10cc23abeed2c1ef780 (patch)
tree6ea378f32bd05947a84bcdceedc1d90418aca13f /scalarstring.py
parentcdb3ca654eb8f6ef734b82bf9130045ce6d85e45 (diff)
downloadruamel.yaml-25b7008eb7721763fe0ea10cc23abeed2c1ef780.tar.gz
roundtrip anchors/aliases on str, int, float
Diffstat (limited to 'scalarstring.py')
-rw-r--r--scalarstring.py51
1 files changed, 33 insertions, 18 deletions
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):