From 68ea6b9ef755296c169b5416fe46ac48250e3c62 Mon Sep 17 00:00:00 2001 From: Anthon van der Neut Date: Fri, 9 Nov 2018 10:47:11 +0100 Subject: fix issue #194 object attributes always sorted Test for this issue in: https://bitbucket.org/ruamel/yaml.data/src/default/object/control_base_representer_mapping_sort.yaml *When this change indeed resolves your problem, please **Close** this issue*. *(You can do so using the WorkFlow pull-down (close to the top right of this page))* --- scalarstring.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'scalarstring.py') diff --git a/scalarstring.py b/scalarstring.py index 03db72d..c7b1a78 100644 --- a/scalarstring.py +++ b/scalarstring.py @@ -3,6 +3,7 @@ from __future__ import print_function, absolute_import, division, unicode_literals from ruamel.yaml.compat import text_type +from ruamel.yaml.anchor import Anchor if False: # MYPY from typing import Text, Any, Dict, List # NOQA @@ -20,7 +21,7 @@ __all__ = [ class ScalarString(text_type): - __slots__ = () + __slots__ = (Anchor.attrib) def __new__(cls, *args, **kw): # type: (Any, Any) -> Any @@ -30,6 +31,26 @@ class ScalarString(text_type): # type: (Any, Any, int) -> Any return type(self)((text_type.replace(self, old, new, maxreplace))) + @property + def anchor(self): + # type: () -> Any + if not hasattr(self, Anchor.attrib): + setattr(self, Anchor.attrib, Anchor()) + return getattr(self, Anchor.attrib) + + def yaml_anchor(self): + # type: () -> Any + if not hasattr(self, Anchor.attrib): + return None + if not self.anchor.always_dump: + return None + return self.anchor + + def yaml_set_anchor(self, value, always_dump=False): + # type: (Any, bool) -> None + self.anchor.value = value + self.anchor.always_dump = always_dump + class LiteralScalarString(ScalarString): __slots__ = 'comment' # the comment after the | on the first line -- cgit v1.2.1