Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/bitstring/bitstring_options.py: 73%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

66 statements  

1from __future__ import annotations 

2 

3import bitstring 

4import os 

5 

6ConstBitStore = bitstring.bitstore.ConstBitStore 

7MutableBitStore = bitstring.bitstore.MutableBitStore 

8 

9class Options: 

10 """Internal class to create singleton module options instance.""" 

11 

12 _instance = None 

13 

14 def __init__(self): 

15 self.set_lsb0(False) 

16 self._bytealigned = False 

17 self.mxfp_overflow = 'saturate' 

18 

19 self.no_color = False 

20 no_color = os.getenv('NO_COLOR') 

21 self.no_color = True if no_color else False 

22 

23 @property 

24 def using_rust_core(self): 

25 x = ConstBitStore() 

26 return x.using_rust_core() 

27 

28 @property 

29 def mxfp_overflow(self) -> str: 

30 return self._mxfp_overflow 

31 

32 @mxfp_overflow.setter 

33 def mxfp_overflow(self, value: str) -> None: 

34 allowed_values = ('saturate', 'overflow') 

35 if value not in allowed_values: 

36 raise ValueError(f"mxfp_overflow must be one of {allowed_values}, not {value}.") 

37 self._mxfp_overflow = value 

38 

39 def __repr__(self) -> str: 

40 attributes = {attr: getattr(self, attr) for attr in dir(self) if not attr.startswith('_') and not callable(getattr(self, attr))} 

41 return '\n'.join(f"{attr}: {value!r}" for attr, value in attributes.items()) 

42 

43 @property 

44 def lsb0(self) -> bool: 

45 return self._lsb0 

46 

47 @lsb0.setter 

48 def lsb0(self, value: bool) -> None: 

49 self.set_lsb0(value) 

50 

51 def set_lsb0(self, value: bool) -> None: 

52 self._lsb0 = bool(value) 

53 Bits = bitstring.bits.Bits 

54 BitArray = bitstring.bitarray_.BitArray 

55 

56 lsb0_methods = { 

57 Bits: {'_find': Bits._find_lsb0, '_rfind': Bits._rfind_lsb0, '_findall': Bits._findall_lsb0}, 

58 BitArray: {'_ror': BitArray._rol_msb0, '_rol': BitArray._ror_msb0, '_append': BitArray._append_lsb0, 

59 '_prepend': BitArray._append_msb0}, 

60 ConstBitStore: {'__setitem__': None, '__delitem__': None, 'invert': None, 

61 'getindex': ConstBitStore.getindex_lsb0, 'getslice': ConstBitStore.getslice_lsb0, 

62 'getslice_withstep': ConstBitStore.getslice_withstep_lsb0}, 

63 MutableBitStore: {'__setitem__': MutableBitStore.setitem_lsb0, '__delitem__': MutableBitStore.delitem_lsb0, 

64 'getindex': MutableBitStore.getindex_lsb0, 'getslice': MutableBitStore.getslice_lsb0, 

65 'getslice_withstep': MutableBitStore.getslice_withstep_lsb0, 'invert': MutableBitStore.invert_lsb0} 

66 } 

67 msb0_methods = { 

68 Bits: {'_find': Bits._find_msb0, '_rfind': Bits._rfind_msb0, '_findall': Bits._findall_msb0}, 

69 BitArray: {'_ror': BitArray._ror_msb0, '_rol': BitArray._rol_msb0, '_append': BitArray._append_msb0, 

70 '_prepend': BitArray._append_lsb0}, 

71 ConstBitStore: {'__setitem__': None, '__delitem__': None, 'invert': None, 

72 'getindex': ConstBitStore.getindex_msb0, 'getslice': ConstBitStore.getslice_msb0, 

73 'getslice_withstep': ConstBitStore.getslice_withstep_msb0}, 

74 MutableBitStore: {'__setitem__': MutableBitStore.setitem_msb0, '__delitem__': MutableBitStore.delitem_msb0, 

75 'getindex': MutableBitStore.getindex_msb0, 'getslice': MutableBitStore.getslice_msb0, 

76 'getslice_withstep': MutableBitStore.getslice_withstep_msb0, 'invert': MutableBitStore.invert_msb0} 

77 } 

78 methods = lsb0_methods if self._lsb0 else msb0_methods 

79 for cls, method_dict in methods.items(): 

80 for attr, method in method_dict.items(): 

81 setattr(cls, attr, method) 

82 

83 @property 

84 def bytealigned(self) -> bool: 

85 return self._bytealigned 

86 

87 @bytealigned.setter 

88 def bytealigned(self, value: bool) -> None: 

89 self._bytealigned = bool(value) 

90 

91 def __new__(cls): 

92 if cls._instance is None: 

93 cls._instance = super(Options, cls).__new__(cls) 

94 return cls._instance 

95 

96 

97class Colour: 

98 def __new__(cls, use_colour: bool) -> Colour: 

99 x = super().__new__(cls) 

100 if use_colour: 

101 cls.blue = '\033[34m' 

102 cls.purple = '\033[35m' 

103 cls.green = '\033[32m' 

104 cls.off = '\033[0m' 

105 else: 

106 cls.blue = cls.purple = cls.green = cls.off = '' 

107 return x