-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
160 lines (123 loc) · 4.55 KB
/
database.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# © Copyright 2023
# Licensed under the MIT License
# https://opensource.org/licenses/MIT
# https://github.com/OctoDiary
from typing import Any
from lightdb import LightDB
class User:
"""Class for user in database"""
def __init__(self, db: "Database", user_id: str):
self.__db = db
self.__id = user_id
def get(self, key: str, default: Any = None) -> Any:
return self.__db.get_key(self.__id, key, default=default)
def set(self, key: str, value: Any) -> None:
self.__db.set_key(self.__id, key, value)
def pop(self, key: str) -> Any:
return self.__db.pop_key(self.__id, key)
def pop_key(self, attr: str, key: str, default: Any = None) -> Any:
attr_value = self.__db.get_key(self.__id, attr)
if attr_value is None:
return None
value = attr_value.pop(key, default)
self.__db.set_key(self.__id, attr, attr_value)
return value
def set_key(self, attr: str, key: str, value: Any) -> None:
attr_value = self.__db.get_key(self.__id, attr)
if attr_value is None:
attr_value = {}
attr_value[key] = value
self.__db.set_key(self.__id, attr, attr_value)
def get_key(self, attr: str, key: str, default: Any = None) -> Any:
attr_value = self.__db.get_key(self.__id, attr)
if attr_value is None:
return default
return attr_value.get(key, default)
def save(self):
self.__db.save()
@property
def token(self) -> str:
return self.get("token")
@token.setter
def token(self, value: str) -> None:
self.set("token", value)
@property
def system(self) -> str:
return self.get("system")
@system.setter
def system(self, value: str) -> None:
self.set("system", value)
def __getattribute__(self, __name: str) -> Any:
if __name.startswith("db_"):
return self.get(__name[3:])
elif __name == "id":
return self.__id
return super().__getattribute__(__name)
def __setattr__(self, __name: str, __value: Any) -> None:
if __name.startswith("db_"):
self.set(__name[3:], __value)
else:
super().__setattr__(__name, __value)
def __getitem__(self, __name: str) -> Any:
return self.get(__name)
def __setitem__(self, __name: str, __value: Any) -> None:
self.set(__name, __value)
def __delitem__(self, __name: str) -> None:
self.pop(__name)
@property
def cache(self) -> dict:
return self.__db.cache.get(self.__id, {})
@cache.setter
def cache(self, value: dict) -> None:
self.__db.cache.set(self.__id, value)
def cache_key(self, name: str, default: Any = None) -> str:
return self.cache.get(name, default)
def cache_set_key(self, name: str, value: Any) -> None:
self.cache[name] = value
self.__db.cache.save()
class Database(LightDB):
"""
Main database class
"""
__instance__ = None
def __new__(cls):
if cls.__instance__ is None:
cls.__instance__ = super().__new__(cls)
return cls.__instance__
def __init__(self) -> None:
super().__init__(location="users_db.json")
self.settings = LightDB("settings.json")
self.cache = LightDB("cache.json")
def __getattribute__(self, __name: str) -> Any:
return (
self.get(__name[3:])
if __name.startswith("db_")
else self.settings.get(__name[9:])
if __name.startswith("settings_")
else super().__getattribute__(__name)
)
def __setattr__(self, __name: str, __value: Any) -> None:
return (
self.set(__name[3:], __value)
if __name.startswith("db_")
else self.settings.set(__name[9:], __value)
if __name.startswith("settings_")
else super().__setattr__(__name, __value)
)
def user(self, id: str | int) -> User:
return User(self, str(id))
@property
def closed(self) -> bool:
return self.settings.get("closed", False)
@closed.setter
def closed(self, value: bool) -> None:
self.settings.set("closed", value)
@property
def admins(self) -> list[str]:
return self.settings.get("admins", [5184725450, 692755648])
@admins.setter
def admins(self, value: list[str]) -> None:
self.settings.set("admins", value)
@property
def blocked_users(self) -> list[int]:
return self.settings.get("blocked-users", [])