-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
320 lines (245 loc) · 10.4 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import datetime
import os
import json
from abc import ABCMeta, abstractmethod
from typing import Union, Optional, TypedDict, List, Tuple
# TODO: Configparser integration, unittests, documentation, and more database types.
class Database(metaclass=ABCMeta):
"""
Creates a blueprint for all the database types and subclasses to follow.
Extends ABCMeta to make the class abstract and uninstantiable.
Class attribute __instance ensures there is only one instance of the class.
.. seealso:: :class: `LocalDatabase`
"""
def __init__(self):
"""
Initializes the class attributes.
self.last_accessed keeps track of the last time the database was accessed (:func: `get_data`).
self.last_modified keeps track of the last time the database was modified (:func: `set_data`, :func: `delete_data`).
self.created_at keeps track of the time the database was created.
"""
self.last_accessed = None
self.last_modified = None
self.created_at = datetime.datetime.now()
@abstractmethod
def get_data(self, key: str) -> Union[dict, str]:
raise NotImplementedError
@abstractmethod
def set_data(self, key: str, value: Union[dict, str]) -> bool:
raise NotImplementedError
@abstractmethod
def delete_data(self, key: str) -> bool:
raise NotImplementedError
class DeserializedKeysDict(TypedDict):
"""
A type hint for the deserialized keys dictionary.
Class attribute main_key is the main key of the dictionary (the first key passed).
Class attribute subkeys is a list of subkeys (the rest of the keys passed).
.. seealso:: :class: `LocalDatabase`
"""
main_key: str
subkeys: Optional[List[str]]
class KeyNotFoundError(Exception):
"""
An exception that is raised when the key is not found in the database.
Extends Exception to make the class an exception.
.. seealso:: :class: `LocalDatabase`
"""
def __init__(self, key):
"""
Initializes the class attributes.
Extend Exception's __init__ method to set the message of the exception.
self.key is the key that was not found in the database.
"""
self.key = key
super().__init__(
f'Key not found in DB records [{self.key}]')
def __str__(self):
return f'Key not found in DB records [{self.key}]'
def __repr__(self):
return f'Key not found in DB records [{self.key}]'
def __reduce__(self):
return (self.__class__, (self.key,))
class LocalDatabase(Database):
"""
A class that represents a local database.
Extends Database to inherit the abstract methods.
Class attribute __instance ensures there is only one instance of the class.
.. seealso:: :class: `Database`
"""
__instance = None
def __new__(cls, path: str, *args, **kwargs):
"""
Creates a new instance of the class if there is no instance already.
If there is an instance, it returns the instance instead of creating a new one.
:param cls: The class that is being instantiated.
:type cls: class
:param path: The path to the database file.
:type path: str
:return: The instance of the class.
:rtype: :class: `LocalDatabase`
:raises: FileNotFoundError: If the database file is not found. (or not a JSON file)
.. seealso:: :class: `Database`
.. note:: If you want to create multiple instances of the class, you can safely remove this function.
"""
if cls.__instance is None:
if not cls.__is_valid_path(path):
raise FileNotFoundError(f'Could not find the database file at {path} [The file must be a JSON file]')
cls.__instance = super().__new__(cls, *args, **kwargs)
print(f'A new database connection has been created successfully! [ID: {id(cls.__instance)}]')
else:
print(f'A database connection has already been made. Reusing the same connection. [ID: {id(cls.__instance)}]')
return cls.__instance
def __init__(self, path: str):
"""
Initializes the class attributes.
Extends Database's __init__ method to use its attributes.
self.path is the path to the database file.
:param path: The path to the database file.
:type path: str
.. seealso:: :class: `Database`
"""
print('Connected to the database!')
self.path = path
super().__init__()
def __str__(self):
"""Returns a string representation of the class. (User friendly)"""
return f'LocalDatabase Connection (ID: {id(self)})'
def __repr__(self):
"""Returns a string representation of the class. (Developer friendly)"""
return f'Database Connection (ID: {id(self)})'
def __del__(self):
"""Disconnects from the database when the instance is deleted."""
print('Disconnected from the database!')
self.__instance = None
def get_data(self, key: str) -> Union[dict, str]:
"""
Gets the data from the database.
:param key: The key to get the data from.
:type key: str
:return: The data from the database.
:rtype: Union[dict, str]
.. seealso:: :class: `Database`
"""
with open(self.path, 'rt', encoding='utf-8') as db_file:
db = json.load(db_file)
self.last_accessed = datetime.datetime.now()
key, subkeys = self.__deserialize_key(key).values()
data: Union[dict, str] = db.get(key, None)
if subkeys and isinstance(data, dict):
for subkey in subkeys:
data = data.get(subkey, None)
if data is None:
break
return data
def set_data(self, key: str, value: Union[dict, str]) -> bool:
"""
Sets the data to the database.
:param key: The key to set the data to.
:type key: str
:param value: The value to set to the key.
:type value: Union[dict, str]
:return: True if the data is set successfully, False otherwise.
:rtype: bool
:raises: KeyNotFoundError: If the key is not found in the database.
.. seealso:: :class: `Database`
"""
with open(self.path, 'r+t', encoding='utf-8') as db_file:
db = json.load(db_file)
self.last_modified = datetime.datetime.now()
key, subkeys = self.__deserialize_key(key).values()
if key not in db:
raise KeyNotFoundError(key)
if subkeys and len(subkeys) > 0:
self.__find_nested_data(db_content=db, keys=(key, subkeys), operation='set', value=value)
else:
db[key] = value
return self.__update_database(db, db_file)
def delete_data(self, key: str) -> bool:
"""
Deletes the data from the database.
:param key: The key to delete the data from.
:type key: str
:return: True if the data is deleted successfully, False otherwise.
:rtype: bool
.. seealso:: :class: `Database`
"""
with open(self.path, 'r+t', encoding='utf-8') as db_file:
self.last_modified = datetime.datetime.now()
db = json.load(db_file)
key, subkeys = self.__deserialize_key(key).values()
if subkeys and len(subkeys) > 0:
self.__find_nested_data(db_content=db, keys=(key, subkeys), operation='delete')
else:
db.pop(key, None)
return self.__update_database(db, db_file)
@staticmethod
def __update_database(new_db_content: dict, db_file):
"""
Updates the database file with the new content.
:param new_db_content: The new content to update the database file with.
:type new_db_content: dict
:param db_file: The database file to update.
:type db_file: file
:return: True if the database is updated successfully, False otherwise.
:rtype: bool
.. seealso:: :class: `Database`
"""
db_file.seek(0)
json.dump(new_db_content, db_file, indent=4, ensure_ascii=False)
db_file.truncate()
return True
@staticmethod
def __find_nested_data(*, db_content: dict, keys: Tuple[str, List[str]], operation: str, value: Optional[Union[dict, str]] = '') -> Union[dict, str]:
"""
Finds the nested data in the database.
:param db_content: The database content to find the nested data in.
:type db_content: dict
:param keys: The keys to find the nested data in. (main key, subkeys)
:type keys: Tuple[str, List[str]]
:param operation: The operation to do with the nested data.
:type operation: str
:param value: The value to set to the nested data.
:type value: Optional[Union[dict, str]]
:return: The nested data.
:rtype: Union[dict, str]
.. seealso:: :class: `Database`
"""
key, subkeys = keys
if subkeys:
current_dict = db_content[key]
for key in subkeys[:-1]:
current_dict = current_dict.setdefault(key, {})
if operation == 'delete':
current_dict.pop(subkeys[-1], None)
elif operation == 'set':
current_dict[subkeys[-1]] = value
else:
return db_content[key]
@staticmethod
def __deserialize_key(key: List[str]) -> DeserializedKeysDict:
"""
Deserializes the key.
:param key: The key to deserialize.
:type key: List[str]
:return: The deserialized key.
:rtype: DeserializedKeysDict
.. seealso:: :class: `Database`
"""
# 'users/user1' -> ['users', 'user1']
key = key.strip().split('/')
return {
'main_key': key[0],
'subkeys': key[1:]
}
@staticmethod
def __is_valid_path(path: str) -> bool:
"""
Checks if the path is valid.
:param path: The path to check.
:type path: str
:return: True if the path is valid, False otherwise.
:rtype: bool
.. seealso:: :class: `Database`
"""
return os.path.exists(path) and os.path.isfile(path) and path.endswith('.json')