Issues (70)

coalib/settings/SectionFilling.py (1 issue)

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