ObjDocumentDelegate is an abstract base class for document type delegate handlers. Each delegate is responsible for generating icon and preview images for a specific category of file types (3D models, audio, spreadsheets, EPUB, web code, video, archives). Delegates encapsulate the domain-specific logic for processing different document types.
Module: factory.core/ObjDocumentDelegate.py
Inherits from: ObjData.ObjData, ABC (Abstract Base Class)
Test file: None (tested through concrete implementations)
This module enables developers to:
The generate_icon() method provides a standardized wrapper with:
Subclasses must implement _do_generate_icon(), enforcing a consistent interface while allowing domain-specific implementations.
Each concrete delegate focuses on a single document category:
Callers never see delegate errors - all exceptions are caught and logged by the template method.
class ObjDocumentDelegate(ObjData.ObjData, ABC):
_IsA: str = "ObjDocumentDelegate"
Inherits from both ObjData for database connectivity and ABC for abstract method enforcement.
_IsA: "ObjDocumentDelegate" - Type identifier for logging and debugging__init__(DB: int = 0) -> NoneInitialize the document delegate.
Parameters:
DB (optional): Database connection objectgenerate_icon(input_path: str, output_path: str, icon_size: int = 128) -> NoneGenerate a thumbnail icon image for the given file. Template method that logs entry, delegates to _do_generate_icon, and catches any exception.
Parameters:
input_path: Path to the source document fileoutput_path: Path where the icon image will be savedicon_size: Target square dimension in pixels (default 128)Behavior:
_IsA identifier_do_generate_icon()Example:
delegate = ObjDocument3D()
delegate.generate_icon(
input_path="/path/to/model.stl",
output_path="/path/to/model_icon.png",
icon_size=128
)
_do_generate_icon(input_path: str, output_path: str, icon_size: int) -> None (Abstract)Subclass hook to produce the icon image at output_path.
Must be implemented by subclasses.
Implementation Guidelines:
DocumentTools.resize_to_square(output_path, icon_size) as the final stepfrom ObjDocumentDelegate import ObjDocumentDelegate
from ObjDocumentTools import DocumentTools
class ObjDocumentCustom(ObjDocumentDelegate):
_IsA = "ObjDocumentCustom"
def _do_generate_icon(
self,
input_path: str,
output_path: str,
icon_size: int
) -> None:
# 1. Load and process the custom file format
data = self._load_custom_format(input_path)
# 2. Generate preview image
preview_image = self._render_preview(data)
preview_image.save(output_path)
# 3. Resize to square (required final step)
DocumentTools.resize_to_square(output_path, icon_size)
from ObjDocument3D import ObjDocument3D
# Create delegate instance
delegate = ObjDocument3D()
# Generate icon (template method handles errors)
delegate.generate_icon(
input_path="/documents/model.stl",
output_path="/documents/model_icon.png",
icon_size=128
)
# Icon is now available at output_path (or template method logged error)
Handles 3D model formats:
Icon Generation: Renders 3D model to 2D image using trimesh library.
Handles audio formats:
Icon Generation: Creates waveform visualization using matplotlib.
Handles spreadsheet formats:
Icon Generation: Renders first few rows/columns as table preview using pandas.
Handles electronic publication format:
Icon Generation: Extracts cover image from EPUB metadata.
Handles code and markup formats:
Icon Generation: Renders syntax-highlighted code snippet.
Handles video formats:
Icon Generation: Extracts frame from video using ffmpeg.
Handles archive formats:
Icon Generation: Lists archive contents as text preview.
┌────────────────────────────────────────┐
│ generate_icon() [Template Method] │
│ ───────────────────────────────────── │
│ 1. Log entry │
│ 2. Call _do_generate_icon() │
│ 3. Catch exceptions │
│ 4. Log errors │
└────────────────────────────────────────┘
│
▼
┌────────────────────────────────────────┐
│ _do_generate_icon() [Hook Method] │
│ ───────────────────────────────────── │
│ Implemented by concrete subclass │
│ Contains domain-specific logic │
└────────────────────────────────────────┘
Delegates typically use DocumentTools.resize_to_square() as the final processing step:
def _do_generate_icon(self, input_path, output_path, icon_size):
# Generate preview...
preview.save(output_path)
# Ensure square dimensions with transparent background
DocumentTools.resize_to_square(output_path, icon_size)
This ensures all icons have consistent dimensions regardless of source aspect ratio.
def generate_icon(self, input_path, output_path, icon_size=128):
try:
self.debug(f"Generating {self._IsA} icon from {input_path}")
self._do_generate_icon(input_path, output_path, icon_size)
except Exception as e:
self.debug(f"Error generating {self._IsA} icon: {e}")
Implications:
While not declared abstract, subclasses should also provide a generate_preview() method for larger preview images. Signatures may vary by document type.
Example:
class ObjDocument3D(ObjDocumentDelegate):
def generate_preview(
self,
input_path: str,
output_path: str,
preview_size: int = 512
) -> None:
# Similar to icon but larger
...
ObjDocumentTools.py - Shared utilities (resize_to_square)ObjDocumentFile.py - Document file managementObjDocument.py - Main document orchestrationObjEnum.py - FileType enum definitionsabc - Abstract base class supportObjData - Database connectivity and base object functionality