db_extractor.CommandLineArgumentsManagement   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A CommandLineArgumentsManagement.__init__() 0 8 1
A CommandLineArgumentsManagement.parse_arguments() 0 16 3
A CommandLineArgumentsManagement.listing_parameter_values() 0 25 3
A CommandLineArgumentsManagement.translate_default_to_action() 0 8 3
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
        file_parts = os.path.normpath(os.path.abspath(__file__)).replace('\\', os.path.altsep)\
20
            .split(os.path.altsep)
21
        locale_domain = file_parts[(len(file_parts)-1)].replace('.py', '')
22
        locale_folder = os.path.normpath(os.path.join(
23
            os.path.join(os.path.altsep.join(file_parts[:-2]), 'project_locale'), locale_domain))
24
        self.locale = gettext.translation(locale_domain, localedir=locale_folder,
25
                                          languages=[in_language], fallback=True)
26
27
    def listing_parameter_values(self, in_logger, timer, title, in_config, given_parameter_values):
28
        timer.start()
29
        in_logger.info('=' * 50)
30
        in_logger.info(self.locale.gettext('{application_name} has started')
31
                       .replace('{application_name}', title))
32
        in_logger.info('~' * 50)
33
        in_logger.info(self.locale.gettext('Overview of input parameter given values'))
34
        in_logger.info('~' * 50)
35
        parameter_values_dictionary = given_parameter_values.__dict__
36
        for input_key, attributes in in_config.items():
37
            # checking first if short key was provided, otherwise consider longer
38
            if input_key in parameter_values_dictionary:
39
                key_value_to_consider = input_key
40
            else:
41
                key_value_to_consider = attributes['option_long'].replace('-', '_')
42
            # having the key consider we determine the value of the current parameter
43
            value_to_consider = parameter_values_dictionary[key_value_to_consider]
44
            # we build the parameter feedback considering "option_description"
45
            # and replacing %s with parameter value
46
            feedback = self.locale.gettext(attributes['option_description']) \
47
                .replace('%s', str(value_to_consider))
48
            # we finally write the feedback to logger
49
            in_logger.info(feedback)
50
        in_logger.info('~' * 50)
51
        timer.stop()
52
53
    def parse_arguments(self, configuration_details):
54
        parser = argparse.ArgumentParser()
55
        for input_key, attributes in configuration_details.items():
56
            action_value = self.translate_default_to_action(attributes['default_value'])
57
            if action_value is None:
58
                parser.add_argument('-' + input_key, '--' + attributes['option_long'],
59
                                    required=attributes['option_required'],
60
                                    default=attributes['default_value'],
61
                                    help=attributes['option_sample_value'])
62
            else:
63
                parser.add_argument('-' + input_key, '--' + attributes['option_long'],
64
                                    required=attributes['option_required'],
65
                                    default=attributes['default_value'],
66
                                    action=action_value)
67
        parser.add_argument('-v', '--verbose', required=False, default=False, action='store_true')
68
        return parser.parse_args()
69
70
    @staticmethod
71
    def translate_default_to_action(given_default_value):
72
        if given_default_value is True:
73
            return 'store_true'
74
        elif given_default_value is False:
75
            return 'store_false'
76
        else:
77
            return None
78