This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessages.py
86 lines (76 loc) · 2.34 KB
/
messages.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- python-indent-offset: 4 -*-
'''
messages: utilities for printing messages and other similar tasks
'''
__version__ = '1.0.0'
__license__ = 'GPLv3'
import six
try:
from termcolor import colored
except:
pass
# Utility functions.
# .............................................................................
def update_progress(progress):
'''Value of "progress" should be a float from 0 to 1.'''
six.print_('\r[{0:10}] {1:.0f}%'.format('#' * int(progress * 10),
progress*100), end='', flush=True)
def msg(text, flags=None, colorize=True):
'''Like the standard print(), but flushes the output immediately and
colorizes the output by default. Flushing immediately is useful when
piping the output of a script, because Python by default will buffer the
output in that situation and this makes it very difficult to see what is
happening in real time.
'''
if colorize:
print(colorcode(text, flags), flush=True)
else:
print(text, flush=True)
def colorcode(text, flags=None, colorize=True):
(prefix, color, attributes) = color_codes(flags)
if colorize:
if attributes and color:
return colored(text, color, attrs=attributes)
elif color:
return colored(text, color)
elif attributes:
return colored(text, attrs=attributes)
else:
return text
elif prefix:
return prefix + ': ' + text
else:
return text
def color_codes(flags):
color = ''
prefix = ''
attrib = []
if type(flags) is not list:
flags = [flags]
if 'error' in flags:
prefix = 'ERROR'
color = 'red'
if 'warning' in flags:
prefix = 'WARNING'
color = 'yellow'
if 'info' in flags:
color = 'green'
if 'white' in flags:
color = 'white'
if 'blue' in flags:
color = 'blue'
if 'grey' in flags:
color = 'grey'
if 'cyan' in flags:
color = 'cyan'
if 'underline' in flags:
attrib.append('underline')
if 'bold' in flags:
attrib.append('bold')
if 'reverse' in flags:
attrib.append('reverse')
if 'dark' in flags:
attrib.append('dark')
return (prefix, color, attrib)