-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_db.py
54 lines (48 loc) · 1.63 KB
/
storage_db.py
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
import sys
import sqlite3
import json
import os
if sys.platform == "win32":
storage_folder = os.path.join(os.getenv('APPDATA'),"DeepMake")
elif sys.platform == "darwin":
storage_folder = os.path.join(os.getenv('HOME'),"Library","Application Support","DeepMake")
elif sys.platform == "linux":
storage_folder = os.path.join(os.getenv('HOME'),".local", "DeepMake")
if not os.path.exists(storage_folder):
os.mkdir(storage_folder)
class storage_db:
def __init__(self):
self.storage_db = os.path.join(storage_folder, 'data_storage.db')
self.init_db()
def init_db(self):
conn = sqlite3.connect(self.storage_db)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS key_value_store (
key TEXT PRIMARY KEY,
value TEXT
)
""")
conn.commit()
conn.close()
def store_data(self, key: str, item: dict):
try:
conn = sqlite3.connect(self.storage_db)
cursor = conn.cursor()
value = json.dumps(dict(item))
cursor.execute("REPLACE INTO key_value_store (key, value) VALUES (?, ?)", (key, value))
conn.commit()
conn.close()
return True
except:
return False
def retrieve_data(self, key: str):
conn = sqlite3.connect(self.storage_db)
cursor = conn.cursor()
cursor.execute("SELECT value FROM key_value_store WHERE key = ?", (key,))
row = cursor.fetchone()
conn.close()
if row:
data = json.loads(row[0])
return data
return False