1
|
|
|
"""
|
2
|
|
|
BasicNeeds - useful functions library
|
3
|
|
|
|
4
|
|
|
This library has functions useful to keep main logic short and simple
|
5
|
|
|
"""
|
6
|
|
|
# package to handle date and times
|
7
|
|
|
from datetime import datetime, timedelta
|
8
|
|
|
# package to add support for multi-language (i18n)
|
9
|
|
|
import gettext
|
10
|
|
|
# package to handle files/folders and related metadata/operations
|
11
|
|
|
import os
|
12
|
|
|
# package regular expressions
|
13
|
|
|
import re
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class BasicNeeds:
|
17
|
|
|
lcl = None
|
18
|
|
|
|
19
|
|
|
def __init__(self, default_language='en_US'):
|
20
|
|
|
current_script = os.path.basename(__file__).replace('.py', '')
|
21
|
|
|
lang_folder = os.path.join(os.path.dirname(__file__), current_script + '_Locale')
|
22
|
|
|
self.lcl = gettext.translation(current_script, lang_folder, languages=[default_language])
|
23
|
|
|
|
24
|
|
|
def fn_check_inputs(self, input_parameters):
|
25
|
|
|
if input_parameters.output_log_file is not None:
|
26
|
|
|
# checking log folder first as there's all further messages will be stored
|
27
|
|
|
self.fn_validate_single_value(os.path.dirname(input_parameters.output_log_file),
|
28
|
|
|
'folder', self.lcl.gettext('log file'))
|
29
|
|
|
|
30
|
|
|
def fn_evaluate_dict_values(self, in_dict):
|
31
|
|
|
true_counted = 0
|
32
|
|
|
for crt_value in in_dict:
|
33
|
|
|
if in_dict[crt_value]:
|
34
|
|
|
true_counted += 1
|
35
|
|
|
all_true = False
|
36
|
|
|
if true_counted == len(in_dict):
|
37
|
|
|
all_true = True
|
38
|
|
|
return all_true
|
39
|
|
|
|
40
|
|
|
def fn_evaluate_list_values(self, in_list):
|
41
|
|
|
true_counted = 0
|
42
|
|
|
for crt_value in in_list:
|
43
|
|
|
if crt_value:
|
44
|
|
|
true_counted += 1
|
45
|
|
|
all_true = False
|
46
|
|
|
if true_counted == len(in_list):
|
47
|
|
|
all_true = True
|
48
|
|
|
return all_true
|
49
|
|
|
|
50
|
|
|
def fn_final_message(self, local_logger, log_file_name, performance_in_seconds):
|
51
|
|
|
total_time_string = str(timedelta(seconds=performance_in_seconds))
|
52
|
|
|
if log_file_name == 'None':
|
53
|
|
|
self.fn_timestamped_print(self.lcl.gettext( \
|
54
|
|
|
'Application finished, whole script took {total_time_string}') \
|
55
|
|
|
.replace('{total_time_string}', total_time_string))
|
56
|
|
|
else:
|
57
|
|
|
local_logger.info(self.lcl.gettext('Total execution time was {total_time_string}') \
|
58
|
|
|
.replace('{total_time_string}', total_time_string))
|
59
|
|
|
self.fn_timestamped_print(self.lcl.gettext( \
|
60
|
|
|
'Application finished, for complete logged details please check {log_file_name}')
|
61
|
|
|
.replace('{log_file_name}', log_file_name))
|
62
|
|
|
|
63
|
|
|
@staticmethod
|
64
|
|
|
def fn_multi_line_string_to_single(input_string):
|
65
|
|
|
string_to_return = input_string.replace('\n', ' ').replace('\r', ' ')
|
66
|
|
|
return re.sub(r'\s{2,100}', ' ', string_to_return).replace(' , ', ', ').strip()
|
67
|
|
|
|
68
|
|
|
@staticmethod
|
69
|
|
|
def fn_numbers_with_leading_zero(input_number_as_string, digits):
|
70
|
|
|
final_number = input_number_as_string
|
71
|
|
|
if len(input_number_as_string) < digits:
|
72
|
|
|
final_number = '0' * (digits - len(input_number_as_string)) + input_number_as_string
|
73
|
|
|
return final_number
|
74
|
|
|
|
75
|
|
|
def fn_optional_print(self, boolean_variable, string_to_print):
|
76
|
|
|
if boolean_variable:
|
77
|
|
|
self.fn_timestamped_print(string_to_print)
|
78
|
|
|
|
79
|
|
|
@staticmethod
|
80
|
|
|
def fn_timestamped_print(string_to_print):
|
81
|
|
|
print(datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S.%f %Z") + '- ' + string_to_print)
|
82
|
|
|
|
83
|
|
|
def fn_validate_one_value(self, value_to_validate, validation_type, name_meaning):
|
84
|
|
|
is_invalid = False
|
85
|
|
|
message = ''
|
86
|
|
|
if validation_type == 'file':
|
87
|
|
|
is_invalid = (not os.path.isfile(value_to_validate))
|
88
|
|
|
message = self.lcl.gettext( \
|
89
|
|
|
'Given file "{value_to_validate}" does not exist') \
|
90
|
|
|
.replace('{value_to_validate}', value_to_validate)
|
91
|
|
|
elif validation_type == 'folder':
|
92
|
|
|
is_invalid = (not os.path.isdir(value_to_validate))
|
93
|
|
|
message = self.lcl.gettext( \
|
94
|
|
|
'Given folder "{value_to_validate}" does not exist') \
|
95
|
|
|
.replace('{value_to_validate}', value_to_validate)
|
96
|
|
|
elif validation_type == 'url':
|
97
|
|
|
url_reg_expression = 'https?://(?:www)?(?:[\\w-]{2,255}(?:\\.\\w{2,66}){1,2})'
|
98
|
|
|
is_invalid = (not re.match(url_reg_expression, value_to_validate))
|
99
|
|
|
message = self.lcl.gettext( \
|
100
|
|
|
'Given url "{value_to_validate}" is not valid') \
|
101
|
|
|
.replace('{value_to_validate}', value_to_validate)
|
102
|
|
|
return {
|
103
|
|
|
'is_invalid': is_invalid,
|
104
|
|
|
'message': message,
|
105
|
|
|
}
|
106
|
|
|
|
107
|
|
|
def fn_validate_single_value(self, value_to_validate, validation_type, name_meaning):
|
108
|
|
|
validation_details = self.fn_validate_one_value(value_to_validate, validation_type,
|
109
|
|
|
name_meaning)
|
110
|
|
|
if validation_details['is_invalid']:
|
111
|
|
|
self.fn_timestamped_print(validation_details['message'])
|
112
|
|
|
exit(1)
|
113
|
|
|
|