You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
catcli/catcli/logger.py

72 lines
1.8 KiB
Python

7 years ago
"""
author: deadc0de6 (https://github.com/deadc0de6)
Copyright (c) 2017, deadc0de6
Logging helper
"""
import sys
1 year ago
from typing import TypeVar, Type
7 years ago
2 years ago
# local imports
2 years ago
from catcli.colors import Colors
2 years ago
from catcli.utils import fix_badchars
7 years ago
1 year ago
CLASSTYPE = TypeVar('CLASSTYPE', bound='Logger')
7 years ago
class Logger:
2 years ago
"""log to stdout/stderr"""
7 years ago
2 years ago
@classmethod
1 year ago
def stdout_nocolor(cls: Type[CLASSTYPE],
string: str) -> None:
2 years ago
"""to stdout no color"""
2 years ago
string = fix_badchars(string)
sys.stdout.write(f'{string}\n')
7 years ago
2 years ago
@classmethod
1 year ago
def stderr_nocolor(cls: Type[CLASSTYPE],
string: str) -> None:
2 years ago
"""to stderr no color"""
2 years ago
string = fix_badchars(string)
sys.stderr.write(f'{string}\n')
2 years ago
@classmethod
1 year ago
def debug(cls: Type[CLASSTYPE],
string: str) -> None:
2 years ago
"""to stderr no color"""
1 year ago
cls.stderr_nocolor(f'[DBG] {string}')
7 years ago
2 years ago
@classmethod
1 year ago
def info(cls: Type[CLASSTYPE],
string: str) -> None:
2 years ago
"""to stdout in color"""
2 years ago
string = fix_badchars(string)
2 years ago
out = f'{Colors.MAGENTA}{string}{Colors.RESET}'
2 years ago
sys.stdout.write(f'{out}\n')
7 years ago
2 years ago
@classmethod
1 year ago
def err(cls: Type[CLASSTYPE],
string: str) -> None:
2 years ago
"""to stderr in RED"""
2 years ago
string = fix_badchars(string)
2 years ago
out = f'{Colors.RED}{string}{Colors.RESET}'
2 years ago
sys.stderr.write(f'{out}\n')
7 years ago
2 years ago
@classmethod
1 year ago
def progr(cls: Type[CLASSTYPE],
string: str) -> None:
2 years ago
"""print progress"""
2 years ago
string = fix_badchars(string)
sys.stderr.write(f'{string}\r')
7 years ago
sys.stderr.flush()
2 years ago
@classmethod
1 year ago
def get_bold_text(cls: Type[CLASSTYPE],
string: str) -> str:
2 years ago
"""make it bold"""
2 years ago
string = fix_badchars(string)
2 years ago
return f'{Colors.BOLD}{string}{Colors.RESET}'