-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson_storage.py
64 lines (56 loc) · 1.99 KB
/
json_storage.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
55
56
57
58
59
60
61
62
63
64
import json
import os
import logging
from datetime import datetime
logger = logging.getLogger('storage')
class JsonStorage:
def __init__(self):
self.filename = 'licenses.json'
self.data = self._load_data()
def _load_data(self):
try:
if os.path.exists(self.filename):
with open(self.filename, 'r') as f:
return json.load(f)
return {}
except Exception as e:
logger.error(f"Error loading JSON data: {str(e)}")
return {}
def _save_data(self):
try:
with open(self.filename, 'w') as f:
json.dump(self.data, f, indent=4, default=str)
except Exception as e:
logger.error(f"Error saving JSON data: {str(e)}")
raise
def test_connection(self):
try:
self._save_data()
return True
except Exception as e:
logger.error(f"Storage test failed: {str(e)}")
raise
def get_user_license(self, user_id: int):
str_id = str(user_id)
if str_id in self.data:
return (
user_id,
self.data[str_id]['license_key'],
self.data[str_id]['created_at'],
self.data[str_id]['expires_at'],
self.data[str_id]['license_id'] # Added license_id
)
return None
def add_user_license(self, user_id: int, license_key: str, license_id: int, expires_at: datetime):
self.data[str(user_id)] = {
'license_key': license_key,
'license_id': license_id, # Added license_id
'created_at': datetime.now().isoformat(),
'expires_at': expires_at.isoformat()
}
self._save_data()
def delete_user_license(self, user_id: int):
str_id = str(user_id)
if str_id in self.data:
del self.data[str_id]
self._save_data()