-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
93 lines (78 loc) · 2.23 KB
/
utils.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
87
88
89
90
91
92
93
"""
Common utitilies.
"""
from datetime import datetime
from sys import exit, stdout
def raise_error(message, cmd=[]):
"""
Prints the given error message and exits with a non-zero code.
@param message: error message
"""
print_coloured(f"[{get_time()}] ", 'white')
print_coloured('ERROR: ', 'red', 'bold')
if cmd:
print_coloured(f"{message}\n", 'red')
print_cmd(cmd)
print('')
else:
print_coloured(f"\n{message}\n\n", 'red')
usage()
exit(1)
def usage():
"""
Prints usage instructions.
"""
print_coloured('Usage:\n', 'white', 'bold')
print_coloured('$ ./wrapsync2.py ', 'white')
print_coloured('<push/pull> <dir_name/all> [rsync_options]\n', 'yellow', 'bold')
def print_cmd(cmd):
"""
Prints the given `rsync` command to the console.
@param cmd: rsync command in a list form
"""
print_coloured(f"{' '.join(cmd)}\n", 'grey')
def get_time():
"""
Returns current time.
@return: time in HH:MM:SS format
"""
return datetime.now().strftime('%H:%M:%S')
def print_coloured(text, colour, effect=''):
"""
Prints the given text in the given colour and effect.
@param text: message to print out
@param colour: display colour
@param effect: (optional) effect to use, such as 'bold' or 'underline'
"""
text_effect = get_text_effect(effect)
stdout.write(
f"{text_effect}{get_colour(colour)}{text}{get_text_effect('reset')}")
def get_colour(colour):
"""
Returns an ANSI escape sequence for the given colour.
@param colour: name of the colour
@return: escape sequence for the given colour
"""
sequence_base = '\033['
colours = {
'red': '31m',
'yellow': '33m',
'green': '32m',
'grey': '37m',
'white': '97m'
}
return f"{sequence_base}{colours[colour]}"
def get_text_effect(effect):
"""
Returns an ASCII escape sequence for a text effect, such as 'bold'.
@param effect: name of the effect
@return: escape sequence for the given effect
"""
sequence_base = '\033['
effects = {
'': '',
'reset': '0m',
'bold': '1m',
'underline': '4m'
}
return f"{sequence_base}{effects[effect]}"