Some refactoring of the new external files

This commit is contained in:
Marc Di Luzio 2024-08-10 10:55:09 +01:00
parent c44f16dd8f
commit 96fb77f71f
3 changed files with 35 additions and 25 deletions

View file

@ -1,11 +1,19 @@
"""Store matching history"""
import os
import time
from schema import Schema, And, Use, Optional
from typing import Protocol
import matching
FILE = "history.json"
class Member(Protocol):
@property
def id(self) -> int:
pass
class History():
def __init__(self, data: dict):
self.__dict__ = data
@ -15,13 +23,31 @@ class History():
return self.__dict__["groups"]
@property
def matchees(self) -> dict:
def matchees(self) -> dict[str, dict]:
return self.__dict__["matchees"]
def save(self) -> None:
"""Save out the history"""
matching.save(FILE, self.__dict__)
def save_groups_to_history(self, groups: list[list[Member]]) -> None:
"""Save out the groups to the history file"""
ts = time.time()
for group in groups:
# Add the group
self.groups.append({
"ts": ts,
"matchees": list(m.id for m in group)
})
# Add the matches to the matchee data
for m in group:
matchee = self.matchees.get(str(m.id), {"matches": []})
for o in (o for o in group if o.id != m.id):
matchee["matches"].append({"ts": ts, "id": o.id})
self.matchees[str(m.id)] = matchee
self.save()
def load() -> History:
"""Load the history and validate it"""