Test Failed
Push — master ( 206750...bcacd1 )
by Daniel
02:10
created

tableau_hyper_management.CommandLineArgumentsHandling   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 110
Duplicated Lines 90 %

Importance

Changes 0
Metric Value
wmc 24
eloc 96
dl 99
loc 110
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A CommandLineArgumentsHandling.fn_build_short_options() 9 9 4
A CommandLineArgumentsHandling.fn_load_configuration() 3 3 2
A CommandLineArgumentsHandling.fn_build_long_options() 8 8 3
A CommandLineArgumentsHandling.fn_build_combined_options() 16 16 5
B CommandLineArgumentsHandling.fn_command_line_argument_interpretation() 40 40 8
A CommandLineArgumentsHandling.fn_assess_option() 15 15 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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