diff --git a/yamllint/cli.py b/yamllint/cli.py index 617064a..e3b75ab 100644 --- a/yamllint/cli.py +++ b/yamllint/cli.py @@ -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', diff --git a/yamllint/format.py b/yamllint/format.py index 4ddcc4c..07663f4 100644 --- a/yamllint/format.py +++ b/yamllint/format.py @@ -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] diff --git a/yamllint/linter.py b/yamllint/linter.py index 7e0fb55..c35fc55 100644 --- a/yamllint/linter.py +++ b/yamllint/linter.py @@ -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)