1
|
|
|
""" |
2
|
|
|
BasicNeeds - useful functions library |
3
|
|
|
|
4
|
|
|
This library has functions useful to keep main logic short and simple |
5
|
|
|
""" |
6
|
|
|
|
7
|
|
|
# standard Python packages |
8
|
|
|
from datetime import datetime, timedelta |
9
|
|
|
import json |
10
|
|
|
import os.path |
11
|
|
|
import re |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class BasicNeeds: |
15
|
|
|
cfg_dtls = {} |
16
|
|
|
|
17
|
|
|
def fn_check_inputs(self, input_parameters, input_script): |
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), 'folder', 'log file') |
20
|
|
|
# checking input file as the main point of whole logic revolves around it |
21
|
|
|
self.fn_validate_single_value(input_parameters.input_file, 'file', 'input file') |
22
|
|
|
# checking script specific inputs |
23
|
|
|
if input_script == 'converter': |
24
|
|
|
self.fn_validate_single_value(os.path.dirname(input_parameters.output_file), 'folder', 'output file') |
25
|
|
|
elif input_script == 'publish_data_source': |
26
|
|
|
self.fn_validate_single_value(input_parameters.input_credentials_file, 'file', 'credentials file') |
27
|
|
|
self.fn_validate_single_value(input_parameters.tableau_server, 'url', 'Tableau Server URL') |
28
|
|
|
|
29
|
|
|
def fn_final_message(self, local_logger, log_file_name, performance_in_seconds): |
30
|
|
|
total_time_string = str(timedelta(seconds=performance_in_seconds)) |
31
|
|
|
if log_file_name == 'None': |
32
|
|
|
self.fn_timestamped_print('Application finished, whole script took ' + total_time_string) |
33
|
|
|
else: |
34
|
|
|
local_logger.info(f'Total execution time was ' + total_time_string) |
35
|
|
|
self.fn_timestamped_print('Application finished, for complete logged details please check ' + log_file_name) |
36
|
|
|
|
37
|
|
|
def fn_get_file_content(self, in_file_handler, in_content_type): |
38
|
|
|
if in_content_type == 'json': |
39
|
|
|
try: |
40
|
|
|
json_interpreted_details = json.load(in_file_handler) |
41
|
|
|
self.fn_timestamped_print('I have interpreted JSON structure from given file') |
42
|
|
|
return json_interpreted_details |
43
|
|
|
except Exception as e: |
44
|
|
|
self.fn_timestamped_print('Error encountered when trying to interpret JSON') |
45
|
|
|
print(e) |
46
|
|
|
elif in_content_type == 'raw': |
47
|
|
|
raw_interpreted_file = in_file_handler.read() |
48
|
|
|
self.fn_timestamped_print('I have read file entire content') |
49
|
|
|
return raw_interpreted_file |
50
|
|
|
else: |
51
|
|
|
self.fn_timestamped_print('Unknown content type provided, ' |
52
|
|
|
+ 'expected either "json" or "raw" but got ' |
53
|
|
|
+ in_content_type) |
54
|
|
|
|
55
|
|
|
def fn_load_configuration(self): |
56
|
|
|
relevant_file = os.path.join(os.path.dirname(__file__), 'config.json') |
57
|
|
|
self.cfg_dtls = self.fn_open_file_and_get_its_content(relevant_file) |
58
|
|
|
# adding a special case data type |
59
|
|
|
self.cfg_dtls['data_types']['str'] = '' |
60
|
|
|
|
61
|
|
|
def fn_open_file_and_get_its_content(self, input_file, content_type='json'): |
62
|
|
|
if os.path.isfile(input_file): |
63
|
|
|
with open(input_file, 'r') as file_handler: |
64
|
|
|
self.fn_timestamped_print('I have opened file: ' + input_file) |
65
|
|
|
return self.fn_get_file_content(file_handler, content_type) |
66
|
|
|
else: |
67
|
|
|
self.fn_timestamped_print('Given file ' + input_file |
68
|
|
|
+ ' does not exist, please check your inputs!') |
69
|
|
|
|
70
|
|
|
def fn_optional_print(self, boolean_variable, string_to_print): |
71
|
|
|
if boolean_variable: |
72
|
|
|
self.fn_timestamped_print(string_to_print) |
73
|
|
|
|
74
|
|
|
@staticmethod |
75
|
|
|
def fn_timestamped_print(string_to_print): |
76
|
|
|
print(datetime.utcnow().strftime("%Y-%b-%d %H:%M:%S.%f %Z") |
77
|
|
|
+ ' - ' + string_to_print) |
78
|
|
|
|
79
|
|
|
def fn_validate_single_value(self, value_to_validate, validation_type, name_meaning): |
80
|
|
|
is_fatal_error = False |
81
|
|
|
if validation_type == 'file': |
82
|
|
|
is_fatal_error = (not os.path.isfile(value_to_validate)) |
83
|
|
|
message = 'Given ' + name_meaning + ' "' + value_to_validate \ |
84
|
|
|
+ '" does not exist, please check your inputs!' |
85
|
|
|
elif validation_type == 'folder': |
86
|
|
|
is_fatal_error = (not os.path.isdir(value_to_validate)) |
87
|
|
|
message = 'Given ' + name_meaning + ' "' + value_to_validate \ |
88
|
|
|
+ '" does not exist, please check your inputs!' |
89
|
|
|
elif validation_type == 'url': |
90
|
|
|
is_fatal_error = (not re.match('https?://(?:www)?(?:[\w-]{2,255}(?:\.\w{2,66}){1,2})', value_to_validate)) |
91
|
|
|
message = 'Given ' + name_meaning + ' "' + value_to_validate \ |
92
|
|
|
+ '" does not seem a valid one, please check your inputs!' |
93
|
|
|
if is_fatal_error: |
94
|
|
|
self.fn_timestamped_print(message) |
|
|
|
|
95
|
|
|
exit(1) |
96
|
|
|
|