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 |
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
|
|
|
if not hasattr(bear, "get_non_optional_settings"): |
69
|
|
|
log_printer.log( |
70
|
|
|
LOG_LEVEL.WARNING, |
|
|
|
|
71
|
|
|
"One of the given bears ({}) has no attribute " |
72
|
|
|
"get_non_optional_settings.".format(str(bear))) |
73
|
|
|
else: |
74
|
|
|
needed = bear.get_non_optional_settings() |
75
|
|
|
for key in needed: |
76
|
|
|
if key in prel_needed_settings: |
77
|
|
|
prel_needed_settings[key].append(bear.name) |
78
|
|
|
else: |
79
|
|
|
prel_needed_settings[key] = [needed[key][0], |
80
|
|
|
bear.name] |
81
|
|
|
|
82
|
|
|
# Strip away existent settings. |
83
|
|
|
needed_settings = {} |
84
|
|
|
for setting, help_text in prel_needed_settings.items(): |
85
|
|
|
if not setting in section: |
86
|
|
|
needed_settings[setting] = help_text |
87
|
|
|
|
88
|
|
|
# Get missing ones. |
89
|
|
|
if len(needed_settings) > 0: |
90
|
|
|
new_vals = acquire_settings(log_printer, needed_settings) |
91
|
|
|
for setting, help_text in new_vals.items(): |
92
|
|
|
section.append(Setting(setting, help_text)) |
93
|
|
|
|
94
|
|
|
return section |
95
|
|
|
|