scalarint.py 4.4 KB

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