import textwrap from typing import Union from .common import LogLevel class Colours: NORMAL = '\033[0m' RED = '\033[91m' GREEN = '\033[92m' YELLOW = '\033[93m' BLUE = '\033[94m' def loglevel_as_str(level: Union[LogLevel, int]) -> str: ## perform a lookup of each attribute of LogLevel and its associate ## value, and search for the attribute that has the matching level ## value passed to the function. _attrs = [_dir for _dir in dir(LogLevel) \ if not _dir.startswith('__')] # list of all attributes of LogLevel for _ in _attrs: # iterate over all attributes defined if getattr(LogLevel, _) == level: # if the level matches return "{0: <5}".format(_) # return the attribute name return " " def logAsCol(level: Union[int, LogLevel]) -> Colours: ## perform a colour lookup based on level number if level == LogLevel.DEBUG: return Colours.BLUE elif level == LogLevel.WARN: return Colours.YELLOW elif level == LogLevel.ERROR: return Colours.RED elif level == LogLevel.PASS: return Colours.GREEN else: return Colours.NORMAL def wrap(value: str, *, width: int = 120, tb_size: int = 4) -> str: return "\n".join(textwrap.wrap(value, width=width, tabsize=tb_size))