From e8f06b59aa8921347df923b031006d4ca3a1c92a Mon Sep 17 00:00:00 2001 From: Martin Mares <mj@ucw.cz> Date: Fri, 1 Jan 2021 22:58:06 +0100 Subject: [PATCH] =?UTF-8?q?mo.util:=20die=20a=20parsov=C3=A1n=C3=AD=20k?= =?UTF-8?q?=C3=B3d=C5=AF=20kol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Kódy kol možná patří do mo.db, ale mám pocit, že se z toho už stává ošklivé skladiště. --- mo/util.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/mo/util.py b/mo/util.py index 2a817c45..6a021e24 100644 --- a/mo/util.py +++ b/mo/util.py @@ -1,12 +1,13 @@ # Různé +from dataclasses import dataclass import datetime import email.message import email.headerregistry import re -from sqlalchemy.orm import joinedload import subprocess -from typing import Any, Optional +import sys +from typing import Any, Optional, Noreturn import textwrap import mo.db as db @@ -64,3 +65,30 @@ def send_password_reset_email(user: db.User, link: str): if sm.returncode != 0: raise RuntimeError('Sendmail failed with return code {}'.format(sm.returncode)) + + +def die(msg: str) -> Noreturn: + print(msg, file=sys.stderr) + sys.exit(1) + + +@dataclass +class RoundCode: + year: int + cat: str + seq: int + + def __str__(self): + return f'{self.year}-{self.cat}-{self.seq}' + + @staticmethod + def parse(code: str) -> Optional['RoundCode']: + m = re.match(r'(\d+)-([A-Z0-9]+)-(\d+)', code) + if m: + return RoundCode(year=int(m[1]), cat=m[2], seq=int(m[3])) + else: + return None + + +def get_round_by_code(code: RoundCode) -> Optional[db.Round]: + return db.get_session().query(db.Round).filter_by(year=code.year, category=code.cat, seq=code.seq).one_or_none() -- GitLab