Completed
Pull Request — master (#2594)
by
unknown
02:15
created

fill_settings()   C

Complexity

Conditions 7

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
c 2
b 0
f 0
dl 0
loc 56
rs 6.7294

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import copy
2
from itertools import chain
3
4
from coalib.bears.BEAR_KIND import BEAR_KIND
5
from coalib.collecting.Collectors import collect_bears
6
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...
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
    :param sections:         The sections to fill up, modified in place.
16
    :param acquire_settings: The method to use for requesting settings. It will
17
                             get a parameter which is a dictionary with the
18
                             settings name as key and a list containing a
19
                             description in [0] and the names of the bears
20
                             who need this setting in all following indexes.
21
    :param log_printer:      The log printer to use for logging.
22
    :return:                 A tuple containing (local_bears, global_bears),
23
                             each of them being a dictionary with the section
24
                             name as key and as value the bears as a list.
25
    """
26
    local_bears = {}
27
    global_bears = {}
28
29
    bears_by_section = {}
30
    bear_dirs_by_section = {}
31
32
    for section_name, section in sections.items():
33
        bear_dirs_by_section[section_name] = section.bear_dirs()
34
        bears_by_section[section_name] = list(section.get("bears", ""))
35
36
    bears_to_collect = set(chain.from_iterable(bears_by_section.values()))
37
    bear_dirs_to_collect = set(chain(*bear_dirs_by_section.values()))
38
39
    all_local_bears, all_global_bears = collect_bears(
40
        bear_dirs_to_collect,
41
        bears_to_collect,
42
        [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL],
43
        log_printer)
44
    all_bears = copy.deepcopy(all_local_bears)
45
    all_bears.extend(all_global_bears)
46
47
    for section_name, section in sections.items():
48
        section_bears = []
49
        section_local_bears = []
50
        section_global_bears = []
51
52
        for bear in all_bears:
53
            if bear.name in bears_by_section[section_name]:
54
                section_bears.append(bear)
55
56
                if bear.kind() == BEAR_KIND.LOCAL:
57
                    section_local_bears.append(bear)
58
                elif bear.kind() == BEAR_KIND.GLOBAL:
59
                    section_global_bears.append(bear)
60
        fill_section(section, acquire_settings, log_printer, section_bears)
61
62
        local_bears[section_name] = section_local_bears
63
        global_bears[section_name] = section_global_bears
64
65
    return local_bears, global_bears
66
67
68
def fill_section(section, acquire_settings, log_printer, bears):
69
    """
70
    Retrieves needed settings from given bears and asks the user for
71
    missing values.
72
73
    If a setting is requested by several bears, the help text from the
74
    latest bear will be taken.
75
76
    :param section:          A section containing available settings. Settings
77
                             will be added if some are missing.
78
    :param acquire_settings: The method to use for requesting settings. It will
79
                             get a parameter which is a dictionary with the
80
                             settings name as key and a list containing a
81
                             description in [0] and the names of the bears
82
                             who need this setting in all following indexes.
83
    :param log_printer:      The log printer for logging.
84
    :param bears:            All bear classes or instances.
85
    :return:                 The new section.
86
    """
87
    # Retrieve needed settings.
88
    prel_needed_settings = {}
89
    for bear in bears:
90
        needed = bear.get_non_optional_settings()
91
        for key in needed:
92
            if key in prel_needed_settings:
93
                prel_needed_settings[key].append(bear.name)
94
            else:
95
                prel_needed_settings[key] = [needed[key][0],
96
                                             bear.name]
97
98
    # Strip away existent settings.
99
    needed_settings = {}
100
    for setting, help_text in prel_needed_settings.items():
101
        if not setting in section:
102
            needed_settings[setting] = help_text
103
104
    # Get missing ones.
105
    if len(needed_settings) > 0:
106
        new_vals = acquire_settings(log_printer, needed_settings)
107
        for setting, help_text in new_vals.items():
108
            section.append(Setting(setting, help_text))
109
110
    return section
111