1
|
|
|
import getopt |
2
|
|
|
import json |
3
|
|
|
import os.path |
4
|
|
|
import pandas as pd |
5
|
|
|
import sys |
6
|
|
|
# argparse = Alternative command line option and argument parsing library. |
7
|
|
|
|
8
|
|
|
from . import TableauHyperApiExtraLogic as ClassTHAEL |
9
|
|
|
|
10
|
|
|
|
11
|
|
View Code Duplication |
class CommandLineArgumentsHandling: |
|
|
|
|
12
|
|
|
config_details = [] |
13
|
|
|
|
14
|
|
|
def fn_assess_option(self, current_option, established_default_value): |
15
|
|
|
if self.config_details['options'][current_option]['default_value'] == established_default_value: |
16
|
|
|
str_feedback_parts = { |
17
|
|
|
'prefix': 'Fatal Error', |
18
|
|
|
'verdict': 'Expected -' + current_option + '|--' |
19
|
|
|
+ self.config_details['options'][current_option]['option_long'] |
20
|
|
|
+ ' <' + self.config_details['options'][current_option]['option_sample_value'] |
21
|
|
|
+ '> but nothing of that sort has been seen...', |
22
|
|
|
'suffix': ':-(' |
23
|
|
|
} |
24
|
|
|
print(str_feedback_parts['prefix'] |
25
|
|
|
+ '_'*(len(str_feedback_parts['verdict']) - len(str_feedback_parts['prefix']))) |
26
|
|
|
print(str_feedback_parts['verdict'] + ' ' + str_feedback_parts['suffix']) |
27
|
|
|
sys.exit(2) |
28
|
|
|
print('Input file is "' + established_default_value + '"') |
29
|
|
|
|
30
|
|
|
def fn_build_combined_options(self): |
31
|
|
|
str_combined_options = '' |
32
|
|
|
for option_index, current_option in enumerate(self.config_details['options']): |
33
|
|
|
if current_option is not None: |
34
|
|
|
str_option_crt = '-' + current_option + '|--' \ |
35
|
|
|
+ self.config_details['options'][current_option]['option_long'] |
36
|
|
|
if 'option_sample_value' in self.config_details['options'][current_option]: |
37
|
|
|
str_option_crt += ' <' \ |
38
|
|
|
+ self.config_details['options'][current_option]['option_sample_value'] + '>' |
39
|
|
|
if self.config_details['options'][current_option]['option_type'] == 'optional': |
40
|
|
|
str_combined_options += ' [' + str_option_crt + '|if omitted default value will be considered: ' \ |
41
|
|
|
+ str(self.config_details['options'][current_option]['default_value']) \ |
42
|
|
|
+ ']' |
43
|
|
|
else: |
44
|
|
|
str_combined_options += ' ' + str_option_crt |
45
|
|
|
return str_combined_options |
46
|
|
|
|
47
|
|
|
def fn_build_long_options(self): |
48
|
|
|
str_long_options = [] |
49
|
|
|
for option_index, current_long_option in enumerate(self.config_details['options']): |
50
|
|
|
if current_long_option is not None: |
51
|
|
|
str_long_options.append(option_index) |
52
|
|
|
str_long_options[option_index] = self.config_details['options'][current_long_option]['option_long'] \ |
53
|
|
|
+ '=' |
54
|
|
|
return str_long_options |
55
|
|
|
|
56
|
|
|
def fn_build_short_options(self): |
57
|
|
|
str_short_options = 'h' |
58
|
|
|
for current_short_option in self.config_details['options']: |
59
|
|
|
if current_short_option is not None: |
60
|
|
|
if self.config_details['options'][current_short_option]['option_type'] == 'mandatory': |
61
|
|
|
str_short_options += current_short_option + ':' |
62
|
|
|
else: |
63
|
|
|
str_short_options += current_short_option |
64
|
|
|
return str_short_options |
65
|
|
|
|
66
|
|
|
def fn_command_line_argument_interpretation(self, argv): |
67
|
|
|
# https://www.programcreek.com/python/example/748/argparse.ArgumentParser |
68
|
|
|
print('#' * 120) |
69
|
|
|
input_file = '' |
70
|
|
|
csv_field_separator = ',' |
71
|
|
|
output_file = '' |
72
|
|
|
verbose = False |
73
|
|
|
self.fn_load_configuration(self) |
74
|
|
|
help_feedback = __file__ + self.fn_build_combined_options(self) |
75
|
|
|
try: |
76
|
|
|
opts, args = getopt.getopt(argv, self.fn_build_short_options(self), self.fn_build_long_options(self)) |
77
|
|
|
except getopt.GetoptError: |
78
|
|
|
print(help_feedback) |
79
|
|
|
sys.exit(2) |
80
|
|
|
for opt, arg in opts: |
81
|
|
|
if opt in ("-h", "--help"): |
82
|
|
|
print(help_feedback) |
83
|
|
|
sys.exit() |
84
|
|
|
elif opt in ("-i", "--input-file"): |
85
|
|
|
input_file = arg |
86
|
|
|
elif opt in ("-s", "--csv-field-separator"): |
87
|
|
|
csv_field_separator = arg |
88
|
|
|
elif opt in ("-o", "--output-file"): |
89
|
|
|
output_file = arg |
90
|
|
|
elif opt in ("-v", "--verbose"): |
91
|
|
|
verbose = True |
92
|
|
|
else: |
93
|
|
|
assert False, "Unhandled Option: " + arg |
94
|
|
|
self.fn_assess_option(self, 'i', input_file) |
95
|
|
|
print('CSV field separator is "' + csv_field_separator + '"') |
96
|
|
|
self.fn_assess_option(self, 'o', output_file) |
97
|
|
|
print('#' * 120) |
98
|
|
|
csv_content_df = pd.read_csv(filepath_or_buffer = input_file, |
99
|
|
|
delimiter = csv_field_separator, |
100
|
|
|
cache_dates = True, |
101
|
|
|
index_col = None, |
102
|
|
|
memory_map = True, |
103
|
|
|
encoding = 'utf-8') |
104
|
|
|
formats_to_evaluate = self.config_details['data_types'] |
105
|
|
|
ClassTHAEL.fn_run_hyper_creation(ClassTHAEL, csv_content_df, formats_to_evaluate, output_file, verbose) |
106
|
|
|
|
107
|
|
|
def fn_load_configuration(self): |
108
|
|
|
with open(os.path.dirname(__file__) + "/config.json", 'r') as json_file: |
109
|
|
|
self.config_details = json.load(json_file) |
110
|
|
|
|