check_numberdealers/check_numberdealers.py

54 lines
2 KiB
Python
Executable file

#!/usr/bin/env python3
import parse_numberdealers
def link(link_text: str, message_obj: parse_numberdealers.Message):
return f"[{link_text}]({parse_numberdealers.URL_PREFIX}{message_obj.id})"
def mention(message: parse_numberdealers.Message):
if message.username is not None:
return f" (@{message.username})"
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 main():
import sys
numbers, errors = parse_numberdealers.parse(sys.stdin)
if numbers == [] and errors == []:
print("No input data")
else:
if numbers == []:
print("No valid number messages!")
else:
print(f"Checked from {numbers[0].recognized_number} up to {numbers[-1].recognized_number}")
if errors:
print("🚨 Errors: 🚨")
print("\n".join(map(str_from_error, errors)))
else:
print("No errors! 🎉")
if __name__ == "__main__":
main()