76 lines
3.2 KiB
Python
76 lines
3.2 KiB
Python
# projectutility/core/models.py
|
|
|
|
import dataclasses
|
|
from typing import List, Dict, Any, Optional
|
|
|
|
|
|
# Note: frozen=True makes instances immutable after creation.
|
|
# This is generally good for data transfer objects like these,
|
|
# ensuring they are not accidentally modified after creation.
|
|
@dataclasses.dataclass(frozen=True)
|
|
class ToolParameter:
|
|
"""
|
|
Represents the definition of a parameter required by a tool,
|
|
parsed and ready for use (e.g., by the GUI to create widgets).
|
|
"""
|
|
|
|
name: str # Internal name/key (e.g., "input_file")
|
|
label: str # User-friendly label for the GUI (e.g., "Input PNG File:")
|
|
type: str # Data type hint (e.g., "string", "file", "folder", "integer", "boolean")
|
|
required: bool # Is this parameter mandatory for the tool?
|
|
default: Any = None # Default value (can be None). Type should match 'type'.
|
|
description: Optional[str] = None # Optional tooltip or help text shown in the GUI.
|
|
options: Optional[Dict[str, Any]] = None
|
|
# Optional dictionary for type-specific configurations.
|
|
# Example for "file" type: {'filter': [('PNG Images', '*.png')], 'save_as': False}
|
|
# Example for "string" type with choices: {'options': ['choice1', 'choice2']}
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class ToolInfo:
|
|
"""
|
|
Holds the processed and resolved information for a single discoverable tool,
|
|
ready to be consumed by the main application (e.g., GUI).
|
|
|
|
This object is typically created by the tool discovery process based on
|
|
registry entries and filesystem checks.
|
|
"""
|
|
|
|
id: str # Unique identifier from the registry (e.g., "icon_generator_git")
|
|
display_name: str # Name shown in the GUI tool list
|
|
description: str # Brief description shown in the GUI when selected
|
|
command: List[str] # The fully resolved command and arguments to execute.
|
|
# Paths within the command (like scripts) should be absolute.
|
|
working_dir: (
|
|
str # The absolute path to the directory where the command should execute.
|
|
)
|
|
parameters: List[ToolParameter] = dataclasses.field(default_factory=list)
|
|
# List of parsed ToolParameter objects required by this tool.
|
|
version: Optional[str] = None # Optional tool version string (from registry).
|
|
has_gui: bool = (
|
|
False # Does the tool have its own GUI (influences process creation)?
|
|
)
|
|
|
|
# Future enhancements could include:
|
|
# icon: Optional[str] = None # Path to an icon for the tool list
|
|
# category: Optional[str] = None # For organizing tools in the UI
|
|
|
|
def __post_init__(self):
|
|
# Basic validation after creation (optional but recommended)
|
|
if not self.id:
|
|
raise ValueError("ToolInfo 'id' cannot be empty.")
|
|
if not self.display_name:
|
|
raise ValueError(f"ToolInfo '{self.id}': 'display_name' cannot be empty.")
|
|
if not self.command:
|
|
raise ValueError(f"ToolInfo '{self.id}': 'command' list cannot be empty.")
|
|
if not self.working_dir or not os.path.isabs(
|
|
self.working_dir
|
|
): # Requires os import
|
|
raise ValueError(
|
|
f"ToolInfo '{self.id}': 'working_dir' must be a non-empty absolute path."
|
|
)
|
|
|
|
|
|
# Helper function needed for __post_init__ validation if uncommented
|
|
import os
|