-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
49 lines (35 loc) · 1.32 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
import functools
from typing import Dict
import pyjson5
from data_objects import *
from singleton import Singleton
@functools.cache
def parse_database():
with open('./database.json') as json_data:
d = pyjson5.load(json_data)
keys = [KeyData(id=v["id"], rf_id=v["rf_id"], name=v["name"]) for v in d["keys"]]
users = [UserData(id=v["id"], rf_id=v["rf_id"], name=v["name"], authorized_for=v["authorized_for"]) for v in
d["users"]]
return keys, users
class KeysDB(Singleton):
_keys_by_id: Dict[str, KeyData]
_keys_by_rf_id: Dict[str, KeyData]
def __init__(self):
keys, _ = parse_database()
self._keys_by_id = {v.id: v for v in keys}
self._keys_by_rf_id = {v.rf_id: v for v in keys}
def by_id(self, k_id):
return self._keys_by_id.get(k_id)
def by_rf_id(self, rf_id):
return self._keys_by_rf_id.get(rf_id)
class UsersDB(Singleton):
_users_by_id: Dict[str, UserData]
_users_by_rf_id: Dict[str, UserData]
def __init__(self):
_, users = parse_database()
self._users_by_id = {v.id: v for v in users}
self._users_by_rf_id = {v.rf_id: v for v in users}
def by_id(self, k_id):
return self._users_by_id.get(k_id)
def by_rf_id(self, rf_id):
return self._users_by_rf_id.get(rf_id)