Failed Conditions
Pull Request — master (#1152)
by Lasse
03:36
created

coalib.collecting.load_configuration()   C

Complexity

Conditions 7

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 7
dl 0
loc 61
rs 6.3068

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
3
from coalib.collecting.Importers import iimport_objects
4
from coalib.misc.Decorators import yield_once
5
from coalib.output.printers.LOG_LEVEL import LOG_LEVEL
6
from coalib.parsing.Globbing import iglob, fnmatch
7
8
9
def _yield_if_right_kind(bear_class, kinds):
10
    try:
11
        if bear_class.kind() in kinds:
12
            yield bear_class
13
    except NotImplementedError:
14
        pass
15
16
17
def _import_bears(file_path, kinds):
18
    # recursive imports:
19
    for bear_list in iimport_objects(file_path,
20
                                     names='__additional_bears__',
21
                                     types=list):
22
        for bear_class in bear_list:
23
            for valid_bear_class in _yield_if_right_kind(bear_class, kinds):
24
                yield valid_bear_class
25
    # normal import
26
    for bear_class in iimport_objects(file_path,
27
                                      attributes='kind',
28
                                      local=True):
29
        for valid_bear_class in _yield_if_right_kind(bear_class, kinds):
30
            yield valid_bear_class
31
32
33
@yield_once
34
def icollect(file_paths):
35
    """
36
    Evaluate globs in file paths and return all matching files.
37
38
    :param file_paths:  file path or list of such that can include globs
39
    :return:            iterator that yields paths of all matching files
40
    """
41
    if isinstance(file_paths, str):
42
        file_paths = [file_paths]
43
44
    for file_path in file_paths:
45
        for match in iglob(file_path):
46
            yield match
47
48
49
def collect_files(file_paths, ignored_file_paths=None):
50
    """
51
    Evaluate globs in file paths and return all matching files
52
53
    :param file_paths:         file path or list of such that can include globs
54
    :param ignored_file_paths: list of globs that match to-be-ignored files
55
    :return:                   list of paths of all matching files
56
    """
57
    valid_files = list(filter(os.path.isfile, icollect(file_paths)))
58
    return remove_ignored(valid_files, ignored_file_paths or [])
59
60
61
def collect_dirs(dir_paths, ignored_dir_paths=None):
62
    """
63
    Evaluate globs in directory paths and return all matching directories
64
65
    :param dir_paths:         file path or list of such that can include globs
66
    :param ignored_dir_paths: list of globs that match to-be-ignored dirs
67
    :return:                  list of paths of all matching directories
68
    """
69
    valid_dirs = list(filter(os.path.isdir, icollect(dir_paths)))
70
    return remove_ignored(valid_dirs, ignored_dir_paths or [])
71
72
73
@yield_once
74
def icollect_bears(bear_dirs, bear_names, kinds, log_printer):
75
    """
76
    Collect all bears from bear directories that have a matching kind.
77
78
    :param bear_dirs:   directory name or list of such that can contain bears
79
    :param bear_names:  names of bears
80
    :param kinds:       list of bear kinds to be collected
81
    :param log_printer: log_printer to handle logging
82
    :return:            iterator that yields bear classes
83
    """
84
    for bear_dir in filter(os.path.isdir, icollect(bear_dirs)):
85
        for bear_name in bear_names:
86
            for matching_file in iglob(
87
                    os.path.join(bear_dir, bear_name + '.py')):
88
89
                try:
90
                    for bear in _import_bears(matching_file, kinds):
91
                        yield bear
92
                except BaseException as exception:
93
                    log_printer.log_exception(
94
                        "Unable to collect bears from {file}. Probably the "
95
                        "file is malformed or the module code raises an "
96
                        "exception.".format(file=matching_file),
97
                        exception,
98
                        log_level=LOG_LEVEL.WARNING)
99
100
101
def collect_bears(bear_dirs, bear_names, kinds, log_printer):
102
    """
103
    Collect all bears from bear directories that have a matching kind.
104
105
    :param bear_dirs:   directory name or list of such that can contain bears
106
    :param bear_names:  names of bears
107
    :param kinds: list  of bear kinds to be collected
108
    :param log_printer: log_printer to handle logging
109
    :return:            list of matching bear classes
110
    """
111
    return list(icollect_bears(bear_dirs, bear_names, kinds, log_printer))
112
113
114
def remove_ignored(file_paths, ignored_globs):
115
    """
116
    Removes file paths from list if they are ignored.
117
118
    :param file_paths:    file path string or list of such
119
    :param ignored_globs: list of globs that match to-be-ignored file paths
120
    :return:              list without those items that should be ignored
121
    """
122
    file_paths = list(set(file_paths))
123
124
    for file_path in file_paths:
125
        for ignored_glob in ignored_globs:
126
            if fnmatch(file_path, ignored_glob):
127
                file_paths.remove(file_path)
128
                break
129
    return file_paths
130