Completed
Pull Request — master (#2409)
by
unknown
01:55
created

run_coala()   F

Complexity

Conditions 9

Size

Total Lines 99

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
dl 0
loc 99
rs 3.1556
c 0
b 0
f 0

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