166 lines
4.1 KiB
Python
166 lines
4.1 KiB
Python
"""AMT 数据库管理脚本 - 交互式管理 Tool 表"""
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
from peewee import SqliteDatabase, Model, CharField, TextField
|
|
|
|
db_path = os.path.join(os.path.dirname(__file__), "data.db")
|
|
db = SqliteDatabase(db_path)
|
|
|
|
|
|
class BaseModel(Model):
|
|
class Meta:
|
|
database = db
|
|
|
|
|
|
class Tool(BaseModel):
|
|
code = CharField(max_length=4, unique=True)
|
|
step = TextField()
|
|
desp = TextField(default="")
|
|
|
|
|
|
def init_db():
|
|
db.connect()
|
|
db.create_tables([Tool])
|
|
|
|
|
|
def list_tools():
|
|
tools = Tool.select().order_by(Tool.code)
|
|
if not tools:
|
|
print("\n (空)")
|
|
return
|
|
print()
|
|
for t in tools:
|
|
print(f" [{t.code}] {t.desp or '(无描述)'}")
|
|
|
|
|
|
def show_tool():
|
|
code = input("输入 code: ").strip()
|
|
tool = Tool.get_or_none(Tool.code == code)
|
|
if tool is None:
|
|
print(f" code {code} 不存在")
|
|
return
|
|
print(f"\n code: {tool.code}")
|
|
print(f" desp: {tool.desp or '(无描述)'}")
|
|
print(f" step:")
|
|
print(json.dumps(json.loads(tool.step), indent=2, ensure_ascii=False))
|
|
|
|
|
|
def add_tool():
|
|
code = input("输入 code (4位数字): ").strip()
|
|
if len(code) != 4 or not code.isdigit():
|
|
print(" code 必须是4位数字")
|
|
return
|
|
if Tool.get_or_none(Tool.code == code):
|
|
print(f" code {code} 已存在,请用编辑功能修改")
|
|
return
|
|
|
|
desp = input("输入描述: ").strip()
|
|
print("输入 step JSON (输入空行结束):")
|
|
lines = []
|
|
while True:
|
|
line = input()
|
|
if line == "":
|
|
break
|
|
lines.append(line)
|
|
|
|
raw = "\n".join(lines)
|
|
try:
|
|
parsed = json.loads(raw)
|
|
except json.JSONDecodeError as e:
|
|
print(f" JSON 解析失败: {e}")
|
|
return
|
|
|
|
Tool.create(code=code, step=json.dumps(parsed, ensure_ascii=False), desp=desp)
|
|
print(f" 已添加 [{code}]")
|
|
|
|
|
|
def edit_tool():
|
|
code = input("输入要编辑的 code: ").strip()
|
|
tool = Tool.get_or_none(Tool.code == code)
|
|
if tool is None:
|
|
print(f" code {code} 不存在")
|
|
return
|
|
|
|
print(f" 当前描述: {tool.desp or '(无描述)'}")
|
|
desp = input("新描述 (回车跳过): ").strip()
|
|
if desp:
|
|
tool.desp = desp
|
|
|
|
print(f" 当前 step:")
|
|
print(json.dumps(json.loads(tool.step), indent=2, ensure_ascii=False))
|
|
choice = input("修改 step? (y/N): ").strip().lower()
|
|
if choice == "y":
|
|
print("输入新的 step JSON (输入空行结束):")
|
|
lines = []
|
|
while True:
|
|
line = input()
|
|
if line == "":
|
|
break
|
|
lines.append(line)
|
|
raw = "\n".join(lines)
|
|
try:
|
|
parsed = json.loads(raw)
|
|
except json.JSONDecodeError as e:
|
|
print(f" JSON 解析失败: {e}")
|
|
return
|
|
tool.step = json.dumps(parsed, ensure_ascii=False)
|
|
|
|
tool.save()
|
|
print(f" 已更新 [{code}]")
|
|
|
|
|
|
def delete_tool():
|
|
code = input("输入要删除的 code: ").strip()
|
|
tool = Tool.get_or_none(Tool.code == code)
|
|
if tool is None:
|
|
print(f" code {code} 不存在")
|
|
return
|
|
|
|
print(f" [{tool.code}] {tool.desp or '(无描述)'}")
|
|
confirm = input("确认删除? (y/N): ").strip().lower()
|
|
if confirm == "y":
|
|
tool.delete_instance()
|
|
print(f" 已删除 [{code}]")
|
|
else:
|
|
print(" 已取消")
|
|
|
|
|
|
MENU = {
|
|
"1": ("列出所有", list_tools),
|
|
"2": ("查看详情", show_tool),
|
|
"3": ("添加", add_tool),
|
|
"4": ("编辑", edit_tool),
|
|
"5": ("删除", delete_tool),
|
|
}
|
|
|
|
|
|
def main():
|
|
init_db()
|
|
print("=== AMT 数据库管理 ===")
|
|
while True:
|
|
print()
|
|
for k, (label, _) in MENU.items():
|
|
print(f" {k}. {label}")
|
|
print(" q. 退出")
|
|
|
|
choice = input("\n> ").strip().lower()
|
|
if choice == "q":
|
|
break
|
|
action = MENU.get(choice)
|
|
if action:
|
|
try:
|
|
action[1]()
|
|
except (KeyboardInterrupt, EOFError):
|
|
print()
|
|
else:
|
|
print(" 无效选项")
|
|
|
|
db.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|