summaryrefslogtreecommitdiff
path: root/scalarint.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 /scalarint.py
parentcdb3ca654eb8f6ef734b82bf9130045ce6d85e45 (diff)
downloadruamel.yaml-25b7008eb7721763fe0ea10cc23abeed2c1ef780.tar.gz
roundtrip anchors/aliases on str, int, float
Diffstat (limited to 'scalarint.py')
-rw-r--r--scalarint.py60
1 files changed, 46 insertions, 14 deletions
diff --git a/scalarint.py b/scalarint.py
index 2bd605d..6842c05 100644
--- a/scalarint.py
+++ b/scalarint.py
@@ -2,22 +2,26 @@
from __future__ import print_function, absolute_import, division, unicode_literals
+from .compat import no_limit_int # NOQA
+from ruamel.yaml.anchor import Anchor
+
if False: # MYPY
from typing import Text, Any, Dict, List # NOQA
__all__ = ['ScalarInt', 'BinaryInt', 'OctalInt', 'HexInt', 'HexCapsInt']
-from .compat import no_limit_int # NOQA
-
class ScalarInt(no_limit_int):
def __new__(cls, *args, **kw):
# type: (Any, Any, Any) -> Any
width = kw.pop('width', None) # type: ignore
underscore = kw.pop('underscore', None) # type: ignore
+ anchor = kw.pop('anchor', None) # type: ignore
v = no_limit_int.__new__(cls, *args, **kw) # type: ignore
v._width = width
v._underscore = underscore
+ if anchor is not None:
+ v.yaml_set_anchor(anchor, always_dump=True)
return v
def __iadd__(self, a): # type: ignore
@@ -65,17 +69,37 @@ class ScalarInt(no_limit_int):
) # NOQA
return x
+ @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, any=False):
+ # type: (bool) -> Any
+ if not hasattr(self, Anchor.attrib):
+ return None
+ 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
+ self.anchor.value = value
+ self.anchor.always_dump = always_dump
+
class BinaryInt(ScalarInt):
- def __new__(cls, value, width=None, underscore=None):
- # type: (Any, Any, Any) -> Any
- return ScalarInt.__new__(cls, value, width=width, underscore=underscore)
+ def __new__(cls, value, width=None, underscore=None, anchor=None):
+ # type: (Any, Any, Any, Any) -> Any
+ return ScalarInt.__new__(cls, value, width=width, underscore=underscore, anchor=anchor)
class OctalInt(ScalarInt):
- def __new__(cls, value, width=None, underscore=None):
- # type: (Any, Any, Any) -> Any
- return ScalarInt.__new__(cls, value, width=width, underscore=underscore)
+ def __new__(cls, value, width=None, underscore=None, anchor=None):
+ # type: (Any, Any, Any, Any) -> Any
+ return ScalarInt.__new__(cls, value, width=width, underscore=underscore, anchor=anchor)
# mixed casing of A-F is not supported, when loading the first non digit
@@ -85,14 +109,22 @@ class OctalInt(ScalarInt):
class HexInt(ScalarInt):
"""uses lower case (a-f)"""
- def __new__(cls, value, width=None, underscore=None):
- # type: (Any, Any, Any) -> Any
- return ScalarInt.__new__(cls, value, width=width, underscore=underscore)
+ def __new__(cls, value, width=None, underscore=None, anchor=None):
+ # type: (Any, Any, Any, Any) -> Any
+ return ScalarInt.__new__(cls, value, width=width, underscore=underscore, anchor=anchor)
class HexCapsInt(ScalarInt):
"""uses upper case (A-F)"""
- def __new__(cls, value, width=None, underscore=None):
- # type: (Any, Any, Any) -> Any
- return ScalarInt.__new__(cls, value, width=width, underscore=underscore)
+ def __new__(cls, value, width=None, underscore=None, anchor=None):
+ # type: (Any, Any, Any, Any) -> Any
+ return ScalarInt.__new__(cls, value, width=width, underscore=underscore, anchor=anchor)
+
+
+class DecimalInt(ScalarInt):
+ """needed if anchor"""
+
+ def __new__(cls, value, width=None, underscore=None, anchor=None):
+ # type: (Any, Any, Any, Any) -> Any
+ return ScalarInt.__new__(cls, value, width=width, underscore=underscore, anchor=anchor)