ui: save periodically in every frontend

This commit is contained in:
rr-
2019-01-03 21:44:23 +01:00
parent ca7ab192c5
commit e008fc6048
6 changed files with 38 additions and 12 deletions
+9
View File
@@ -1,5 +1,7 @@
import logging
import pickle
import typing as T
from datetime import datetime
from pathlib import Path
from pqcli.mechanic import Player
@@ -9,6 +11,7 @@ class Roster:
def __init__(self, path: Path, players: T.List[Player]) -> None:
self.path = path
self.players = players
self._last_save = datetime.now()
@staticmethod
def load(path: T.Union[str, Path]) -> "Roster":
@@ -20,6 +23,7 @@ class Roster:
return Roster(real_path, players=[])
def save(self) -> None:
self._last_save = datetime.now()
old_path = self.path.with_name(self.path.name + ".old")
tmp_path = self.path.with_name(self.path.name + ".new")
tmp_path.parent.mkdir(parents=True, exist_ok=True)
@@ -27,3 +31,8 @@ class Roster:
if self.path.exists():
self.path.rename(old_path)
tmp_path.rename(self.path)
def save_periodically(self):
if (datetime.now() - self._last_save).total_seconds() >= 300:
logging.info("Saving...")
self.save()