Completed
Pull Request — master (#2539)
by
unknown
02:17
created

run_coala()   F

Complexity

Conditions 9

Size

Total Lines 103

Duplication

Lines 0
Ratio 0 %

Importance

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