1 | import os |
||
2 | import sys |
||
3 | from argparse import ArgumentParser |
||
4 | from collections import OrderedDict |
||
5 | |||
6 | from coalib.parsing.DefaultArgParser import default_arg_parser |
||
7 | from coalib.parsing.LineParser import LineParser |
||
8 | from coalib.settings.Section import Section, append_to_sections |
||
9 | |||
10 | |||
11 | def parse_cli(arg_list=None, |
||
12 | origin=os.getcwd(), |
||
13 | arg_parser=None, |
||
14 | key_value_delimiters=('=', ':'), |
||
15 | comment_seperators=(), |
||
16 | key_delimiters=(',',), |
||
17 | section_override_delimiters=(".",)): |
||
18 | """ |
||
19 | Parses the CLI arguments and creates sections out of it. |
||
20 | |||
21 | :param arg_list: The CLI argument list. |
||
22 | :param origin: Directory used to interpret relative |
||
23 | paths given as argument. |
||
24 | :param arg_parser: Instance of ArgParser that is used to |
||
25 | parse none-setting arguments. |
||
26 | :param key_value_delimiters: Delimiters to separate key and value |
||
27 | in setting arguments. |
||
28 | :param comment_seperators: Allowed prefixes for comments. |
||
29 | :param key_delimiters: Delimiter to separate multiple keys of |
||
30 | a setting argument. |
||
31 | :param section_override_delimiters: The delimiter to delimit the section |
||
32 | from the key name (e.g. the '.' in |
||
33 | sect.key = value). |
||
34 | :return: A dictionary holding section names |
||
35 | as keys and the sections themselves |
||
36 | as value. |
||
37 | """ |
||
38 | # Note: arg_list can also be []. Hence we cannot use |
||
39 | # `arg_list = arg_list or default_list` |
||
40 | arg_list = sys.argv[1:] if arg_list is None else arg_list |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
41 | arg_parser = arg_parser or default_arg_parser() |
||
42 | origin += os.path.sep |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
43 | sections = OrderedDict(default=Section('Default')) |
||
44 | line_parser = LineParser(key_value_delimiters, |
||
45 | comment_seperators, |
||
46 | key_delimiters, |
||
47 | {}, |
||
48 | section_override_delimiters) |
||
49 | |||
50 | for arg_key, arg_value in sorted( |
||
51 | vars(arg_parser.parse_args(arg_list)).items()): |
||
52 | if arg_key == 'settings' and arg_value is not None: |
||
53 | parse_custom_settings(sections, |
||
54 | arg_value, |
||
55 | origin, |
||
56 | line_parser) |
||
57 | else: |
||
58 | if isinstance(arg_value, list): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
59 | arg_value = ",".join([str(val) for val in arg_value]) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
60 | |||
61 | append_to_sections(sections, |
||
62 | arg_key, |
||
63 | arg_value, |
||
64 | origin, |
||
65 | from_cli=True) |
||
66 | |||
67 | return sections |
||
68 | |||
69 | |||
70 | def parse_custom_settings(sections, |
||
71 | custom_settings_list, |
||
72 | origin, |
||
73 | line_parser): |
||
74 | """ |
||
75 | Parses the custom settings given to coala via ``-S something=value``. |
||
76 | |||
77 | :param sections: The Section dictionary to add to (mutable). |
||
78 | :param custom_settings_list: The list of settings strings. |
||
79 | :param origin: The originating directory. |
||
80 | :param line_parser: The LineParser to use. |
||
81 | """ |
||
82 | for setting_definition in custom_settings_list: |
||
83 | (_, key_touples, value, _) = line_parser.parse(setting_definition) |
||
84 | for key_touple in key_touples: |
||
85 | append_to_sections(sections, |
||
86 | key=key_touple[1], |
||
87 | value=value, |
||
88 | origin=origin, |
||
89 | section_name=key_touple[0], |
||
90 | from_cli=True) |
||
91 | |||
92 | |||
93 | def check_conflicts(sections): |
||
94 | ''' |
||
95 | Checks if there are any conflicting aruments passed |
||
96 | |||
97 | :return: True if no conflicts |
||
98 | :raises SystemExit: If there are conflicting arguments (exit code: 2) |
||
99 | ''' |
||
100 | conflicts = {'no_config': {'save', 'find_config'}} |
||
101 | conflicting_keys = conflicts.keys() |
||
102 | |||
103 | for section in sections: |
||
104 | keys = set(sections[section]) |
||
105 | possible_conflicts = keys & conflicting_keys |
||
106 | for key in possible_conflicts: |
||
107 | intersection = keys & conflicts[key] |
||
108 | if len(intersection) > 0: |
||
109 | ArgumentParser().exit(2, |
||
110 | key + " cannot be given at the same " |
||
111 | "time with " + ', '.join(intersection)) |
||
112 | return True |
||
113 |