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 handle files/folders and related metadata/operations
|
9
|
|
|
import os
|
10
|
|
|
# package regular expressions
|
11
|
|
|
import re
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class BasicNeeds:
|
15
|
|
|
|
16
|
|
|
def fn_check_inputs(self, input_parameters):
|
17
|
|
|
if input_parameters.output_log_file is not None:
|
18
|
|
|
# checking log folder first as there's all further messages will be stored
|
19
|
|
|
self.fn_validate_single_value(os.path.dirname(input_parameters.output_log_file),
|
20
|
|
|
'folder', 'log file')
|
21
|
|
|
|
22
|
|
|
def fn_final_message(self, local_logger, log_file_name, performance_in_seconds):
|
23
|
|
|
total_time_string = str(timedelta(seconds=performance_in_seconds))
|
24
|
|
|
if log_file_name == 'None':
|
25
|
|
|
self.fn_timestamped_print('Application finished, whole script took '
|
26
|
|
|
+ total_time_string)
|
27
|
|
|
else:
|
28
|
|
|
local_logger.info(f'Total execution time was ' + total_time_string)
|
29
|
|
|
self.fn_timestamped_print('Application finished, '
|
30
|
|
|
+ 'for complete logged details please check '
|
31
|
|
|
+ log_file_name)
|
32
|
|
|
|
33
|
|
|
@staticmethod
|
34
|
|
|
def fn_multi_line_string_to_single_line(input_string):
|
35
|
|
|
string_to_return = input_string.replace('\n', ' ').replace('\r', ' ')
|
36
|
|
|
return re.sub(r'\s{2,100}', ' ', string_to_return).replace(' , ', ', ').strip()
|
37
|
|
|
|
38
|
|
|
@staticmethod
|
39
|
|
|
def fn_numbers_with_leading_zero(input_number_as_string, digits):
|
40
|
|
|
final_number = input_number_as_string
|
41
|
|
|
if len(input_number_as_string) < digits:
|
42
|
|
|
final_number = '0' * (digits - len(input_number_as_string)) + input_number_as_string
|
43
|
|
|
return final_number
|
44
|
|
|
|
45
|
|
|
def fn_optional_print(self, boolean_variable, string_to_print):
|
46
|
|
|
if boolean_variable:
|
47
|
|
|
self.fn_timestamped_print(string_to_print)
|
48
|
|
|
|
49
|
|
|
@staticmethod
|
50
|
|
|
def fn_timestamped_print(string_to_print):
|
51
|
|
|
print(datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S.%f %Z") + ' - ' + string_to_print)
|
52
|
|
|
|
53
|
|
|
@staticmethod
|
54
|
|
|
def fn_validate_one_value(value_to_validate, validation_type, name_meaning):
|
55
|
|
|
is_fatal_error = False
|
56
|
|
|
message = ''
|
57
|
|
|
if validation_type == 'file':
|
58
|
|
|
is_fatal_error = (not os.path.isfile(value_to_validate))
|
59
|
|
|
message = 'Given ' + name_meaning + ' "' + value_to_validate \
|
60
|
|
|
+ '" does not exist, please check your inputs!'
|
61
|
|
|
elif validation_type == 'folder':
|
62
|
|
|
is_fatal_error = (not os.path.isdir(value_to_validate))
|
63
|
|
|
message = 'Given ' + name_meaning + ' "' + value_to_validate \
|
64
|
|
|
+ '" does not exist, please check your inputs!'
|
65
|
|
|
elif validation_type == 'url':
|
66
|
|
|
url_reg_expression = 'https?://(?:www)?(?:[\\w-]{2,255}(?:\\.\\w{2,66}){1,2})'
|
67
|
|
|
is_fatal_error = (not re.match(url_reg_expression, value_to_validate))
|
68
|
|
|
message = 'Given ' + name_meaning + ' "' + value_to_validate \
|
69
|
|
|
+ '" does not seem a valid one, please check your inputs!'
|
70
|
|
|
return {
|
71
|
|
|
'is_fatal_error': is_fatal_error,
|
72
|
|
|
'message': message,
|
73
|
|
|
}
|
74
|
|
|
|
75
|
|
|
def fn_validate_single_value(self, value_to_validate, validation_type, name_meaning):
|
76
|
|
|
validation_details = self.fn_validate_one_value(value_to_validate, validation_type,
|
77
|
|
|
name_meaning)
|
78
|
|
|
if validation_details['is_fatal_error']:
|
79
|
|
|
self.fn_timestamped_print(validation_details['message'])
|
80
|
|
|
exit(1)
|
81
|
|
|
|