云端粗出

This commit is contained in:
root
2025-07-12 17:07:14 +08:00
parent bb14fba3fa
commit 5f1fc7d9c8
9 changed files with 659 additions and 165 deletions

View File

@@ -13,7 +13,6 @@ from dataclasses import dataclass, asdict
from datetime import datetime
from ..utils.logger import setup_logger
from ..utils.jsonrpc import create_response_handler, create_progress_reporter, JSONRPCError
from ..config import settings
logger = setup_logger(__name__)
@@ -548,132 +547,3 @@ class TemplateManager:
except Exception as e:
logger.error(f"Failed to get template detail for {template_id}: {e}")
return None
def main():
"""Command line interface for template management."""
import argparse
import json
import sys
parser = argparse.ArgumentParser(description="Template Manager")
parser.add_argument("--action", required=True, help="Action to perform")
parser.add_argument("--source_folder", help="Source folder for batch import")
parser.add_argument("--template_id", help="Template ID for operations")
args = parser.parse_args()
# Create JSON-RPC response handler
rpc = create_response_handler("template_manager")
progress = create_progress_reporter()
try:
manager = TemplateManager()
if args.action == "batch_import":
if not args.source_folder:
rpc.error(JSONRPCError.INVALID_PARAMS, "Source folder is required for batch import")
return
def progress_callback(message):
# Parse progress information from message
if "Processing template" in message:
# Extract template info from message like "Processing template 1/3: Template_Name"
parts = message.split(":")
if len(parts) >= 2:
template_name = parts[1].strip()
# Extract progress numbers
if "/" in parts[0]:
progress_part = parts[0].split("Processing template")[1].strip()
if "/" in progress_part:
current, total = progress_part.split("/")
try:
current_num = int(current.strip())
total_num = int(total.strip())
progress_percent = (current_num / total_num) * 100
progress.report(
step="import",
progress=progress_percent,
message=message,
details={
"current_template": template_name,
"total_templates": total_num,
"processed_templates": current_num
}
)
return
except ValueError:
pass
# Default progress step
progress.step("import", message)
result = manager.batch_import_templates(args.source_folder, progress_callback)
# Convert TemplateInfo objects to dictionaries for JSON serialization
if 'imported_templates' in result:
result['imported_templates'] = [asdict(template) for template in result['imported_templates']]
# Send successful result via JSON-RPC
rpc.success(result)
elif args.action == "get_templates":
templates = manager.get_templates()
result = {
"status": True,
"templates": [asdict(template) for template in templates]
}
rpc.success(result)
elif args.action == "get_template":
if not args.template_id:
rpc.error(JSONRPCError.INVALID_PARAMS, "Template ID is required")
return
template = manager.get_template(args.template_id)
if template:
result = {
"status": True,
"template": asdict(template)
}
rpc.success(result)
else:
rpc.error(JSONRPCError.TEMPLATE_NOT_FOUND, f"Template not found: {args.template_id}")
elif args.action == "delete_template":
if not args.template_id:
rpc.error(JSONRPCError.INVALID_PARAMS, "Template ID is required")
return
success = manager.delete_template(args.template_id)
if success:
result = {
"status": True,
"msg": "Template deleted successfully"
}
rpc.success(result)
else:
rpc.error(JSONRPCError.TEMPLATE_NOT_FOUND, "Failed to delete template")
elif args.action == "get_template_detail":
if not args.template_id:
rpc.error(JSONRPCError.INVALID_PARAMS, "Template ID is required")
return
detail = manager.get_template_detail(args.template_id)
if detail:
rpc.success(detail)
else:
rpc.error(JSONRPCError.TEMPLATE_NOT_FOUND, f"Template detail not found: {args.template_id}")
else:
rpc.error(JSONRPCError.METHOD_NOT_FOUND, f"Unknown action: {args.action}")
except Exception as e:
# Send error via JSON-RPC
rpc.error(JSONRPCError.INTERNAL_ERROR, f"Template manager error: {str(e)}", str(e))
sys.exit(1)
if __name__ == "__main__":
main()