| 1 |
# coding: utf-8 |
| 2 |
|
| 3 |
from __future__ import print_function, absolute_import, division, unicode_literals |
| 4 |
|
| 5 |
""" |
| 6 |
You cannot subclass bool, and this is necessary for round-tripping anchored |
| 7 |
bool values (and also if you want to preserve the original way of writing) |
| 8 |
|
| 9 |
bool.__bases__ is type 'int', so that is what is used as the basis for ScalarBoolean as well. |
| 10 |
|
| 11 |
You can use these in an if statement, but not when testing equivalence |
| 12 |
""" |
| 13 |
|
| 14 |
from strictyaml.ruamel.anchor import Anchor |
| 15 |
|
| 16 |
if False: # MYPY |
| 17 |
from typing import Text, Any, Dict, List # NOQA |
| 18 |
|
| 19 |
__all__ = ["ScalarBoolean"] |
| 20 |
|
| 21 |
# no need for no_limit_int -> int |
| 22 |
|
| 23 |
|
| 24 |
class ScalarBoolean(int): |
| 25 |
def __new__(cls, *args, **kw): |
| 26 |
# type: (Any, Any, Any) -> Any |
| 27 |
anchor = kw.pop("anchor", None) # type: ignore |
| 28 |
b = int.__new__(cls, *args, **kw) # type: ignore |
| 29 |
if anchor is not None: |
| 30 |
b.yaml_set_anchor(anchor, always_dump=True) |
| 31 |
return b |
| 32 |
|
| 33 |
@property |
| 34 |
def anchor(self): |
| 35 |
# type: () -> Any |
| 36 |
if not hasattr(self, Anchor.attrib): |
| 37 |
setattr(self, Anchor.attrib, Anchor()) |
| 38 |
return getattr(self, Anchor.attrib) |
| 39 |
|
| 40 |
def yaml_anchor(self, any=False): |
| 41 |
# type: (bool) -> Any |
| 42 |
if not hasattr(self, Anchor.attrib): |
| 43 |
return None |
| 44 |
if any or self.anchor.always_dump: |
| 45 |
return self.anchor |
| 46 |
return None |
| 47 |
|
| 48 |
def yaml_set_anchor(self, value, always_dump=False): |
| 49 |
# type: (Any, bool) -> None |
| 50 |
self.anchor.value = value |
| 51 |
self.anchor.always_dump = always_dump |