Test Failed
Branch development/test (a98bf9)
by Daniel
04:33
created

sources.tableau_hyper_management.ProjectNeeds   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 106
Duplicated Lines 23.58 %

Importance

Changes 0
Metric Value
wmc 9
eloc 73
dl 25
loc 106
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A ProjectNeeds.fn_check_inputs_specific() 0 11 3
A ProjectNeeds.listing_parameter_values() 25 25 3
A ProjectNeeds.initiate_logger_and_timer() 0 8 1
A ProjectNeeds.__init__() 0 16 1
A ProjectNeeds.load_configuration() 0 11 1

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
"""
2
Class Converter Specific Needs
3
4
Handling specific needs for Extractor script
5
"""
6
# useful methods to measure time performance by small pieces of code
7
from codetiming import Timer
8
# package to add support for multi-language (i18n)
9
import gettext
10
# package to facilitate operating system operations
11
import os
12
# package to facilitate common operations
13
from common.BasicNeeds import BasicNeeds
14
from common.CommandLineArgumentsManagement import CommandLineArgumentsManagement
15
from common.DataInputOutput import DataInputOutput
16
from common.FileOperations import FileOperations
17
from common.LoggingNeeds import LoggingNeeds
18
19
20
class ProjectNeeds:
21
    class_bn = None
22
    class_clam = None
23
    class_dio = None
24
    class_fo = None
25
    config = None
26
    locale = None
27
    parameters = None
28
    script = None
29
    timer = None
30
31
    def __init__(self, destination_script, default_language='en_US'):
32
        self.script = destination_script
33
        current_file_basename = os.path.basename(__file__).replace('.py', '')
34
        lang_folder = os.path.join(os.path.dirname(__file__), current_file_basename + '_Locale')
35
        self.locale = gettext.translation(current_file_basename, lang_folder,
36
                                          languages=[default_language])
37
        # instantiate Basic Needs class
38
        self.class_bn = BasicNeeds(default_language)
39
        # instantiate File Operations class
40
        self.class_fo = FileOperations(default_language)
41
        # instantiate Command Line Arguments class
42
        self.class_clam = CommandLineArgumentsManagement(default_language)
43
        # instantiate Data Manipulator class, useful to manipulate data frames
44
        self.class_dio = DataInputOutput(default_language)
45
        # instantiate Logger class
46
        self.class_ln = LoggingNeeds()
47
48
    def fn_check_inputs_specific(self, input_parameters):
49
        if self.script == 'converter':
50
            self.class_bn.fn_validate_single_value(
51
                    os.path.dirname(input_parameters.output_file), 'folder')
52
        if self.script == 'publisher':
53
            self.class_bn.fn_validate_single_value(
54
                    input_parameters.input_file, 'file')
55
            self.class_bn.fn_validate_single_value(
56
                    input_parameters.input_credentials_file, 'file')
57
            self.class_bn.fn_validate_single_value(
58
                    input_parameters.tableau_server, 'url')
59
60
    def initiate_logger_and_timer(self):
61
        # initiate logger
62
        self.class_ln.initiate_logger(self.parameters.output_log_file, self.script)
63
        # initiate localization specific for this script
64
        # define global timer to use
65
        self.timer = Timer(self.script,
66
                           text=self.locale.gettext('Time spent is {seconds}'),
67
                           logger=self.class_ln.logger.debug)
68
69
    def load_configuration(self):
70
        # load application configuration (inputs are defined into a json file)
71
        ref_folder = os.path.dirname(__file__).replace('tableau_hyper_management', 'config')
72
        config_file = os.path.join(ref_folder, 'tableau-hyper-management.json').replace('\\', '/')
73
        self.config = self.class_fo.fn_open_file_and_get_content(config_file)
74
        # get command line parameter values
75
        self.parameters = self.class_clam.parse_arguments(self.config['input_options'][self.script])
76
        # checking inputs, if anything is invalid an exit(1) will take place
77
        self.class_bn.fn_check_inputs(self.parameters)
78
        # checking inputs, if anything is invalid an exit(1) will take place
79
        self.fn_check_inputs_specific(self.parameters)
80
81 View Code Duplication
    def listing_parameter_values(self, in_logger, timer, title, in_config, given_parameter_values):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
82
        timer.start()
83
        in_logger.info('=' * 50)
84
        in_logger.info(self.locale.gettext('{application_name} has started')
85
                       .replace('{application_name}', title))
86
        in_logger.info('~' * 50)
87
        in_logger.info(self.locale.gettext('Overview of input parameter given values'))
88
        in_logger.info('~' * 50)
89
        parameter_values_dictionary = given_parameter_values.__dict__
90
        for input_key, attributes in in_config.items():
91
            # checking first if short key was provided, otherwise consider longer
92
            if input_key in parameter_values_dictionary:
93
                key_value_to_consider = input_key
94
            else:
95
                key_value_to_consider = attributes['option_long'].replace('-', '_')
96
            # having the key consider we determine the value of the current parameter
97
            value_to_consider = parameter_values_dictionary[key_value_to_consider]
98
            # we build the parameter feedback considering "option_description"
99
            # and replacing %s with parameter value
100
            feedback = self.locale.gettext(attributes['option_description']) \
101
                .replace('%s', value_to_consider)
102
            # we finally write the feedback to logger
103
            in_logger.info(feedback)
104
        in_logger.info('~' * 50)
105
        timer.stop()
106