codekingpro/portable-devtools
114k
1"""2You cannot subclass bool, and this is necessary for round-tripping anchored3bool values (and also if you want to preserve the original way of writing)4 5bool.__bases__ is type 'int', so that is what is used as the basis for ScalarBoolean as well.6 7You can use these in an if statement, but not when testing equivalence8"""9 10from __future__ import annotations11 12from ruamel.yaml.anchor import Anchor13 14if False: # MYPY15 from typing import Text, Any, Dict, List # NOQA16 17__all__ = ['ScalarBoolean']18 19 20class ScalarBoolean(int):21 def __new__(cls: Any, *args: Any, **kw: Any) -> Any:22 anchor = kw.pop('anchor', None)23 b = int.__new__(cls, *args, **kw)24 if anchor is not None:25 b.yaml_set_anchor(anchor, always_dump=True)26 return b27 28 @property29 def anchor(self) -> Any:30 if not hasattr(self, Anchor.attrib):31 setattr(self, Anchor.attrib, Anchor())32 return getattr(self, Anchor.attrib)33 34 def yaml_anchor(self, any: bool = False) -> Any:35 if not hasattr(self, Anchor.attrib):36 return None37 if any or self.anchor.always_dump:38 return self.anchor39 return None40 41 def yaml_set_anchor(self, value: Any, always_dump: bool = False) -> None:42 self.anchor.value = value43 self.anchor.always_dump = always_dump44 