| 
 | 1 | +from typing import Any, Callable, Literal, Union  | 
 | 2 | + | 
 | 3 | +_Verbosity_Num = Literal[0, 1, 2]  | 
 | 4 | +_Verbosity_Enum = Literal['quiet', 'normal', 'verbose']  | 
 | 5 | +VerbosityLevel = Union[_Verbosity_Num, _Verbosity_Enum]  | 
 | 6 | +VerbosityMapping: dict[_Verbosity_Enum, _Verbosity_Num] = {  | 
 | 7 | +    'quiet': 0,  | 
 | 8 | +    'normal': 1,  | 
 | 9 | +    'verbose': 2,  | 
 | 10 | +}  | 
 | 11 | + | 
 | 12 | + | 
 | 13 | +class Verbosity:  | 
 | 14 | +    """  | 
 | 15 | +    # Verbosity  | 
 | 16 | +
  | 
 | 17 | +    Can be instantiated with and compared against a number or an enum.  | 
 | 18 | +    """  | 
 | 19 | + | 
 | 20 | +    level_number: _Verbosity_Num  | 
 | 21 | +    level_string: _Verbosity_Enum  | 
 | 22 | + | 
 | 23 | +    def __init__(self, level: VerbosityLevel) -> None:  | 
 | 24 | +        """  | 
 | 25 | +        Initializes the verbosity object.  | 
 | 26 | +
  | 
 | 27 | +        Parameters  | 
 | 28 | +        ----------  | 
 | 29 | +        :param level: The level of verbosity. Can be a number or an enum.  | 
 | 30 | +
  | 
 | 31 | +        Raises  | 
 | 32 | +        ------  | 
 | 33 | +        ValueError: If the level is not a valid number or enum.  | 
 | 34 | +        ValueError: If the level is not of a valid type.  | 
 | 35 | +        """  | 
 | 36 | +        if isinstance(level, str):  | 
 | 37 | +            if level not in VerbosityMapping.keys():  | 
 | 38 | +                raise ValueError('Invalid verbosity level')  | 
 | 39 | +            self.level_string = level  | 
 | 40 | +            self.level_number = VerbosityMapping[level]  | 
 | 41 | +        elif isinstance(level, int):  | 
 | 42 | +            if level not in VerbosityMapping.values():  | 
 | 43 | +                raise ValueError('Invalid verbosity level')  | 
 | 44 | +            self.level_number = level  | 
 | 45 | +            self.level_string = list(VerbosityMapping.keys())[level]  | 
 | 46 | +        else:  | 
 | 47 | +            raise ValueError(f'{type(level)} is not a valid verbosity level')  | 
 | 48 | + | 
 | 49 | +    def __str__(self) -> str:  | 
 | 50 | +        return self.level_string  | 
 | 51 | + | 
 | 52 | +    def __eq__(self, other: Any) -> bool:  | 
 | 53 | +        try:  | 
 | 54 | +            return self._compare(other, lambda a, b: a == b)  | 
 | 55 | +        except ValueError:  | 
 | 56 | +            return False  | 
 | 57 | + | 
 | 58 | +    def __lt__(self, other: Any) -> bool:  | 
 | 59 | +        return self._compare(other, lambda a, b: a < b)  | 
 | 60 | + | 
 | 61 | +    def __le__(self, other: Any) -> bool:  | 
 | 62 | +        return self._compare(other, lambda a, b: a <= b)  | 
 | 63 | + | 
 | 64 | +    def __gt__(self, other: Any) -> bool:  | 
 | 65 | +        return self._compare(other, lambda a, b: a > b)  | 
 | 66 | + | 
 | 67 | +    def __ge__(self, other: Any) -> bool:  | 
 | 68 | +        return self._compare(other, lambda a, b: a >= b)  | 
 | 69 | + | 
 | 70 | +    def __ne__(self, other: Any) -> bool:  | 
 | 71 | +        return not self.__eq__(other)  | 
 | 72 | + | 
 | 73 | +    def _compare(  | 
 | 74 | +        self, other: Any, operator: Callable[[VerbosityLevel, Any], bool]  | 
 | 75 | +    ) -> bool:  | 
 | 76 | +        if isinstance(other, Verbosity):  | 
 | 77 | +            return operator(self.level_number, other.level_number)  | 
 | 78 | +        if isinstance(other, int):  | 
 | 79 | +            return operator(self.level_number, other)  | 
 | 80 | +        if isinstance(other, str):  | 
 | 81 | +            if Verbosity.is_valid_level(other):  | 
 | 82 | +                return operator(self.level_number, VerbosityMapping[other])  | 
 | 83 | +            return operator(self.level_string, other)  | 
 | 84 | +        raise ValueError('Cannot compare Verbosity with other types')  | 
 | 85 | + | 
 | 86 | +    @staticmethod  | 
 | 87 | +    def is_valid_level(level: Any) -> bool:  | 
 | 88 | +        """  | 
 | 89 | +        Determines whether the given level is a valid verbosity level.  | 
 | 90 | +
  | 
 | 91 | +        Parameters  | 
 | 92 | +        ----------  | 
 | 93 | +        :param level: The level to check.  | 
 | 94 | +
  | 
 | 95 | +        Returns  | 
 | 96 | +        -------  | 
 | 97 | +        :returns: True if the level is a valid verbosity level, False otherwise.  | 
 | 98 | +        """  | 
 | 99 | +        if isinstance(level, int):  | 
 | 100 | +            return level in VerbosityMapping.values()  | 
 | 101 | +        if isinstance(level, str):  | 
 | 102 | +            return level in VerbosityMapping.keys()  | 
 | 103 | +        return False  | 
 | 104 | + | 
 | 105 | + | 
