From 35c693ff250c23c649c8c1a006e499af9af91e5e Mon Sep 17 00:00:00 2001 From: Alexandros Kiousis Date: Thu, 18 May 2017 15:38:19 +0300 Subject: [PATCH 1/2] Add JSON output option --- yamllint/cli.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/yamllint/cli.py b/yamllint/cli.py index 41695a3..a167ede 100644 --- a/yamllint/cli.py +++ b/yamllint/cli.py @@ -20,6 +20,7 @@ import os.path import sys import argparse +import json from yamllint import APP_DESCRIPTION, APP_NAME, APP_VERSION from yamllint.config import YamlLintConfig, YamlLintConfigError @@ -48,6 +49,18 @@ class Format(object): 'level': problem.level, 'message': problem.message}) + @staticmethod + def json_output(problem, filename): + output_dict = {} + output_dict["path"] = filename + output_dict["line"] = problem.line + output_dict["char"] = problem.column + output_dict["description"] = problem.message + output_dict["code"] = "YAMLLINT" + output_dict["name"] = "YAMLLINT" + output_dict["severity"] = problem.level + return json.dumps(output_dict) + @staticmethod def standard(problem, filename): line = ' %d:%d' % (problem.line, problem.column) @@ -87,8 +100,8 @@ def run(argv=None): action='store', help='custom configuration (as YAML source)') parser.add_argument('-f', '--format', - choices=('parsable', 'standard'), default='standard', - help='format for parsing output') + choices=('parsable', 'standard', 'json'), + default='standard', help='format for parsing output') parser.add_argument('-s', '--strict', action='store_true', help='return non-zero exit code on warnings ' @@ -134,6 +147,8 @@ def run(argv=None): for problem in linter.run(f, conf, filepath): if args.format == 'parsable': print(Format.parsable(problem, file)) + elif args.format == 'json': + print(Format.json_output(problem, file)) elif sys.stdout.isatty(): if first: print('\033[4m%s\033[0m' % file) From b0412b5e9f7ab0c3e2d738125f58077eb915d765 Mon Sep 17 00:00:00 2001 From: Alexandros Kiousis Date: Fri, 1 Sep 2017 15:21:05 +0300 Subject: [PATCH 2/2] Simplify the json building lines --- yamllint/cli.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/yamllint/cli.py b/yamllint/cli.py index a167ede..841dd93 100644 --- a/yamllint/cli.py +++ b/yamllint/cli.py @@ -51,15 +51,15 @@ class Format(object): @staticmethod def json_output(problem, filename): - output_dict = {} - output_dict["path"] = filename - output_dict["line"] = problem.line - output_dict["char"] = problem.column - output_dict["description"] = problem.message - output_dict["code"] = "YAMLLINT" - output_dict["name"] = "YAMLLINT" - output_dict["severity"] = problem.level - return json.dumps(output_dict) + return json.dumps({ + "path": filename, + "line": problem.line, + "char": problem.column, + "description": problem.message, + "code": "YAMLLINT", + "name": "YAMLLINT", + "severity": problem.level, + }) @staticmethod def standard(problem, filename):