Completed
Pull Request — master (#2463)
by Lasse
02:16
created

fill_section()   D

Complexity

Conditions 8

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
dl 0
loc 43
rs 4
c 0
b 0
f 0
1
import copy
2
3
from coalib.bears.BEAR_KIND import BEAR_KIND
4
from coalib.collecting.Collectors import collect_bears
5
from coalib.output.printers.LOG_LEVEL import LOG_LEVEL
0 ignored issues
show
Unused Code introduced by
Unused LOG_LEVEL imported from coalib.output.printers.LOG_LEVEL
Loading history...
6
from coalib.settings.Setting import Setting
7
8
9
def fill_settings(sections, acquire_settings, log_printer):
10
    """
11
    Retrieves all bears and requests missing settings via the given
12
    acquire_settings method.
13
14
    :param sections:         The sections to fill up, modified in place.
15
    :param acquire_settings: The method to use for requesting settings. It will
16
                             get a parameter which is a dictionary with the
17
                             settings name as key and a list containing a
18
                             description in [0] and the names of the bears
19
                             who need this setting in all following indexes.
20
    :param log_printer:      The log printer to use for logging.
21
    :return:                 A tuple containing (local_bears, global_bears),
22
                             each of them being a dictionary with the section
23
                             name as key and as value the bears as a list.
24
    """
25
    local_bears = {}
26
    global_bears = {}
27
28
    for section_name, section in sections.items():
29
        bear_dirs = section.bear_dirs()
30
        bears = list(section.get("bears", ""))
31
        section_local_bears, section_global_bears = collect_bears(
32
            bear_dirs,
33
            bears,
34
            [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL],
35
            log_printer)
36
        all_bears = copy.deepcopy(section_local_bears)
37
        all_bears.extend(section_global_bears)
38
        fill_section(section, acquire_settings, log_printer, all_bears)
39
40
        local_bears[section_name] = section_local_bears
41
        global_bears[section_name] = section_global_bears
42
43
    return local_bears, global_bears
44
45
46
def fill_section(section, acquire_settings, log_printer, bears):
47
    """
48
    Retrieves needed settings from given bears and asks the user for
49
    missing values.
50
51
    If a setting is requested by several bears, the help text from the
52
    latest bear will be taken.
53
54
    :param section:          A section containing available settings. Settings
55
                             will be added if some are missing.
56
    :param acquire_settings: The method to use for requesting settings. It will
57
                             get a parameter which is a dictionary with the
58
                             settings name as key and a list containing a
59
                             description in [0] and the names of the bears
60
                             who need this setting in all following indexes.
61
    :param log_printer:      The log printer for logging.
62
    :param bears:            All bear classes or instances.
63
    :return:                 The new section.
64
    """
65
    # Retrieve needed settings.
66
    prel_needed_settings = {}
67
    for bear in bears:
68
        needed = bear.get_non_optional_settings()
69
        for key in needed:
70
            if key in prel_needed_settings:
71
                prel_needed_settings[key].append(bear.name)
72
            else:
73
                prel_needed_settings[key] = [needed[key][0],
74
                                             bear.name]
75
76
    # Strip away existent settings.
77
    needed_settings = {}
78
    for setting, help_text in prel_needed_settings.items():
79
        if not setting in section:
80
            needed_settings[setting] = help_text
81
82
    # Get missing ones.
83
    if len(needed_settings) > 0:
84
        new_vals = acquire_settings(log_printer, needed_settings)
85
        for setting, help_text in new_vals.items():
86
            section.append(Setting(setting, help_text))
87
88
    return section
89