Completed
Pull Request — master (#1116)
by Lasse
01:41
created

bears.general.SpaceConsistencyBear   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 0 %
Metric Value
dl 0
loc 67
rs 10
wmc 11

1 Method

Rating   Name   Duplication   Size   Complexity  
F run() 0 66 11
1
from coalib.bearlib.spacing.SpacingHelper import SpacingHelper
2
from coalib.results.Diff import Diff
3
from coalib.bears.LocalBear import LocalBear
4
from coalib.results.Result import Result
5
6
7
class SpaceConsistencyBear(LocalBear):
8
    def run(self,
9
            filename,
10
            file,
11
            use_spaces: bool,
12
            allow_trailing_whitespace: bool=False,
13
            tab_width: int=SpacingHelper.DEFAULT_TAB_WIDTH,
14
            enforce_newline_at_EOF: bool=True):
15
        '''
16
        Checks the space consistency for each line.
17
18
        :param use_spaces:                True if spaces are to be used instead
19
                                          of tabs.
20
        :param allow_trailing_whitespace: Whether to allow trailing whitespace
21
                                          or not.
22
        :param tab_width:                 Number of spaces representing one
23
                                          tab.
24
        :param enforce_newline_at_EOF:    Whether to enforce a newline at the
25
                                          End Of File.
26
        '''
27
        spacing_helper = SpacingHelper(tab_width)
28
        result_texts = []
29
30
        for line_number, line in enumerate(file, start=1):
31
            replacement = line
32
33
            if enforce_newline_at_EOF:
34
                # Since every line contains at the end at least one \n, only
35
                # the last line could potentially not have one. So we don't
36
                # need to check whether the current line_number is the last
37
                # one.
38
                if replacement[-1] != "\n":
39
                    replacement += "\n"
40
                    result_texts.append("No newline at EOF.")
41
42
            if not allow_trailing_whitespace:
43
                pre_replacement = line
44
                replacement = replacement.rstrip(" \t\n") + "\n"
45
                if replacement != pre_replacement:
46
                    result_texts.append("Trailing whitespaces.")
47
48
            if use_spaces:
49
                pre_replacement = replacement
50
                replacement = spacing_helper.replace_tabs_with_spaces(
51
                    replacement)
52
                if replacement != pre_replacement:
53
                    result_texts.append("Tabs used instead of spaces.")
54
            else:
55
                pre_replacement = replacement
56
                replacement = spacing_helper.replace_spaces_with_tabs(
57
                    replacement)
58
                if replacement != pre_replacement:
59
                    result_texts.append("Spaces used instead of tabs.")
60
61
            if len(result_texts) > 0:
62
                diff = Diff(file)
63
                diff.change_line(line_number, line, replacement)
64
                inconsistencies = "".join("\n- " + string
65
                                          for string in result_texts)
66
                yield Result.from_values(
67
                    self,
68
                    "Line contains following spacing inconsistencies:"
69
                    + inconsistencies,
70
                    diffs={filename: diff},
71
                    file=filename,
72
                    line=line_number)
73
                result_texts = []
74