Commit f2f2dbbb authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Add configurable output for plugin

parent e5022212
Loading
Loading
Loading
Loading
+26 −3
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ Module providing a Pylint plugin

import json

import pylint.config
import pylint.reporters


@@ -27,7 +28,8 @@ class JSONStatsReporter:
	Serialise reports and stats to a JSON file
	"""

	def __init__(self, parent=None):
	def __init__(self, options, parent=None):
		self.options = options
		self.parent = parent or pylint.reporters.BaseReporter()
		self.messages = []

@@ -55,10 +57,28 @@ class JSONStatsReporter:
			stats=stats,
			previous=previous_stats,
		)
		with open('.lint.json', 'w') as fout:
		with open(self.options.config.json_output, 'w') as fout:
			json.dump(report, fout, default=serialiser, indent=4)


class JSONStatsOptions(pylint.config.OptionsProviderMixIn):
	"""Options for the JSONStatsReporter plugin"""

	name = 'json_stats'
	options = (
		(
			"json-output",
			{
				"type": "string",
				"dest": "json_output",
				"default": "lint.json",
				"metavar": "<output-json>",
				"help": "The filename to output JSON serialised results and stats",
			}
		),
	)


def serialiser(obj):
	"""Serialise sets as JSON arrays"""
	if isinstance(obj, set):
@@ -68,8 +88,11 @@ def serialiser(obj):

def register(linter):
	"""Pylint plugin entrypoint function"""
	options = JSONStatsOptions()
	linter.register_options_provider(options)

	def set_reporter(reporter):
		org_set_reporter(JSONStatsReporter(reporter))
		org_set_reporter(JSONStatsReporter(options, reporter))
	linter.set_reporter, org_set_reporter = set_reporter, linter.set_reporter
	set_reporter(linter.reporter)