1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
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 self.conn:
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(self):
for row in (
self.conn
.execute('SELECT game_id FROM status WHERE installed = 1')
):
yield row[0]
def set_uninstalled(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()
|