add json-formatted output
This commit is contained in:
@@ -21,6 +21,7 @@ import io
|
|||||||
import os
|
import os
|
||||||
import platform
|
import platform
|
||||||
import sys
|
import sys
|
||||||
|
import json
|
||||||
|
|
||||||
from yamllint import APP_DESCRIPTION, APP_NAME, APP_VERSION
|
from yamllint import APP_DESCRIPTION, APP_NAME, APP_VERSION
|
||||||
from yamllint import linter
|
from yamllint import linter
|
||||||
@@ -84,10 +85,22 @@ class Format(object):
|
|||||||
line += ' \033[2m(%s)\033[0m' % problem.rule
|
line += ' \033[2m(%s)\033[0m' % problem.rule
|
||||||
return line
|
return line
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def json(problem, filename):
|
||||||
|
return json.dumps({
|
||||||
|
"path": filename,
|
||||||
|
"line": problem.line,
|
||||||
|
"char": problem.column,
|
||||||
|
"description": problem.message,
|
||||||
|
"code": "yamllint",
|
||||||
|
"name": "yamllint",
|
||||||
|
"severity": problem.level,
|
||||||
|
})
|
||||||
|
|
||||||
def show_problems(problems, file, args_format, no_warn):
|
def show_problems(problems, file, args_format, no_warn):
|
||||||
max_level = 0
|
max_level = 0
|
||||||
first = True
|
first = True
|
||||||
|
problems_json = []
|
||||||
|
|
||||||
for problem in problems:
|
for problem in problems:
|
||||||
max_level = max(max_level, PROBLEM_LEVELS[problem.level])
|
max_level = max(max_level, PROBLEM_LEVELS[problem.level])
|
||||||
@@ -95,6 +108,8 @@ def show_problems(problems, file, args_format, no_warn):
|
|||||||
continue
|
continue
|
||||||
if args_format == 'parsable':
|
if args_format == 'parsable':
|
||||||
print(Format.parsable(problem, file))
|
print(Format.parsable(problem, file))
|
||||||
|
elif args_format == 'json':
|
||||||
|
problems_json.append(json.loads(Format.json(problem, file)))
|
||||||
elif args_format == 'colored' or \
|
elif args_format == 'colored' or \
|
||||||
(args_format == 'auto' and supports_color()):
|
(args_format == 'auto' and supports_color()):
|
||||||
if first:
|
if first:
|
||||||
@@ -107,12 +122,14 @@ def show_problems(problems, file, args_format, no_warn):
|
|||||||
first = False
|
first = False
|
||||||
print(Format.standard(problem, file))
|
print(Format.standard(problem, file))
|
||||||
|
|
||||||
|
if args_format == 'json':
|
||||||
|
print(json.dumps(problems_json))
|
||||||
|
|
||||||
if not first and args_format != 'parsable':
|
if not first and args_format != 'parsable':
|
||||||
print('')
|
print('')
|
||||||
|
|
||||||
return max_level
|
return max_level
|
||||||
|
|
||||||
|
|
||||||
def run(argv=None):
|
def run(argv=None):
|
||||||
parser = argparse.ArgumentParser(prog=APP_NAME,
|
parser = argparse.ArgumentParser(prog=APP_NAME,
|
||||||
description=APP_DESCRIPTION)
|
description=APP_DESCRIPTION)
|
||||||
@@ -130,7 +147,7 @@ def run(argv=None):
|
|||||||
action='store',
|
action='store',
|
||||||
help='custom configuration (as YAML source)')
|
help='custom configuration (as YAML source)')
|
||||||
parser.add_argument('-f', '--format',
|
parser.add_argument('-f', '--format',
|
||||||
choices=('parsable', 'standard', 'colored', 'auto'),
|
choices=('parsable', 'standard', 'colored', 'auto', 'json'),
|
||||||
default='auto', help='format for parsing output')
|
default='auto', help='format for parsing output')
|
||||||
parser.add_argument('-s', '--strict',
|
parser.add_argument('-s', '--strict',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
|
|||||||
Reference in New Issue
Block a user