-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_manager.py
More file actions
229 lines (187 loc) · 7.36 KB
/
Copy pathtemplate_manager.py
File metadata and controls
229 lines (187 loc) · 7.36 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
昭通洪水可视化集成程序 - 模板管理模块
提供模板创建、保存、加载和管理功能
主要功能:
1. 模板创建:根据当前参数创建计算模板
2. 模板保存:将模板保存到文件系统
3. 模板加载:从文件系统加载模板
4. 模板管理:查看、编辑、删除模板
5. 模板导出/导入:支持模板的导出和导入
版本信息:
- 版本:v1.0.0
- 更新日期:2024-05-29
"""
import os
import json
import shutil
from datetime import datetime
from typing import Dict, List, Any
# 模板目录
TEMPLATE_DIR = "templates"
# 确保模板目录存在
if not os.path.exists(TEMPLATE_DIR):
os.makedirs(TEMPLATE_DIR)
class TemplateManager:
"""
模板管理类
负责模板的创建、保存、加载和管理
"""
def __init__(self):
"""初始化模板管理器"""
self.templates: Dict[str, Dict[str, Any]] = {}
self.load_templates()
def load_templates(self) -> None:
"""加载所有模板"""
self.templates.clear()
try:
for filename in os.listdir(TEMPLATE_DIR):
if filename.endswith(".json"):
filepath = os.path.join(TEMPLATE_DIR, filename)
with open(filepath, "r", encoding="utf-8") as f:
template = json.load(f)
template["filename"] = filename
self.templates[filename] = template
except Exception as e:
print(f"加载模板失败:{e}")
def get_templates(self) -> List[Dict[str, Any]]:
"""获取所有模板列表"""
return list(self.templates.values())
def get_template(self, filename: str) -> Dict[str, Any]:
"""获取指定模板"""
return self.templates.get(filename, {})
def create_template(self, template_name: str, param_type: str, params: Dict[str, Any]) -> bool:
"""
创建模板
:param template_name: 模板名称
:param param_type: 参数类型(design_flood, flood_level, backwater_analysis等)
:param params: 模板参数
:return: 创建是否成功
"""
try:
# 创建模板数据
template = {
"name": template_name,
"type": param_type,
"params": params,
"created_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"updated_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
# 生成文件名
filename = f"{template_name}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
filepath = os.path.join(TEMPLATE_DIR, filename)
# 保存模板到文件
with open(filepath, "w", encoding="utf-8") as f:
json.dump(template, f, ensure_ascii=False, indent=2)
# 重新加载模板
self.load_templates()
return True
except Exception as e:
print(f"创建模板失败:{e}")
return False
def save_template(self, filename: str, template: Dict[str, Any]) -> bool:
"""
保存模板
:param filename: 模板文件名
:param template: 模板数据
:return: 保存是否成功
"""
try:
# 更新模板的更新时间
template["updated_at"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# 保存模板到文件
filepath = os.path.join(TEMPLATE_DIR, filename)
with open(filepath, "w", encoding="utf-8") as f:
json.dump(template, f, ensure_ascii=False, indent=2)
# 重新加载模板
self.load_templates()
return True
except Exception as e:
print(f"保存模板失败:{e}")
return False
def delete_template(self, filename: str) -> bool:
"""
删除模板
:param filename: 模板文件名
:return: 删除是否成功
"""
try:
filepath = os.path.join(TEMPLATE_DIR, filename)
if os.path.exists(filepath):
os.remove(filepath)
# 重新加载模板
self.load_templates()
return True
return False
except Exception as e:
print(f"删除模板失败:{e}")
return False
def export_template(self, filename: str, export_path: str) -> bool:
"""
导出模板
:param filename: 模板文件名
:param export_path: 导出路径
:return: 导出是否成功
"""
try:
if filename not in self.templates:
return False
template = self.templates[filename]
with open(export_path, "w", encoding="utf-8") as f:
json.dump(template, f, ensure_ascii=False, indent=2)
return True
except Exception as e:
print(f"导出模板失败:{e}")
return False
def import_template(self, import_path: str) -> bool:
"""
导入模板
:param import_path: 导入路径
:return: 导入是否成功
"""
try:
with open(import_path, "r", encoding="utf-8") as f:
template = json.load(f)
# 生成新的文件名
template_name = template.get("name", "imported_template")
filename = f"{template_name}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
filepath = os.path.join(TEMPLATE_DIR, filename)
# 保存模板到文件
with open(filepath, "w", encoding="utf-8") as f:
json.dump(template, f, ensure_ascii=False, indent=2)
# 重新加载模板
self.load_templates()
return True
except Exception as e:
print(f"导入模板失败:{e}")
return False
def update_template(self, filename: str, template_name: str, params: Dict[str, Any]) -> bool:
"""
更新模板
:param filename: 模板文件名
:param template_name: 新的模板名称
:param params: 新的模板参数
:return: 更新是否成功
"""
try:
if filename not in self.templates:
return False
# 获取原模板
template = self.templates[filename].copy()
# 更新模板数据
template["name"] = template_name
template["params"] = params
template["updated_at"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# 保存模板到文件
filepath = os.path.join(TEMPLATE_DIR, filename)
with open(filepath, "w", encoding="utf-8") as f:
json.dump(template, f, ensure_ascii=False, indent=2)
# 重新加载模板
self.load_templates()
return True
except Exception as e:
print(f"更新模板失败:{e}")
return False
# 创建全局模板管理器实例
template_manager = TemplateManager()