-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
174 lines (153 loc) · 5.41 KB
/
data.py
File metadata and controls
174 lines (153 loc) · 5.41 KB
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
import json
import datetime
class Server_Meta_Data:
pwd: str
http_port: int
hello_port: int
def __init__(self, pwd:str="默认密码", http_port:int=5000, hello_port:int=5001):
self.pwd = pwd
self.http_port = http_port
self.hello_port = hello_port
@classmethod
def from_dict(cls, data:dict):
"""从字典创建实例"""
return cls(
pwd=data.get("pwd", "默认密码"),
http_port=data.get("http_port", 5000),
hello_port=data.get("hello_port", 5001),
)
def to_dict(self):
"""转换为字典"""
return {
"pwd": self.pwd,
"http_port": self.http_port,
"hello_port": self.hello_port,
}
class Server_Todo_Entry_Data:
name: str
ddl: str
def __init__(self, name:str="新条目", ddl:str=""):
self.name = name
self.ddl = ddl
@classmethod
def from_dict(cls, data:dict):
"""从字典创建实例"""
return cls(
name=data.get("name", "新条目"),
ddl=data.get("ddl", "")
)
def to_dict(self):
"""转换为字典"""
return {
"name": self.name,
"ddl": self.ddl
}
class Server_Todo_Tab_Data:
name: str
todo_entry_list: list[Server_Todo_Entry_Data]
def __init__(self, name:str="新条目", todo_entry_list:list[Server_Todo_Entry_Data]=None):
self.name = name
if todo_entry_list is not None:
self.todo_entry_list = todo_entry_list
else:
self.todo_entry_list = []
@classmethod
def from_dict(cls, data:dict):
"""从字典创建实例"""
entry_list = [Server_Todo_Entry_Data.from_dict(entry_data)
for entry_data in data.get("todo_entry_list", [])]
return cls(
name=data.get("name", "新条目"),
todo_entry_list=entry_list
)
def to_dict(self):
"""转换为字典"""
return {
"name": self.name,
"todo_entry_list": [entry.to_dict() for entry in self.todo_entry_list]
}
class Server_Todo_Data:
sync_time: datetime.datetime
todo_tab_list: list[Server_Todo_Tab_Data]
def __init__(self, sync_time:str="无时间", todo_tab_list:list[Server_Todo_Tab_Data]=None):
try:
self.sync_time = datetime.datetime.strptime(sync_time, "%Y-%m-%d %H:%M:%S")
except ValueError as e:
print(f"时间解析失败: {e}")
self.sync_time = datetime.datetime.now()
if todo_tab_list is not None:
self.todo_tab_list = todo_tab_list
else:
self.todo_tab_list = []
@classmethod
def from_dict(cls, data: dict):
"""从字典创建实例"""
tab_list = [Server_Todo_Tab_Data.from_dict(tab_data)
for tab_data in data.get("tab_list", [])]
return cls(
sync_time=data.get("sync_time", "无时间"),
todo_tab_list=tab_list
)
def get_sync_time(self):
return self.sync_time.strftime("%Y-%m-%d %H:%M:%S")
def update_sync_time(self):
print("更新时间")
self.sync_time = datetime.datetime.now()
self.save("./data/data.json")
return self.get_sync_time()
def get_tab_list(self):
return [tab.to_dict() for tab in self.todo_tab_list]
def update_tab_list(self, new_tab_list:list[dict]):
print("更新列表")
self.todo_tab_list = [Server_Todo_Tab_Data.from_dict(tab_data) for tab_data in new_tab_list]
self.update_sync_time()
def to_dict(self):
"""转换为字典"""
return {
"sync_time": self.sync_time.strftime("%Y-%m-%d %H:%M:%S"),
"todo_tab_list": [tab.to_dict() for tab in self.todo_tab_list]
}
def save(self, file_path:str):
print("保存数据")
try:
with open(file_path, 'w', encoding='utf-8') as file:
json.dump(self.to_dict(), file, ensure_ascii=False, indent=2)
print(f"数据已保存到 {file_path}")
except Exception as e:
print(f"保存文件时出错: {e}")
def load_todo_data(file_path:str) -> Server_Todo_Data:
print("加载数据")
try:
with open(file_path, 'r', encoding='utf-8') as file:
json_data = json.load(file)
print("读取成功:", json_data)
return Server_Todo_Data.from_dict(json_data)
except FileNotFoundError:
print("文件不存在")
except json.JSONDecodeError:
print("JSON格式错误")
except Exception as e:
print(f"读取文件时出错: {e}")
return Server_Todo_Data()
def load_meta_data(file_path:str) -> Server_Meta_Data:
print("加载元数据")
try:
with open(file_path, 'r', encoding='utf-8') as file:
json_data = json.load(file)
print("读取成功:", json_data)
return Server_Meta_Data.from_dict(json_data)
except FileNotFoundError:
print("文件不存在")
except json.JSONDecodeError:
print("JSON格式错误")
except Exception as e:
print(f"读取文件时出错: {e}")
return Server_Meta_Data()
_meta_data = load_meta_data("data/meta.json")
_todo_data = load_todo_data("data/data.json")
def get_hello_port():
return _meta_data.hello_port
def get_http_port():
return _meta_data.http_port
def get_todo_data() -> Server_Todo_Data:
return _todo_data