check_numberdealers/numberdealers/report_errors.py

69 lines
2.2 KiB
Python
Raw Permalink Normal View History

2023-10-27 14:18:51 +02:00
#!/usr/bin/env python3
from . import parse_numberdealers
2023-10-27 14:18:51 +02:00
2023-11-08 20:48:58 +01:00
def link(link_text: str, message_obj: parse_numberdealers.Message):
return f"[{link_text}]({parse_numberdealers.URL_PREFIX}{message_obj.id})"
2023-11-08 20:48:58 +01:00
def mention(message: parse_numberdealers.Message):
if message.username is not None:
return f" (@{message.username})"
2023-10-27 14:18:51 +02:00
else:
return ""
def str_from_error(err):
if isinstance(err, parse_numberdealers.UnrecognizedNumber):
msg = f"🚨 Unrecognized post {link(err.message.message, err.message)}"
elif isinstance(err, parse_numberdealers.EditedMessage):
msg = f"🚨 Edited post {link(err.message.message, err.message)}"
elif isinstance(err, parse_numberdealers.NonNumberMessage):
msg = f"🚨 Non-number message {link(err.message.message, err.message)}"
elif isinstance(err, parse_numberdealers.ShouldHaveBeen):
msg = f"🚨 {link(err.message.recognized_number, err.message)} should have been {err.expected_number}"
elif isinstance(err, parse_numberdealers.Duplicate):
msg = f"🚨 Duplicate {link(err.message.recognized_number, err.message)}"
elif isinstance(err, parse_numberdealers.Stray):
msg = f"🚨 Stray {link(err.message.recognized_number, err.message)}"
elif isinstance(err, parse_numberdealers.Skipped):
msg = f"🚨 {link('Skipped', err.message)} {err.expected_number}"
elif isinstance(err, parse_numberdealers.Jump):
msg = f"🚨 Going from {link(err.previous_message.recognized_number, err.previous_message)}" \
f" to {link(err.message.recognized_number, err.message)}"
return msg + mention(err.message)
def report_errors(errors):
if errors:
return list(map(str_from_error, errors))
else:
return []
def main():
import sys
error_style = {
"--numberdealers": parse_numberdealers.ErrorStyle.NUMBERDEALERS,
"--numberdealers-ng": parse_numberdealers.ErrorStyle.NUMBERDEALERS_NG
}[sys.argv[1]]
numbers, errors = parse_numberdealers.parse(sys.stdin, error_style)
if numbers == [] and errors == []:
print("No input data")
2023-10-27 14:18:51 +02:00
else:
if numbers == []:
print("No valid number messages!")
else:
print(f"Checked from {numbers[0].recognized_number} up to {numbers[-1].recognized_number}")
if not errors:
print("No errors! 🎉")
else:
print("\n".join(report_errors(errors)))
if __name__ == "__main__":
main()