clog/utils/printfmt.py
theonepath 5e922c5515
Added clog package modules
This commit is a continuation from commit [3746b773f4] which is a
separation of library modules into its own package from a larger
project.

Version control jump is significant in feature implementations due
simply to under-using commit control after minor feature
implementations. Project will receive better commit messages going
forward on changes to the package.
2022-05-03 14:51:57 +01:00

41 lines
1.3 KiB
Python

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))