-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_config.py
84 lines (62 loc) · 2.32 KB
/
data_config.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
"""Configuring data"""
import ast
from dataclasses import dataclass, asdict
@dataclass
class Work:
name: str
description: str
due_date: str
def to_dict(self):
return {k: str(v) for k, v in asdict(self).items()}
@dataclass
class UserClass:
name: str
permission_level: int # permission levels: 0 - student, 1 - admin, 2 - owner
def to_dict(self):
return {k: str(v) for k, v in asdict(self).items()}
SUBJECT_DATA = {'real': '',
'alias': [],
'description': 'No description',
'owner': '',
'admin': [],
'students': [],
'homework': {},
'project': {},
'test': {}, }
MESSAGE_DATA = {'emoji': [],
'role': [], }
USER_DATA = {'classes': {},
'reminders': [], }
with open('./data/subject_data', 'r') as f:
subject_data = ast.literal_eval(f.read())
with open('./data/message_data', 'r', encoding='utf-8') as f:
message_listener = ast.literal_eval(f.read())
with open('./data/user_data', 'r') as f:
user_data = ast.literal_eval(f.read())
def save_all() -> None:
with open('./data/subject_data', 'w') as file:
file.write(repr(subject_data))
with open('./data/message_data', 'w', encoding='utf-8') as file:
file.write(repr(message_listener))
with open('./data/user_data', 'w') as file:
file.write(repr(user_data))
def _generate_default_data(data: dict, default: dict):
for key, value in default.items():
if isinstance(value, dict):
if key not in data:
data[key] = {}
_generate_default_data(data[key], value)
if key not in data:
data[key] = value
def subject_generate_default_data(subject_name: str):
if subject_name.lower() not in subject_data:
subject_data[subject_name.lower()] = {}
_generate_default_data(subject_data[subject_name.lower()], SUBJECT_DATA)
def message_generate_default_data(message_id: int):
if message_id not in message_listener:
message_listener[message_id] = {}
_generate_default_data(message_listener[message_id], MESSAGE_DATA)
def user_generate_default_data(user_id: int):
if user_id not in user_data:
user_data[user_id] = {}
_generate_default_data(user_data[user_id], USER_DATA)