-
Notifications
You must be signed in to change notification settings - Fork 1
/
styledprint.py
55 lines (36 loc) · 1.25 KB
/
styledprint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from colorama import Fore
from enum import Enum
import io
import logging
class Verbosity(Enum):
Error = 0
Warning = 1
Debug = 2
def __ge__(self, other):
if (self.__class__ is other.__class__):
return self.value >= other.value
return NotImplemented
default_verbosity = Verbosity.Error
def set_verbosity(level):
global default_verbosity
default_verbosity = Verbosity(level)
def print_styled(verbosity, color, text, *args):
if (default_verbosity >= verbosity):
print(color + text + Fore.RESET, *args)
def to_print(text, *args):
with io.StringIO() as sio:
print(text, *args, file=sio)
return sio.getvalue()
def print_error(text, *args):
print_styled(Verbosity.Error, Fore.RED, text, *args)
logging.error(to_print(text, *args))
def print_info(text, *args):
print_styled(Verbosity.Warning, Fore.YELLOW, text, *args)
logging.info(to_print(text, *args))
def print_info_begin(text, *args):
print_info('******* {0} *******'.format(text), *args)
def print_info_end(text, *args):
print_info('******* {0} *******\n'.format(text), *args)
def print_debug(text, *args):
print_styled(Verbosity.Debug, Fore.CYAN, text, *args)
logging.debug(to_print(text, *args))