1from __future__ import annotations
2
3import bitstring
4import os
5
6class Options:
7 """Internal class to create singleton module options instance."""
8
9 _instance = None
10
11 def __init__(self):
12 self.set_lsb0(False)
13 self._bytealigned = False
14 self.mxfp_overflow = 'saturate'
15
16 self.no_color = False
17 no_color = os.getenv('NO_COLOR')
18 self.no_color = True if no_color else False
19
20 @property
21 def mxfp_overflow(self) -> str:
22 return self._mxfp_overflow
23
24 @mxfp_overflow.setter
25 def mxfp_overflow(self, value: str) -> None:
26 allowed_values = ('saturate', 'overflow')
27 if value not in allowed_values:
28 raise ValueError(f"mxfp_overflow must be one of {allowed_values}, not {value}.")
29 self._mxfp_overflow = value
30
31 def __repr__(self) -> str:
32 attributes = {attr: getattr(self, attr) for attr in dir(self) if not attr.startswith('_') and not callable(getattr(self, attr))}
33 return '\n'.join(f"{attr}: {value!r}" for attr, value in attributes.items())
34
35 @property
36 def lsb0(self) -> bool:
37 return self._lsb0
38
39 @lsb0.setter
40 def lsb0(self, value: bool) -> None:
41 self.set_lsb0(value)
42
43 def set_lsb0(self, value: bool) -> None:
44 self._lsb0 = bool(value)
45 Bits = bitstring.bits.Bits
46 BitArray = bitstring.bitarray_.BitArray
47 BitStore = bitstring.bitstore.BitStore
48
49 lsb0_methods = {
50 Bits: {'_find': Bits._find_lsb0, '_rfind': Bits._rfind_lsb0, '_findall': Bits._findall_lsb0},
51 BitArray: {'_ror': BitArray._rol_msb0, '_rol': BitArray._ror_msb0, '_append': BitArray._append_lsb0,
52 '_prepend': BitArray._append_msb0},
53 BitStore: {'__setitem__': BitStore.setitem_lsb0, '__delitem__': BitStore.delitem_lsb0,
54 'getindex': BitStore.getindex_lsb0, 'getslice': BitStore.getslice_lsb0,
55 'getslice_withstep': BitStore.getslice_withstep_lsb0, 'invert': BitStore.invert_lsb0}
56 }
57 msb0_methods = {
58 Bits: {'_find': Bits._find_msb0, '_rfind': Bits._rfind_msb0, '_findall': Bits._findall_msb0},
59 BitArray: {'_ror': BitArray._ror_msb0, '_rol': BitArray._rol_msb0, '_append': BitArray._append_msb0,
60 '_prepend': BitArray._append_lsb0},
61 BitStore: {'__setitem__': BitStore.setitem_msb0, '__delitem__': BitStore.delitem_msb0,
62 'getindex': BitStore.getindex_msb0, 'getslice': BitStore.getslice_msb0,
63 'getslice_withstep': BitStore.getslice_withstep_msb0, 'invert': BitStore.invert_msb0}
64 }
65 methods = lsb0_methods if self._lsb0 else msb0_methods
66 for cls, method_dict in methods.items():
67 for attr, method in method_dict.items():
68 setattr(cls, attr, method)
69
70 @property
71 def bytealigned(self) -> bool:
72 return self._bytealigned
73
74 @bytealigned.setter
75 def bytealigned(self, value: bool) -> None:
76 self._bytealigned = bool(value)
77
78 def __new__(cls):
79 if cls._instance is None:
80 cls._instance = super(Options, cls).__new__(cls)
81 return cls._instance
82
83
84class Colour:
85 def __new__(cls, use_colour: bool) -> Colour:
86 x = super().__new__(cls)
87 if use_colour:
88 cls.blue = '\033[34m'
89 cls.purple = '\033[35m'
90 cls.green = '\033[32m'
91 cls.off = '\033[0m'
92 else:
93 cls.blue = cls.purple = cls.green = cls.off = ''
94 return x