|
11 | 11 | } |
12 | 12 |
|
13 | 13 |
|
| 14 | +class Verbosity: |
| 15 | + """ |
| 16 | + # Verbosity |
| 17 | +
|
| 18 | + Can be instantiated with and compared against a number or an enum. |
| 19 | + """ |
| 20 | + |
| 21 | + level_number: _Verbosity_Num |
| 22 | + level_string: _Verbosity_Enum |
| 23 | + |
| 24 | + def __init__(self, level: VerbosityLevel) -> None: |
| 25 | + """ |
| 26 | + Initializes the verbosity object. |
| 27 | +
|
| 28 | + Parameters |
| 29 | + ---------- |
| 30 | + :param level: The level of verbosity. Can be a number or an enum. |
| 31 | +
|
| 32 | + Raises |
| 33 | + ------ |
| 34 | + ValueError: If the level is not a valid number or enum. |
| 35 | + ValueError: If the level is not of a valid type. |
| 36 | + """ |
| 37 | + if isinstance(level, str): |
| 38 | + if level not in ['quiet', 'normal', 'verbose']: |
| 39 | + raise ValueError('Invalid verbosity level') |
| 40 | + self.level_string = level |
| 41 | + self.level_number = VerbosityMapping[level] |
| 42 | + elif isinstance(level, int): |
| 43 | + if level not in [0, 1, 2]: |
| 44 | + raise ValueError('Invalid verbosity level') |
| 45 | + self.level_number = level |
| 46 | + self.level_string = list(VerbosityMapping.keys())[level] |
| 47 | + else: |
| 48 | + raise ValueError(f'{type(level)} is not a valid verbosity level') |
| 49 | + |
| 50 | + def __str__(self) -> str: |
| 51 | + return self.level_string |
| 52 | + |
| 53 | + def __eq__(self, other: Any) -> bool: |
| 54 | + try: |
| 55 | + return self._compare(other, lambda a, b: a == b) |
| 56 | + except ValueError: |
| 57 | + return False |
| 58 | + |
| 59 | + def __lt__(self, other: Any) -> bool: |
| 60 | + return self._compare(other, lambda a, b: a < b) |
| 61 | + |
| 62 | + def __le__(self, other: Any) -> bool: |
| 63 | + return self._compare(other, lambda a, b: a <= b) |
| 64 | + |
| 65 | + def __gt__(self, other: Any) -> bool: |
| 66 | + return self._compare(other, lambda a, b: a > b) |
| 67 | + |
| 68 | + def __ge__(self, other: Any) -> bool: |
| 69 | + return self._compare(other, lambda a, b: a >= b) |
| 70 | + |
| 71 | + def __ne__(self, other: Any) -> bool: |
| 72 | + return not self.__eq__(other) |
| 73 | + |
| 74 | + def _compare( |
| 75 | + self, other: Any, operator: Callable[[VerbosityLevel, Any], bool] |
| 76 | + ) -> bool: |
| 77 | + if isinstance(other, Verbosity): |
| 78 | + return operator(self.level_number, other.level_number) |
| 79 | + if isinstance(other, int): |
| 80 | + return operator(self.level_number, other) |
| 81 | + if isinstance(other, str): |
| 82 | + if Verbosity.is_valid_level(other): |
| 83 | + return operator(self.level_number, VerbosityMapping[other]) |
| 84 | + return operator(self.level_string, other) |
| 85 | + raise ValueError('Cannot compare Verbosity with other types') |
| 86 | + |
| 87 | + @staticmethod |
| 88 | + def is_valid_level(level: Any) -> bool: |
| 89 | + if isinstance(level, int): |
| 90 | + return level in VerbosityMapping.values() |
| 91 | + if isinstance(level, str): |
| 92 | + return level in VerbosityMapping.keys() |
| 93 | + return False |
| 94 | + |
| 95 | + |
14 | 96 | class Settings: |
15 | 97 | """ |
16 | 98 | # Settings |
17 | 99 | `Non Instantiable` |
18 | 100 | """ |
19 | 101 |
|
20 | | - VERBOSITY: bool = True |
| 102 | + # Verbosity |
| 103 | + VerbosityLevel = VerbosityLevel |
| 104 | + VERBOSITY: Verbosity = Verbosity(1) |
| 105 | + |
| 106 | + # Graceful Exit |
21 | 107 | GRACEFUL_EXIT_ENABLED: bool = True |
22 | 108 |
|
23 | 109 | def __init__(self): |
|
0 commit comments