Completed
Pull Request — master (#2539)
by
unknown
01:51
created

run_coala()   F

Complexity

Conditions 9

Size

Total Lines 100

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
c 0
b 0
f 0
dl 0
loc 100
rs 3.1304

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 os
2
import platform
3
4
import pip
5
from pyprint.ConsolePrinter import ConsolePrinter
6
7
from coalib import VERSION
8
from coalib.misc.Exceptions import get_exitcode
9
from coalib.output.Interactions import fail_acquire_settings
10
from coalib.output.printers.LogPrinter import LogPrinter
11
from coalib.output.printers.LOG_LEVEL import LOG_LEVEL
12
from coalib.processes.Processing import execute_section, simplify_section_result
13
from coalib.settings.ConfigurationGathering import gather_configuration
14
from coalib.misc.Caching import FileCache
15
from coalib.misc.CachingUtilities import (
16
    settings_changed, update_settings_db, get_settings_hash)
17
18
do_nothing = lambda *args: True
19
20
21
def run_coala(log_printer=None,
22
              print_results=do_nothing,
23
              acquire_settings=fail_acquire_settings,
24
              print_section_beginning=do_nothing,
25
              nothing_done=do_nothing,
26
              autoapply=True,
27
              arg_parser=None):
28
    """
29
    This is a main method that should be usable for almost all purposes and
30
    reduces executing coala to one function call.
31
32
    :param log_printer:             A LogPrinter object to use for logging.
33
    :param print_results:           A callback that takes a LogPrinter, a
34
                                    section, a list of results to be printed,
35
                                    the file dict and the mutable file diff
36
                                    dict.
37
    :param acquire_settings:        The method to use for requesting settings.
38
                                    It will get a parameter which is a
39
                                    dictionary with the settings name as key
40
                                    and a list containing a description in [0]
41
                                    and the names of the bears who need this
42
                                    setting in all following indexes.
43
    :param print_section_beginning: A callback that will be called with a
44
                                    section name string whenever analysis of a
45
                                    new section is started.
46
    :param nothing_done:            A callback that will be called with only a
47
                                    log printer that shall indicate that
48
                                    nothing was done.
49
    :param autoapply:               Set to False to autoapply nothing by
50
                                    default; this is overridable via any
51
                                    configuration file/CLI.
52
    :return:                        A dictionary containing a list of results
53
                                    for all analyzed sections as key.
54
    """
55
    log_printer = log_printer or LogPrinter(ConsolePrinter(), LOG_LEVEL.DEBUG)
56
57
    exitcode = 0
58
    results = {}
59
    file_dicts = {}
60
    try:
61
        yielded_results = yielded_unfixed_results = False
62
        did_nothing = True
63
        sections, local_bears, global_bears, targets = gather_configuration(
64
            acquire_settings,
65
            log_printer,
66
            autoapply=autoapply,
67
            arg_parser=arg_parser)
68
69
        log_printer.debug("Platform {} -- Python {}, pip {}, coalib {}"
70
                          .format(platform.system(), platform.python_version(),
71
                                  pip.__version__, VERSION))
72
73
        config_file = os.path.abspath(str(sections["default"].get("config")))
74
75
        settings_hash = get_settings_hash(sections)
76
        flush_cache = bool(sections["default"].get("flush_cache", False) or
77
                           settings_changed(log_printer, settings_hash))
78
79
        disable_caching = bool(sections["default"].get(
80
            "disable_caching", False))
81
        cache = None
82
        if not sections["default"].get("disable_caching", False):
83
            cache = FileCache(log_printer, os.getcwd(), flush_cache)
84
85
        for section_name, section in sections.items():
86
            if not section.is_enabled(targets):
87
                continue
88
89
            print_section_beginning(section)
90
            section_result = execute_section(
91
                section=section,
92
                global_bear_list=global_bears[section_name],
93
                local_bear_list=local_bears[section_name],
94
                print_results=print_results,
95
                cache=cache,
96
                log_printer=log_printer)
97
            yielded, yielded_unfixed, results[section_name] = (
98
                simplify_section_result(section_result))
99
100
            yielded_results = yielded_results or yielded
101
            yielded_unfixed_results = (
102
                yielded_unfixed_results or yielded_unfixed)
103
            did_nothing = False
104
105
            file_dicts[section_name] = section_result[3]
106
107
        update_settings_db(log_printer, settings_hash)
108
        if cache:
109
            cache.write()
110
111
        if did_nothing:
112
            nothing_done(log_printer)
113
        elif yielded_unfixed_results:
114
            exitcode = 1
115
        elif yielded_results:
116
            exitcode = 5
117
    except BaseException as exception:  # pylint: disable=broad-except
118
        exitcode = exitcode or get_exitcode(exception, log_printer)
119
120
    return results, exitcode, file_dicts
121