Issues (70)

coalib/parsing/CliParsing.py (1 issue)

Labels
Severity
1
import os
0 ignored issues
show
There seems to be a cyclic import (coalib.collecting.Collectors -> coalib.settings.Section).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
2
from argparse import ArgumentParser
3
from collections import OrderedDict
4
5
from coalib.parsing.DefaultArgParser import default_arg_parser
6
from coalib.parsing.LineParser import LineParser
7
from coalib.settings.Section import Section, append_to_sections
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
    arg_parser = default_arg_parser() if arg_parser is None else arg_parser
38
    origin += os.path.sep
39
    sections = OrderedDict(default=Section('Default'))
40
    line_parser = LineParser(key_value_delimiters,
41
                             comment_seperators,
42
                             key_delimiters,
43
                             {},
44
                             section_override_delimiters)
45
46
    for arg_key, arg_value in sorted(
47
            vars(arg_parser.parse_args(arg_list)).items()):
48
        if arg_key == 'settings' and arg_value is not None:
49
            parse_custom_settings(sections,
50
                                  arg_value,
51
                                  origin,
52
                                  line_parser)
53
        else:
54
            if isinstance(arg_value, list):
55
                arg_value = ",".join([str(val) for val in arg_value])
56
57
            append_to_sections(sections,
58
                               arg_key,
59
                               arg_value,
60
                               origin,
61
                               from_cli=True)
62
63
    return sections
64
65
66
def parse_custom_settings(sections,
67
                          custom_settings_list,
68
                          origin,
69
                          line_parser):
70
    """
71
    Parses the custom settings given to coala via ``-S something=value``.
72
73
    :param sections:             The Section dictionary to add to (mutable).
74
    :param custom_settings_list: The list of settings strings.
75
    :param origin:               The originating directory.
76
    :param line_parser:          The LineParser to use.
77
    """
78
    for setting_definition in custom_settings_list:
79
        (_, key_tuples, value, _) = line_parser.parse(setting_definition)
80
        for key_tuple in key_tuples:
81
            append_to_sections(sections,
82
                               key=key_tuple[1],
83
                               value=value,
84
                               origin=origin,
85
                               section_name=key_tuple[0],
86
                               from_cli=True)
87
88
89
def check_conflicts(sections):
90
    """
91
    Checks if there are any conflicting arguments passed.
92
93
    :param sections:    The ``{section_name: section_object}`` dictionary to
94
                        check conflicts for.
95
    :return:            True if no conflicts occur.
96
    :raises SystemExit: If there are conflicting arguments (exit code: 2)
97
    """
98
    for section in sections.values():
99
        if (
100
                section.get('no_config', False) and
101
                (section.get('save', False) or
102
                 section.get('find_config', False))):
103
            ArgumentParser().error(
104
                "'no_config' cannot be set together 'save' or 'find_config'.")
105
106
    return True
107