|
1
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
|
|
2
|
|
|
# -*- coding: future_fstrings -*- |
|
3
|
|
|
|
|
4
|
|
|
from db_sync_tool.utility import log, mode, system |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
# |
|
8
|
|
|
# GLOBALS |
|
9
|
|
|
# |
|
10
|
|
|
|
|
11
|
|
|
class CliFormat: |
|
|
|
|
|
|
12
|
|
|
BEIGE = '\033[96m' |
|
13
|
|
|
PURPLE = '\033[95m' |
|
14
|
|
|
BLUE = '\033[94m' |
|
15
|
|
|
YELLOW = '\033[93m' |
|
16
|
|
|
GREEN = '\033[92m' |
|
17
|
|
|
RED = '\033[91m' |
|
18
|
|
|
BLACK = '\033[90m' |
|
19
|
|
|
ENDC = '\033[0m' |
|
20
|
|
|
BOLD = '\033[1m' |
|
21
|
|
|
UNDERLINE = '\033[4m' |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
class Subject: |
|
|
|
|
|
|
25
|
|
|
INFO = CliFormat.GREEN + '[INFO]' + CliFormat.ENDC |
|
26
|
|
|
LOCAL = CliFormat.BEIGE + '[LOCAL]' + CliFormat.ENDC |
|
27
|
|
|
TARGET = CliFormat.BLUE + '[TARGET]' + CliFormat.ENDC |
|
28
|
|
|
ORIGIN = CliFormat.PURPLE + '[ORIGIN]' + CliFormat.ENDC |
|
29
|
|
|
ERROR = CliFormat.RED + '[ERROR]' + CliFormat.ENDC |
|
30
|
|
|
WARNING = CliFormat.YELLOW + '[WARNING]' + CliFormat.ENDC |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
# |
|
34
|
|
|
# FUNCTIONS |
|
35
|
|
|
# |
|
36
|
|
|
|
|
37
|
|
|
def message(header, message, do_print=True, do_log=False, debug=False, verbose_only=False): |
|
|
|
|
|
|
38
|
|
|
""" |
|
39
|
|
|
Formatting a message for print or log |
|
40
|
|
|
:param header: String |
|
41
|
|
|
:param message: String |
|
42
|
|
|
:param do_print: Boolean |
|
43
|
|
|
:param do_log: Boolean |
|
44
|
|
|
:param debug: Boolean |
|
45
|
|
|
:param verbose_only: Boolean |
|
46
|
|
|
:return: String message |
|
47
|
|
|
""" |
|
48
|
|
|
# Logging if explicitly forced or verbose option is active |
|
49
|
|
|
if do_log or system.config['verbose']: |
|
50
|
|
|
_message = remove_multiple_elements_from_string([CliFormat.BEIGE, CliFormat.PURPLE, CliFormat.BLUE, CliFormat.YELLOW, CliFormat.GREEN, CliFormat.RED, CliFormat.BLACK, CliFormat.ENDC, CliFormat.BOLD, CliFormat.UNDERLINE], message) |
|
|
|
|
|
|
51
|
|
|
# @ToDo: Can this be done better? Dynamic functions? |
|
52
|
|
|
if debug: |
|
53
|
|
|
log.get_logger().debug(_message) |
|
54
|
|
|
elif header == Subject.WARNING: |
|
55
|
|
|
log.get_logger().warning(_message) |
|
56
|
|
|
elif header == Subject.ERROR: |
|
57
|
|
|
log.get_logger().error(_message) |
|
58
|
|
|
else: |
|
59
|
|
|
log.get_logger().info(_message) |
|
60
|
|
|
|
|
61
|
|
|
# Formatting message if mute option is inactive |
|
62
|
|
|
if (system.config['mute'] and header == Subject.ERROR) or (not system.config['mute']): |
|
63
|
|
|
if do_print: |
|
64
|
|
|
if not verbose_only or (verbose_only and system.config['verbose']): |
|
65
|
|
|
print(header + extend_output_by_sync_mode(header, debug) + ' ' + message) |
|
66
|
|
|
else: |
|
67
|
|
|
return header + extend_output_by_sync_mode(header, debug) + ' ' + message |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
def extend_output_by_sync_mode(header, debug=False): |
|
71
|
|
|
""" |
|
72
|
|
|
Extending the output by a client information (LOCAL|REMOTE) |
|
73
|
|
|
:param header: String |
|
74
|
|
|
:return: String message |
|
75
|
|
|
""" |
|
76
|
|
|
_sync_mode = mode.get_sync_mode() |
|
77
|
|
|
_debug = '' |
|
78
|
|
|
|
|
79
|
|
|
if debug: |
|
80
|
|
|
_debug = CliFormat.BLACK + '[DEBUG]' + CliFormat.ENDC |
|
81
|
|
|
|
|
82
|
|
|
if header == Subject.INFO or header == Subject.LOCAL or header == Subject.WARNING or header == Subject.ERROR: |
|
|
|
|
|
|
83
|
|
|
return '' |
|
84
|
|
|
else: |
|
85
|
|
|
if mode.is_remote(subject_to_host(header)): |
|
|
|
|
|
|
86
|
|
|
return CliFormat.BLACK + '[REMOTE]' + CliFormat.ENDC + _debug |
|
87
|
|
|
else: |
|
88
|
|
|
return CliFormat.BLACK + '[LOCAL]' + CliFormat.ENDC + _debug |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
def host_to_subject(host): |
|
|
|
|
|
|
92
|
|
|
""" |
|
93
|
|
|
Converting the client to the according subject |
|
94
|
|
|
:param host: String |
|
95
|
|
|
:return: String subject |
|
96
|
|
|
""" |
|
97
|
|
|
if host == mode.Client.ORIGIN: |
|
|
|
|
|
|
98
|
|
|
return Subject.ORIGIN |
|
99
|
|
|
elif host == mode.Client.TARGET: |
|
100
|
|
|
return Subject.TARGET |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
def subject_to_host(subject): |
|
|
|
|
|
|
104
|
|
|
""" |
|
105
|
|
|
Converting the subject to the according host |
|
106
|
|
|
:param subject: String |
|
107
|
|
|
:return: String host |
|
108
|
|
|
""" |
|
109
|
|
|
if subject == Subject.ORIGIN: |
|
|
|
|
|
|
110
|
|
|
return mode.Client.ORIGIN |
|
111
|
|
|
elif subject == Subject.TARGET: |
|
112
|
|
|
return mode.Client.TARGET |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
def remove_multiple_elements_from_string(elements, string): |
|
116
|
|
|
""" |
|
117
|
|
|
Removing multiple elements from a string |
|
118
|
|
|
:param elements: List |
|
119
|
|
|
:param string: String |
|
120
|
|
|
:return: String string |
|
121
|
|
|
""" |
|
122
|
|
|
for element in elements: |
|
123
|
|
|
if element in string: |
|
124
|
|
|
string = string.replace(element, '') |
|
125
|
|
|
return string |
|
126
|
|
|
|
|
|
|
|
|
|
127
|
|
|
|