from csv import DictReader
from io import StringIO

from coalib.bearlib.abstractions.Linter import linter
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
from coalib.results.Result import Result


def convert_if_not_empty(value: str, conversion):
    """
    Returns the value converted if it is not None or empty.

    :param value:      The value to convert.
    :param conversion: The conversion callable.
    :return:           None or the converted value.
    """
    if value is not None and value != '':
        return conversion(value)

    return None


@linter(executable='coffeelint')
class CoffeeLintBear:
    """
    Check CoffeeScript code for a clean and consistent style.

    For more information about coffeelint, visit <http://www.coffeelint.org/>.
    """

    LANGUAGES = "CoffeeScript"

    severity_map = {'warn': RESULT_SEVERITY.NORMAL,
                    'error': RESULT_SEVERITY.MAJOR}

    @staticmethod
    def create_arguments(filename, file, config_file):
        return '--reporter=csv', filename

    def process_output(self, output, filename, file):
        reader = DictReader(StringIO(output))

        for row in reader:
            try:
                yield Result.from_values(
                    origin=self,
                    message=row['message'],
                    file=filename,
                    line=convert_if_not_empty(row['lineNumber'], int),
                    end_line=convert_if_not_empty(row['lineNumberEnd'], int),
                    severity=self.severity_map[row['level']])
            except KeyError:  # Invalid CSV line, ignore
                pass

@@ -1,54 +1,15 @@
-from csv import DictReader%0Afrom io import StringIO
+import json
 %0A%0Afr
@@ -166,374 +166,8 @@
 t%0A%0A%0A
-def convert_if_not_empty(value: str, conversion):%0A    %22%22%22%0A    Returns the value converted if it is not None or empty.%0A%0A    :param value:      The value to convert.%0A    :param conversion: The conversion callable.%0A    :return:           None or the converted value.%0A    %22%22%22%0A    if value is not None and value != '':%0A        return conversion(value)%0A%0A    return None%0A%0A%0A
 @lin
@@ -617,11 +617,11 @@
 ter=
-csv
+raw
 ', f
@@ -695,44 +695,35 @@
     
-reader = DictReader(StringIO
+output = json.loads
 (output)
 )%0A%0A 
@@ -718,17 +718,16 @@
 (output)
-)
 %0A%0A      
@@ -732,48 +732,128 @@
     
-for row in reader:%0A            try:%0A    
+assert len(output) == 1, %22More than 1 file parsed, something went wrong%22%0A        for item in tuple(output.values())%5B0%5D:%0A
     
@@ -906,29 +906,58 @@
     
-   
- 
 origin=
-self,%0A    
+%22%7B%7D (%7B%7D)%22.format(self.name, item%5B'rule'%5D),%0A
     
@@ -976,19 +976,20 @@
 message=
-row
+item
 %5B'messag
@@ -1009,20 +1009,16 @@
         
-    
 file=fil
@@ -1044,42 +1044,22 @@
     
-    
 line=
-convert_if_not_empty(row%5B
+item.get(
 'lin
@@ -1066,22 +1066,22 @@
 eNumber'
-%5D
 , 
-int
+None
 ),%0A     
@@ -1091,20 +1091,16 @@
         
-  
-  
 end_line
@@ -1104,33 +1104,17 @@
 ine=
-convert_if_not_empty(row%5B
+item.get(
 'lin
@@ -1128,21 +1128,17 @@
 End'
-%5D
 , 
-int),%0A    
+None),%0A
     
@@ -1180,11 +1180,12 @@
 map%5B
-row
+item
 %5B'le
@@ -1190,17 +1190,17 @@
 level'%5D%5D
-)
+,
 %0A       
@@ -1208,70 +1208,115 @@
     
-except KeyError:  # Invalid CSV line, ignore%0A                pass
+    additional_info=item.get('description',%0A                                         item.get('context', %22%22)))
 %0A

Use --format=raw for json