Completed
Pull Request — master (#1152)
by Lasse
01:55
created

bears.coffee_script.CoffeeLintBear   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %
Metric Value
dl 0
loc 26
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 5 1
A process_output() 0 14 3
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 function (or class which has the
16
                       appropriate constructor).
17
    :return:           None or the converted value.
18
    """
19
    if value is not None and value != '':
20
        return conversion(value)
21
22
    return None
23
24
25
class CoffeeLintBear(LocalBear, Lint):
26
    executable = 'coffeelint'
27
    arguments = '--reporter=csv'
28
    severity_map = {'warn': RESULT_SEVERITY.NORMAL,
29
                    'error': RESULT_SEVERITY.MAJOR}
30
31
    def run(self, filename, file):
32
        """
33
        Coffeelint's your files!
34
        """
35
        return self.lint(filename)
36
37
    def process_output(self, output, filename):
38
        reader = DictReader(StringIO(output))
39
40
        for row in reader:
41
            try:
42
                yield Result.from_values(
43
                    origin=self,
44
                    message=row['message'],
45
                    file=filename,
46
                    line=convert_if_not_empty(row['lineNumber'], int),
47
                    end_line=convert_if_not_empty(row['lineNumberEnd'], int),
48
                    severity=self.severity_map[row['level']])
49
            except KeyError:  # Invalid CSV line, ignore
50
                pass
51