diff options
Diffstat (limited to 'gamechestcli')
-rw-r--r-- | gamechestcli/gamechest/gameconfig.py | 42 |
1 files changed, 42 insertions, 0 deletions
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() |