From 59f21d6c7e97e7eb1ad70d31523929d398d27972 Mon Sep 17 00:00:00 2001 From: vg Date: Mon, 2 Jan 2023 18:23:33 +0100 Subject: git-sync on dita --- gamechestcli/gamechest/gameconfig.py | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'gamechestcli') diff --git a/gamechestcli/gamechest/gameconfig.py b/gamechestcli/gamechest/gameconfig.py index 3a53837..5e46efa 100644 --- a/gamechestcli/gamechest/gameconfig.py +++ b/gamechestcli/gamechest/gameconfig.py @@ -104,3 +104,45 @@ class GameConfig: # default instance of gameconfig, same instance intended to be shared through # all modules which needs it. config = GameConfig() + +class GameStatus: + + def load_yaml(self): + with contextlib.ExitStack() as stack: + stack.enter_context(contextlib.suppress(FileNotFoundError)) + fdin = stack.enter_context(open(self.yaml_path, 'r', + encoding='utf8')) + data = yaml.safe_load(fdin) + self.last_loaded_profile = data.get('last_loaded_profile', + self.last_loaded_profile) + self.last_loaded_game = data.get('last_loaded_game', + self.last_loaded_game) + + def write_yaml(self): + with contextlib.ExitStack() as stack: + stack.enter_context(contextlib.suppress(FileNotFoundError)) + fdout = stack.enter_context(open(self.yaml_path, 'w', + encoding='utf8')) + data = { + 'last_loaded_profile': self.last_loaded_profile, + 'last_loaded_game': self.last_loaded_game, + } + yaml.safe_dump(data, fdout) + + def __init__(self): + self.yaml_path = ( + xdg_data_home() / consts.XDG_RESOURCE_NAME / 'status.yaml' + ) + self.last_loaded_profile = config.get_profile_id() + self.last_loaded_game = None + self.load_yaml() + + def set_last_loaded_profile(self, profile): + self.last_loaded_profile = profile + self.write_yaml() + + def set_last_loaded_game(self, game): + self.last_loaded_game = game + self.write_yaml() + +status = GameStatus() -- cgit v1.2.3