From 7d8d2d42f85ad9f71fc6058f94efc94193d3dfa5 Mon Sep 17 00:00:00 2001 From: Duncan Tourolle Date: Tue, 11 Nov 2025 17:00:45 +0100 Subject: [PATCH] Use pythons tempdir --- pyPhotoAlbum/project.py | 19 ++++++++++--------- pyPhotoAlbum/project_serializer.py | 18 +++++++++--------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/pyPhotoAlbum/project.py b/pyPhotoAlbum/project.py index ef0b951..37ae6dd 100644 --- a/pyPhotoAlbum/project.py +++ b/pyPhotoAlbum/project.py @@ -119,9 +119,9 @@ class Project: # Embedded templates - templates that travel with the project self.embedded_templates: Dict[str, Dict[str, Any]] = {} - # Track if this project was loaded from a temp directory and should be cleaned up - self._is_temp_project = False - self._temp_cleanup_path = None + # Temporary directory management (if loaded from .ppz) + # Using TemporaryDirectory instance that auto-cleans on deletion + self._temp_dir = None # Initialize asset manager self.asset_manager = AssetManager(self.folder_path) @@ -370,14 +370,15 @@ class Project: Cleanup project resources, including temporary directories. Should be called when the project is closed or no longer needed. """ - if self._is_temp_project and self._temp_cleanup_path: + if self._temp_dir is not None: try: - import shutil - if os.path.exists(self._temp_cleanup_path): - shutil.rmtree(self._temp_cleanup_path) - print(f"Cleaned up temporary project directory: {self._temp_cleanup_path}") + # Let TemporaryDirectory clean itself up + temp_path = self._temp_dir.name + self._temp_dir.cleanup() + self._temp_dir = None + print(f"Cleaned up temporary project directory: {temp_path}") except Exception as e: - print(f"Warning: Failed to cleanup temporary directory {self._temp_cleanup_path}: {e}") + print(f"Warning: Failed to cleanup temporary directory: {e}") def __del__(self): """Destructor to ensure cleanup happens when project is deleted.""" diff --git a/pyPhotoAlbum/project_serializer.py b/pyPhotoAlbum/project_serializer.py index cd23afa..9a86a4e 100644 --- a/pyPhotoAlbum/project_serializer.py +++ b/pyPhotoAlbum/project_serializer.py @@ -135,15 +135,15 @@ def load_from_zip(zip_path: str, extract_to: Optional[str] = None) -> Tuple[Opti return None, f"ZIP file not found: {zip_path}" # Track if we created a temp directory - is_temp_extraction = False + temp_dir_obj = None # Determine extraction directory if extract_to is None: - # Extract to a temporary directory - # Use prefix to make it easier to identify temp project folders + # Create a temporary directory using TemporaryDirectory + # This will be attached to the Project and auto-cleaned on deletion zip_basename = os.path.splitext(os.path.basename(zip_path))[0] - extract_to = tempfile.mkdtemp(prefix=f"pyPhotoAlbum_{zip_basename}_") - is_temp_extraction = True + temp_dir_obj = tempfile.TemporaryDirectory(prefix=f"pyPhotoAlbum_{zip_basename}_") + extract_to = temp_dir_obj.name else: # Create extraction directory if it doesn't exist os.makedirs(extract_to, exist_ok=True) @@ -194,10 +194,10 @@ def load_from_zip(zip_path: str, extract_to: Optional[str] = None) -> Tuple[Opti project.asset_manager.project_folder = extract_to project.asset_manager.assets_folder = os.path.join(extract_to, "assets") - # Mark as temporary project if we extracted to a temp directory - if is_temp_extraction: - project._is_temp_project = True - project._temp_cleanup_path = extract_to + # Attach temporary directory to project (if we created one) + # The TemporaryDirectory will auto-cleanup when the project is deleted + if temp_dir_obj is not None: + project._temp_dir = temp_dir_obj print(f"Project loaded to temporary directory: {extract_to}") # Normalize asset paths in all ImageData elements