-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve addon report infrastructure #64
Changes from 6 commits
d1ff7c8
6c21cf6
cd194c4
c986c1b
1482426
c385759
9b8cadc
d1235ed
8fb6fc0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import json | ||
import os | ||
from argparse import ArgumentParser | ||
|
||
from kodi_addon_checker.report import ReportManager | ||
|
||
|
||
class Config(object): | ||
def __init__(self, repo_path, cmd_args=None): | ||
self.configs = {} if cmd_args is None else vars(cmd_args) | ||
self._load_config(repo_path) | ||
|
||
def _load_config(self, repo_path): | ||
config_path = os.path.join(repo_path, '.tests-config.json') | ||
if os.path.isfile(config_path): | ||
with open(config_path) as json_data: | ||
file_config = json.load(json_data) | ||
if file_config is not None: | ||
for key, value in self.configs.items(): | ||
if value is not None: | ||
file_config[key] = value | ||
self.configs = file_config | ||
|
||
def is_enabled(self, value): | ||
return self.configs.get(value, False) | ||
|
||
def __getitem__(self, item): | ||
return self.configs.get(item) | ||
|
||
|
||
class ConfigManager(object): | ||
configurations = {} | ||
|
||
@classmethod | ||
def register(cls, config, description, default_value, action): | ||
cls.configurations[config] = [description, default_value, action] | ||
|
||
@classmethod | ||
def fill_cmd_args(cls, parser: ArgumentParser): | ||
# Add --reporter | ||
parser.add_argument("--reporter", action="append", choices=list(ReportManager.reporters.keys()), | ||
help="""enable a reporter with the given name. | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
You can use this option multiple times to enable more than one reporters""") | ||
|
||
@classmethod | ||
def process_config(cls, config): | ||
print(config.configs) | ||
reporters = config["reporter"] | ||
if reporters is not None: | ||
# To disable all, pass empty array in .tests-config.json | ||
ReportManager.enable(reporters) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from kodi_addon_checker.common import colorPrint | ||
|
||
from kodi_addon_checker.report import Reporter, Report, Record, reporter, INFORMATION, WARNING, PROBLEM | ||
|
||
|
||
@reporter(name="console", enabled=True) | ||
class ConsoleReporter(Reporter): | ||
|
||
def report(self, report: Report): | ||
if type(report) is Record: | ||
if report.log_level == INFORMATION: | ||
colorPrint(report, "34") | ||
elif report.log_level == WARNING: | ||
colorPrint(report, "35") | ||
elif report.log_level == PROBLEM: | ||
colorPrint(report, "31") | ||
else: | ||
for rep in report: | ||
self.report(rep) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import inspect | ||
from abc import ABC | ||
|
||
PROBLEM = "ERROR" | ||
WARNING = "WARN" | ||
INFORMATION = "INFO" | ||
|
||
|
||
class Report(object): | ||
def __init__(self): | ||
self.problem_count = 0 | ||
self.warning_count = 0 | ||
self.information_count = 0 | ||
self.reports = [] | ||
|
||
def add(self, report): | ||
self.reports.append(report) | ||
if type(report) is Record: | ||
if PROBLEM == report.log_level: | ||
self.problem_count += 1 | ||
elif WARNING == report.log_level: | ||
self.warning_count += 1 | ||
else: | ||
self.information_count += 1 | ||
else: | ||
self.problem_count += report.problem_count | ||
self.warning_count += report.warning_count | ||
self.information_count += report.information_count | ||
|
||
def __iter__(self): | ||
return iter(self.reports) | ||
|
||
|
||
class Record(Report): | ||
def __init__(self, log_level, message, start_line=-1, end_line=-1, start_char_position=-1, | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
end_char_position=-1): | ||
self.log_level = log_level | ||
self.message = message | ||
self.start_line = start_line | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
self.end_line = end_line | ||
self.start_char_position = start_char_position | ||
self.end_char_position = end_char_position | ||
|
||
def add(self, record): | ||
pass | ||
|
||
def __str__(self): | ||
""" | ||
Text representation of record. | ||
:return: text representation of report | ||
""" | ||
if self.start_line == -1: | ||
return "%s: %s" % (self.log_level, self.message) | ||
elif self.start_char_position == -1: | ||
return "%s [%d-%d]: %s" % (self.log_level, self.start_line, self.end_line, self.message) | ||
else: | ||
return "%s [%d:%d-%d:%d]: %s" % ( | ||
self.log_level, self.start_line, self.start_char_position, self.end_line, self.end_char_position, | ||
self.message) | ||
|
||
|
||
class Reporter(ABC): | ||
|
||
def report(self, report: Report): | ||
pass | ||
|
||
|
||
class ReportManager(object): | ||
reporters = {} | ||
|
||
@classmethod | ||
def register(cls, reporter_clazz: Reporter, name, enabled): | ||
cls.reporters[name] = [reporter_clazz, enabled] | ||
|
||
@classmethod | ||
def enable(cls, names): | ||
""" | ||
Enable only the given list of names and disable the rest. | ||
:param names: list of reporter names | ||
:return: None | ||
""" | ||
for name, arr in cls.reporters.items(): | ||
arr[1] = name in names | ||
|
||
@classmethod | ||
def report(cls, report: Report): | ||
for arr in cls.reporters.values(): | ||
if arr[1]: | ||
# Report enabled | ||
arr[0]().report(report) | ||
|
||
|
||
def reporter(name, enabled=False): | ||
def _reporter(clazz): | ||
if inspect.isclass(clazz): | ||
if not hasattr(clazz, "report") or len(inspect.signature(getattr(clazz, "report")).parameters.items()) != 2: | ||
raise RuntimeError("Reporter must have a function 'report(self, report: Report)") | ||
else: | ||
raise RuntimeError("Reporter must be a class") | ||
|
||
# Register the reporter | ||
ReportManager.register(clazz, name, enabled) | ||
return clazz | ||
|
||
return _reporter |
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.