Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

125 рядки
4.0 KiB

  1. from __future__ import annotations
  2. from ruamel.yaml.anchor import Anchor
  3. if False: # MYPY
  4. from typing import Text, Any, Dict, List # NOQA
  5. __all__ = ['ScalarInt', 'BinaryInt', 'OctalInt', 'HexInt', 'HexCapsInt', 'DecimalInt']
  6. class ScalarInt(int):
  7. def __new__(cls: Any, *args: Any, **kw: Any) -> Any:
  8. width = kw.pop('width', None)
  9. underscore = kw.pop('underscore', None)
  10. anchor = kw.pop('anchor', None)
  11. v = int.__new__(cls, *args, **kw)
  12. v._width = width
  13. v._underscore = underscore
  14. if anchor is not None:
  15. v.yaml_set_anchor(anchor, always_dump=True)
  16. return v
  17. def __iadd__(self, a: Any) -> Any: # type: ignore
  18. x = type(self)(self + a)
  19. x._width = self._width # type: ignore
  20. x._underscore = ( # type: ignore
  21. self._underscore[:] if self._underscore is not None else None # type: ignore
  22. ) # NOQA
  23. return x
  24. def __ifloordiv__(self, a: Any) -> Any: # type: ignore
  25. x = type(self)(self // a)
  26. x._width = self._width # type: ignore
  27. x._underscore = ( # type: ignore
  28. self._underscore[:] if self._underscore is not None else None # type: ignore
  29. ) # NOQA
  30. return x
  31. def __imul__(self, a: Any) -> Any: # type: ignore
  32. x = type(self)(self * a)
  33. x._width = self._width # type: ignore
  34. x._underscore = ( # type: ignore
  35. self._underscore[:] if self._underscore is not None else None # type: ignore
  36. ) # NOQA
  37. return x
  38. def __ipow__(self, a: Any) -> Any: # type: ignore
  39. x = type(self)(self ** a)
  40. x._width = self._width # type: ignore
  41. x._underscore = ( # type: ignore
  42. self._underscore[:] if self._underscore is not None else None # type: ignore
  43. ) # NOQA
  44. return x
  45. def __isub__(self, a: Any) -> Any: # type: ignore
  46. x = type(self)(self - a)
  47. x._width = self._width # type: ignore
  48. x._underscore = ( # type: ignore
  49. self._underscore[:] if self._underscore is not None else None # type: ignore
  50. ) # NOQA
  51. return x
  52. @property
  53. def anchor(self) -> Any:
  54. if not hasattr(self, Anchor.attrib):
  55. setattr(self, Anchor.attrib, Anchor())
  56. return getattr(self, Anchor.attrib)
  57. def yaml_anchor(self, any: bool = False) -> Any:
  58. if not hasattr(self, Anchor.attrib):
  59. return None
  60. if any or self.anchor.always_dump:
  61. return self.anchor
  62. return None
  63. def yaml_set_anchor(self, value: Any, always_dump: bool = False) -> None:
  64. self.anchor.value = value
  65. self.anchor.always_dump = always_dump
  66. class BinaryInt(ScalarInt):
  67. def __new__(
  68. cls, value: Any, width: Any = None, underscore: Any = None, anchor: Any = None,
  69. ) -> Any:
  70. return ScalarInt.__new__(cls, value, width=width, underscore=underscore, anchor=anchor)
  71. class OctalInt(ScalarInt):
  72. def __new__(
  73. cls, value: Any, width: Any = None, underscore: Any = None, anchor: Any = None,
  74. ) -> Any:
  75. return ScalarInt.__new__(cls, value, width=width, underscore=underscore, anchor=anchor)
  76. # mixed casing of A-F is not supported, when loading the first non digit
  77. # determines the case
  78. class HexInt(ScalarInt):
  79. """uses lower case (a-f)"""
  80. def __new__(
  81. cls, value: Any, width: Any = None, underscore: Any = None, anchor: Any = None,
  82. ) -> Any:
  83. return ScalarInt.__new__(cls, value, width=width, underscore=underscore, anchor=anchor)
  84. class HexCapsInt(ScalarInt):
  85. """uses upper case (A-F)"""
  86. def __new__(
  87. cls, value: Any, width: Any = None, underscore: Any = None, anchor: Any = None,
  88. ) -> Any:
  89. return ScalarInt.__new__(cls, value, width=width, underscore=underscore, anchor=anchor)
  90. class DecimalInt(ScalarInt):
  91. """needed if anchor"""
  92. def __new__(
  93. cls, value: Any, width: Any = None, underscore: Any = None, anchor: Any = None,
  94. ) -> Any:
  95. return ScalarInt.__new__(cls, value, width=width, underscore=underscore, anchor=anchor)