Completed
Push — master ( 545613...55df2f )
by Daniel
14s queued 11s
created

CommandLineArgumentsManagement.translate_default_to_action()   A

Complexity

Conditions 3

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nop 1
dl 0
loc 8
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
    locale = None
17
18
    def __init__(self, in_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.locale = gettext.translation(current_script, lang_folder, languages=[in_language])
22
23
    def listing_parameter_values(self, in_logger, timer, title, in_config, given_parameter_values):
24
        timer.start()
25
        in_logger.info('=' * 50)
26
        in_logger.info(self.locale.gettext('{application_name} has started')
27
                       .replace('{application_name}', title))
28
        in_logger.info('~' * 50)
29
        in_logger.info(self.locale.gettext('Overview of input parameter given values'))
30
        in_logger.info('~' * 50)
31
        parameter_values_dictionary = given_parameter_values.__dict__
32
        for input_key, attributes in in_config.items():
33
            # checking first if short key was provided, otherwise consider longer
34
            if input_key in parameter_values_dictionary:
35
                key_value_to_consider = input_key
36
            else:
37
                key_value_to_consider = attributes['option_long'].replace('-', '_')
38
            # having the key consider we determine the value of the current parameter
39
            value_to_consider = parameter_values_dictionary[key_value_to_consider]
40
            # we build the parameter feedback considering "option_description"
41
            # and replacing %s with parameter value
42
            feedback = self.locale.gettext(attributes['option_description']) \
43
                .replace('%s', str(value_to_consider))
44
            # we finally write the feedback to logger
45
            in_logger.info(feedback)
46
        in_logger.info('~' * 50)
47
        timer.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