Refactor
This commit is contained in:
+11
-6
@@ -1,7 +1,8 @@
|
||||
import os
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict
|
||||
from PIL import Image
|
||||
from tools.utils import ToolError
|
||||
|
||||
logger = logging.getLogger("MattCP")
|
||||
|
||||
@@ -10,15 +11,17 @@ async def handle(args: Dict[str, Any]):
|
||||
Extracts text-based metadata chunks (tEXt, zTXt, iTXt) from a PNG file.
|
||||
These often contain AI generation prompts and parameters.
|
||||
"""
|
||||
path = args.get("path")
|
||||
if not path:
|
||||
return {"text": "Error: Missing path argument"}
|
||||
path_str = args.get("path")
|
||||
if not path_str:
|
||||
raise ToolError("Missing path argument")
|
||||
|
||||
path = Path(path_str)
|
||||
|
||||
try:
|
||||
with Image.open(path) as img:
|
||||
# Metadata extraction is specific to PNG format in this implementation
|
||||
if img.format != 'PNG':
|
||||
return {"text": "Error: File is not a PNG image."}
|
||||
raise ToolError("File is not a PNG image.")
|
||||
|
||||
# Pillow parses PNG text chunks into the .info dictionary
|
||||
metadata = img.info
|
||||
@@ -28,5 +31,7 @@ async def handle(args: Dict[str, Any]):
|
||||
# Format as a simple key: value list
|
||||
output = "\n".join([f"{k}: {v}" for k, v in metadata.items()])
|
||||
return {"text": f"PNG Metadata:\n{output}"}
|
||||
except ToolError:
|
||||
raise
|
||||
except Exception as e:
|
||||
return {"text": f"Error reading PNG metadata: {str(e)}"}
|
||||
raise ToolError(f"Error reading PNG metadata: {str(e)}")
|
||||
|
||||
Reference in New Issue
Block a user