Passed
Push — development/test ( 7ab62a...6c4337 )
by Daniel
01:22
created

CommandLineArgumentsManagement.__init__()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
"""
2
CommandLineArgumentManagement - library to manage input parameters from command line
3
4
This library allows handling pre-configured arguments to be received from command line and use them
5
to call the main package functions
6
"""
7
# package to handle arguments from command line
8
import argparse
9
# package to add support for multi-language (i18n)
10
import gettext
11
# package to facilitate operating system operations
12
import os
13
14
15
class CommandLineArgumentsManagement:
16
    lcl = None
17
18
    def __init__(self, default_language='en_US'):
19
        current_script = os.path.basename(__file__).replace('.py', '')
20
        lang_folder = os.path.join(os.path.dirname(__file__), current_script + '_Locale')
21
        self.lcl = gettext.translation(current_script, lang_folder, languages=[default_language])
22
23
    def listing_parameter_values(self, local_logger, timmer, title, configuration_details,
24
                                 given_parameter_values):
25
        timmer.start()
26
        local_logger.info('='*50)
27
        local_logger.info(self.lcl.gettext('{application_name} has started') \
28
                          .replace('{application_name}', title))
29
        local_logger.info('~' * 50)
30
        local_logger.info(self.lcl.gettext('Overview of input parameter given values'))
31
        local_logger.info('~' * 50)
32
        parameter_values_dictionary = given_parameter_values.__dict__
33
        for input_key, attributes in configuration_details.items():
34
            # checking first if short key was provided, otherwise consider longer
35
            if input_key in parameter_values_dictionary:
36
                key_value_to_consider = input_key
37
            else:
38
                key_value_to_consider = attributes['option_long'].replace('-', '_')
39
            # having the key consider we determine the value of the current parameter
40
            value_to_consider = parameter_values_dictionary[key_value_to_consider]
41
            # we build the parameter feedback considering "option_description"
42
            # and replacing %s with parameter value
43
            feedback = attributes['option_description'] % value_to_consider
44
            # we finally write the feedback to logger
45
            local_logger.info(feedback)
46
        local_logger.info('~' * 50)
47
        timmer.stop()
48
49
    def parse_arguments(self, configuration_details):
50
        parser = argparse.ArgumentParser()
51
        for input_key, attributes in configuration_details.items():
52
            action_value = self.translate_default_to_action(attributes['default_value'])
53
            if action_value is None:
54
                parser.add_argument('-' + input_key, '--' + attributes['option_long'],
55
                                    required=attributes['option_required'],
56
                                    default=attributes['default_value'],
57
                                    help=attributes['option_sample_value'])
58
            else:
59
                parser.add_argument('-' + input_key, '--' + attributes['option_long'],
60
                                    required=attributes['option_required'],
61
                                    default=attributes['default_value'],
62
                                    action=action_value)
63
        parser.add_argument('-v', '--verbose', required=False, default=False, action='store_true')
64
        return parser.parse_args()
65
66
    @staticmethod
67
    def translate_default_to_action(given_default_value):
68
        if given_default_value is True:
69
            return 'store_true'
70
        elif given_default_value is False:
71
            return 'store_false'
72
        else:
73
            return None
74