diff options
Diffstat (limited to 'gamechestcli/gamechest/statusdb.py')
-rw-r--r-- | gamechestcli/gamechest/statusdb.py | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/gamechestcli/gamechest/statusdb.py b/gamechestcli/gamechest/statusdb.py new file mode 100644 index 0000000..a010206 --- /dev/null +++ b/gamechestcli/gamechest/statusdb.py @@ -0,0 +1,93 @@ +import sqlite3 + +from xdg import xdg_state_home + +from . import consts + + +class StatusDB: + + def __init__(self): + db_path_dir = xdg_state_home() / consts.XDG_RESOURCE_NAME + db_path_dir.mkdir(parents=True, exist_ok=True) + db_path = db_path_dir / consts.STATUS_DB_NAME + self.conn = sqlite3.connect(db_path) + with self.conn: + self.conn.execute(''' + CREATE TABLE IF NOT EXISTS status ( + game_id text, + installed bool, + version_installed text + ) + ''') + + def close(self): + self.conn.close() + + def is_installed(self, game_info): + row = ( + self.conn + .execute('SELECT installed FROM status WHERE game_id = ?', + (game_info['id'], )) + .fetchone() + ) + if row is None: + return False + return bool(row[0]) + + def set_installed(self, game_info, installed=True): + cursor = self.conn.cursor() + row = ( + cursor + .execute('SELECT installed FROM status WHERE game_id = ?', + (game_info['id'], )) + .fetchone() + ) + with cursor: + if row is None: + cursor.execute(''' + INSERT INTO status + (game_id, installed, version_installed) + VALUES (?, ?, ?) + ''', ( + game_info['id'], + installed, + game_info['version'], + )) + else: + cursor.execute(''' + UPDATE status SET + installed = ?, + version_installed = ? + WHERE game_id = ? + ''', ( + installed, + game_info['version'], + game_info['id'], + )) + + def get_installed_game_name(self, game_id): + row = ( + self.conn + .execute('SELECT version_installed FROM status WHERE game_id = ?', + (game_id, )) + .fetchone() + ) + if row is None: + #return False + raise ValueError('Game not found') + version_installed = row[0] + return f'{game_id}_v{version}' + + def unset_installed(self, game_info): + return self.set_installed(game_info, installed=False) + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, traceback): + self.close() + + +if __name__ == "__main__": + status_db = StatusDB() |