-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_manager.py
61 lines (43 loc) · 2.23 KB
/
data_manager.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
import json
from pathlib import Path
from nonebot import get_plugin_config
from .config import Config
class DataManager:
def __init__(self, data_storage_path: str) -> None:
self.data_storage_path = Path(data_storage_path)
self.data_storage_path.mkdir(parents=True, exist_ok=True)
self.binding_data = self.load_json("binding_data.json")
self.scheduled_tasks = self.load_json("scheduled_tasks.json")
self.query_limit_data = self.load_json("query_limit_data.json")
self.electricity_data = self.load_json("electricity_data.json")
def load_json(self, filename: str) -> dict:
file_path = self.data_storage_path / filename
if file_path.exists():
with file_path.open("r", encoding="utf-8") as f:
return json.load(f)
return {}
def save_json(self, data: dict, filename: str) -> None:
file_path = self.data_storage_path / filename
with file_path.open("w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=4)
def load_binding_data(self) -> None:
temp = self.load_json("binding_data.json")
self.binding_data = {"group": {}, "user": {}} if temp == {} else temp
def save_binding_data(self) -> None:
self.save_json(self.binding_data, "binding_data.json")
def load_scheduled_tasks(self) -> None:
temp = self.load_json("scheduled_tasks.json")
self.scheduled_tasks = {"group": {}, "user": {}} if temp == {} else temp
def save_scheduled_tasks(self) -> None:
self.save_json(self.scheduled_tasks, "scheduled_tasks.json")
def load_query_limit_data(self) -> None:
temp = self.load_json("query_limit_data.json")
self.query_limit_data = {"group": {}, "user": {}} if temp == {} else temp
def save_query_limit_data(self) -> None:
self.save_json(self.query_limit_data, "query_limit_data.json")
def load_electricity_data(self) -> None:
self.electricity_data = self.load_json("electricity_data.json")
def save_electricity_data(self) -> None:
self.save_json(self.electricity_data, "electricity_data.json")
config = get_plugin_config(Config).csust_electricity
data_manager = DataManager(config.data_storage_path)