Passed
Push — master ( b23eaf...087ae8 )
by Daniel
01:44
created

BasicNeeds.fn_get_file_content()   A

Complexity

Conditions 4

Size

Total Lines 17
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 17
rs 9.6
c 0
b 0
f 0
cc 4
nop 3
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, timezone, timedelta
9
import json
10
import os.path
11
12
13
class BasicNeeds:
14
    cfg_dtls = {}
15
16
    def fn_final_message(self, local_logger, timmer, log_file_name):
17
        total_time_string = str(timedelta(seconds = timmer.timers.total('thm')))
18
        if log_file_name == 'None':
19
            self.fn_timestamped_print(self, 'Application finished, whole script took '
20
                                      + total_time_string)
21
        else:
22
            local_logger.info(f'Total execution time was ' + total_time_string)
23
            self.fn_timestamped_print(self, 'Application finished, '
24
                                      + 'for complete logged details please check ' + log_file_name)
25
26
    def fn_get_file_content(self, in_file_handler, in_content_type):
27
        if in_content_type == 'json':
28
            try:
29
                json_interpreted_details = json.load(in_file_handler)
30
                self.fn_timestamped_print(self, 'I have interpreted JSON structure from given file')
31
                return json_interpreted_details
32
            except Exception as e:
33
                self.fn_timestamped_print(self, 'Error encountered when trying to interpret JSON')
34
                print(e)
35
        elif in_content_type == 'raw':
36
            raw_interpreted_file = in_file_handler.read()
37
            self.fn_timestamped_print(self, 'I have read file entire content')
38
            return raw_interpreted_file
39
        else:
40
            self.fn_timestamped_print(self, 'Unknown content type provided, '
41
                                      + 'expected either "json" or "raw" but got '
42
                                      + in_content_type)
43
44
    def fn_load_configuration(self):
45
        relevant_file = os.path.join(os.path.dirname(__file__), 'config.json')
46
        self.cfg_dtls = self.fn_open_file_and_get_its_content(self, relevant_file)
47
        # adding a special case data type
48
        self.cfg_dtls['data_types']['str'] = ''
49
50
    def fn_open_file_and_get_its_content(self, input_file, content_type = 'json'):
51
        if os.path.isfile(input_file):
52
            with open(input_file, 'r') as file_handler:
53
                self.fn_timestamped_print(self, 'I have opened file: ' + input_file)
54
                return self.fn_get_file_content(self, file_handler, content_type)
55
        else:
56
            self.fn_timestamped_print(self, 'Given file ' + input_file
57
                                      + ' does not exist, please check your inputs!')
58
59
    def fn_optional_print(self, boolean_variable, string_to_print):
60
        if boolean_variable:
61
            self.fn_timestamped_print(self, string_to_print)
62
63
    @staticmethod
64
    def fn_timestamped_print(self, string_to_print):
65
        print(datetime.utcnow().strftime("%Y-%b-%d %H:%M:%S.%f %Z")
66
              + ' - ' + string_to_print)
67