Failed Conditions
Pull Request — master (#1371)
by Abdeali
01:38
created

coalib.parsing.parse_cli()   C

Complexity

Conditions 7

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 7
dl 0
loc 57
rs 6.6397

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
from collections import OrderedDict
2
import os
3
import sys
4
5
from coalib.parsing.LineParser import LineParser
6
from coalib.settings.Section import Section, append_to_sections
7
from coalib.parsing.DefaultArgParser import default_arg_parser
8
9
10
def parse_cli(arg_list=None,
11
              origin=os.getcwd(),
12
              arg_parser=None,
13
              key_value_delimiters=('=', ':'),
14
              comment_seperators=(),
15
              key_delimiters=(',',),
16
              section_override_delimiters=(".",)):
17
    """
18
    Parses the CLI arguments and creates sections out of it.
19
20
    :param arg_list:                    The CLI argument list.
21
    :param origin:                      Directory used to interpret relative
22
                                        paths given as argument.
23
    :param arg_parser:                  Instance of ArgParser that is used to
24
                                        parse none-setting arguments.
25
    :param key_value_delimiters:        Delimiters to separate key and value
26
                                        in setting arguments.
27
    :param comment_seperators:          Allowed prefixes for comments.
28
    :param key_delimiters:              Delimiter to separate multiple keys of
29
                                        a setting argument.
30
    :param section_override_delimiters: The delimiter to delimit the section
31
                                        from the key name (e.g. the '.' in
32
                                        sect.key = value).
33
    :return:                            A dictionary holding section names
34
                                        as keys and the sections themselves
35
                                        as value.
36
    """
37
    # Note: arg_list can also be []. Hence we cannot use
38
    # `arg_list = arg_list or default_list`
39
    arg_list = sys.argv[1:] if arg_list is None else arg_list
40
    arg_parser = arg_parser or default_arg_parser()
41
    origin += os.path.sep
42
    sections = OrderedDict(default=Section('Default'))
43
    line_parser = LineParser(key_value_delimiters,
44
                             comment_seperators,
45
                             key_delimiters,
46
                             {},
47
                             section_override_delimiters)
48
49
    for arg_key, arg_value in sorted(
50
            vars(arg_parser.parse_args(arg_list)).items()):
51
        if arg_key == 'settings' and arg_value is not None:
52
            parse_custom_settings(sections,
53
                                  arg_value,
54
                                  origin,
55
                                  line_parser)
56
        else:
57
            if isinstance(arg_value, list):
58
                arg_value = ",".join([str(val) for val in arg_value])
59
60
            append_to_sections(sections,
61
                               arg_key,
62
                               arg_value,
63
                               origin,
64
                               from_cli=True)
65
66
    return sections
67
68
69
def parse_custom_settings(sections,
70
                          custom_settings_list,
71
                          origin,
72
                          line_parser):
73
    """
74
    Parses the custom settings given to coala via `-S something=value`.
75
76
    :param sections:             The Section dictionary to add to (mutable).
77
    :param custom_settings_list: The list of settings strings.
78
    :param origin:               The originating directory.
79
    :param line_parser:          The LineParser to use.
80
    """
81
    for setting_definition in custom_settings_list:
82
        (dummy,
83
         key_touples,
84
         value,
85
         dummy) = line_parser.parse(setting_definition)
86
        for key_touple in key_touples:
87
            append_to_sections(sections,
88
                               key=key_touple[1],
89
                               value=value,
90
                               origin=origin,
91
                               section_name=key_touple[0],
92
                               from_cli=True)
93