1 | 106 | class Settings:  | 
2 | 107 |     """  | 
3 | 108 |     # Settings  | 
4 | 109 |     `Non Instantiable`  | 
5 | 110 |     """  | 
6 | 111 | 
 
  | 
 | 112 | +    # Verbosity  | 
 | 113 | +    VerbosityLevel = VerbosityLevel  | 
 | 114 | +    VERBOSITY: Verbosity = Verbosity(1)  | 
 | 115 | + | 
 | 116 | +    # Graceful Exit  | 
7 | 117 |     GRACEFUL_EXIT_ENABLED: bool = True  | 
8 | 118 | 
 
  | 
9 | 119 |     def __init__(self):  | 
10 | 120 |         raise NotImplementedError('This class is not instantiable')  | 
11 | 121 | 
 
  | 
12 | 122 |     @staticmethod  | 
13 |  | -    def set_graceful_exit(enabled: bool = True):  | 
 | 123 | +    def set_graceful_exit(enabled: bool = True) -> None:  | 
 | 124 | +        """  | 
 | 125 | +        Enables/Disables graceful exiting.  | 
 | 126 | +
  | 
 | 127 | +        Parameters  | 
 | 128 | +        ----------  | 
 | 129 | +        :param enabled: True to enable graceful exit, False otherwise.  | 
 | 130 | +
  | 
 | 131 | +        Returns  | 
 | 132 | +        -------  | 
 | 133 | +        :returns: None  | 
 | 134 | +        """  | 
14 | 135 |         Settings.GRACEFUL_EXIT_ENABLED = enabled  | 
 | 136 | + | 
 | 137 | +    @staticmethod  | 
 | 138 | +    def set_verbosity(verbosity: VerbosityLevel = 'normal') -> None:  | 
 | 139 | +        """  | 
 | 140 | +        Sets the verbosity level.  | 
 | 141 | +
  | 
 | 142 | +        Parameters  | 
 | 143 | +        ----------  | 
 | 144 | +        :param verbosity: The level of verbosity. Can be a number or an enum.  | 
 | 145 | +
  | 
 | 146 | +        Returns  | 
 | 147 | +        -------  | 
 | 148 | +        :returns: None  | 
 | 149 | +
  | 
 | 150 | +        Raises  | 
 | 151 | +        ------  | 
 | 152 | +        ValueError: If the level is not a valid number or enum.  | 
 | 153 | +        ValueError: If the level is not of a valid type.  | 
 | 154 | +        """  | 
 | 155 | +        Settings.VERBOSITY = Verbosity(verbosity)  | 
0 commit comments