Failed Conditions
Pull Request — master (#1404)
by Abdeali
01:46 queued 12s
created

_process_issues()   A

Complexity

Conditions 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 14
rs 9.4285
1
from io import StringIO
2
from csv import DictReader
3
4
from coalib.bearlib.abstractions.Lint import Lint
5
from coalib.bears.LocalBear import LocalBear
6
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
7
from coalib.results.Result import Result
8
9
10
def convert_if_not_empty(value: str, conversion):
11
    """
12
    Returns the value converted if it is not None or empty.
13
14
    :param value:      The value to convert.
15
    :param conversion: The conversion callable.
16
    :return:           None or the converted value.
17
    """
18
    if value is not None and value != '':
19
        return conversion(value)
20
21
    return None
22
23
24
class CoffeeLintBear(LocalBear, Lint):
25
    executable = 'coffeelint'
26
    arguments = '--reporter=csv'
27
    severity_map = {'warn': RESULT_SEVERITY.NORMAL,
28
                    'error': RESULT_SEVERITY.MAJOR}
29
30
    def run(self, filename, file):
31
        """
32
        Coffeelint's your files!
33
        """
34
        return self.lint(filename)
35
36
    def _process_issues(self, output, filename, file):
37
        reader = DictReader(StringIO("".join(output)))
38
39
        for row in reader:
40
            try:
41
                yield Result.from_values(
42
                    origin=self,
43
                    message=row['message'],
44
                    file=filename,
45
                    line=convert_if_not_empty(row['lineNumber'], int),
46
                    end_line=convert_if_not_empty(row['lineNumberEnd'], int),
47
                    severity=self.severity_map[row['level']])
48
            except KeyError:  # Invalid CSV line, ignore
49
                pass
50