feat: json formater

pull/442/head
QuentinN42 3 years ago
parent 6217241fec
commit fbf8108b97
No known key found for this signature in database
GPG Key ID: 2CD7D563712B3A50

@ -59,7 +59,7 @@ def run(argv=None):
help='custom configuration (as YAML source)')
parser.add_argument('-f', '--format',
choices=('parsable', 'standard', 'colored', 'github',
'auto'),
'json', 'auto'),
default='auto', help='format for parsing output')
parser.add_argument('-s', '--strict',
action='store_true',

@ -4,6 +4,7 @@ from __future__ import print_function
import os
import platform
import sys
import json
from yamllint.linter import PROBLEM_LEVELS
@ -209,6 +210,26 @@ class StandardFormater(Formater):
return line
class JSONFormater(Formater):
"""The parsable formater."""
name = 'json'
def show_problems_for_all_files(self, all_problems):
"""Show all problems of all files."""
lst = []
for k, v in all_problems.items():
lst += self.show_problems_for_file(v, k)
return json.dumps(lst, indent=4)
def show_problems_for_file(self, problems, file):
"""Show all problems of a specific file."""
return list(map(self.show_problem, problems, [file] * len(problems)))
def show_problem(self, problem, file):
"""Show all problems of a specific file."""
return {**problem.dict, "file": file}
def max_level(all_problems):
"""Return the max level of all problems."""
all_levels = [problem.level for problems in all_problems.values() for problem in problems]

@ -62,6 +62,17 @@ class LintProblem(object):
def __repr__(self):
return '%d:%d: %s' % (self.line, self.column, self.message)
@property
def dict(self):
"""Return self as a dictionary."""
return {
"line": self.line,
"column": self.column,
"desc": self.desc,
"rule": self.rule,
"level": self.level,
}
def get_cosmetic_problems(buffer, conf, filepath):
rules = conf.enabled_rules(filepath)

Loading…
Cancel
Save