From 3f1d697568d5eab11df9df246d371e945c52af7d Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 9 Sep 2025 11:24:21 +0300 Subject: [PATCH] Add dynamic asset path resolution for YTSage Introduced get_asset_path to resolve asset paths for both development and installed package environments. Updated ICON_PATH and SOUND_PATH to use this function, improving portability and reliability of asset loading. --- src/utils/ytsage_constants.py | 39 +++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/utils/ytsage_constants.py b/src/utils/ytsage_constants.py index 474b7ea..62a0d7e 100644 --- a/src/utils/ytsage_constants.py +++ b/src/utils/ytsage_constants.py @@ -16,11 +16,46 @@ YTSage application constants. import os import platform import subprocess +import sys from pathlib import Path +# Handle resource paths for both development and installed package +def get_asset_path(asset_relative_path: str) -> Path: + """ + Get the absolute path to an asset file, works both in development and installed package. + + Args: + asset_relative_path: Relative path to the asset (e.g., "assets/Icon/icon.png") + + Returns: + Path: Absolute path to the asset file + """ + try: + # Use importlib.resources (standard in Python 3.9+) + import importlib.resources as resources + try: + # Navigate to the package root and then to the asset + package_path = resources.files('ytsage') + asset_path = package_path / asset_relative_path + if asset_path.is_file(): + return Path(str(asset_path)) + except (ImportError, AttributeError, FileNotFoundError): + pass + + except Exception: + pass + + # Fallback to relative path (for development environment) + current_file = Path(__file__) + # Go up from src/utils to ytsage root, then to asset + ytsage_root = current_file.parent.parent.parent + asset_path = ytsage_root / asset_relative_path + + return asset_path + # Assets Constants -ICON_PATH: Path = Path("assets/Icon/icon.png") -SOUND_PATH: Path = Path("assets/sound/notification.mp3") +ICON_PATH: Path = get_asset_path("assets/Icon/icon.png") +SOUND_PATH: Path = get_asset_path("assets/sound/notification.mp3") OS_NAME: str = platform.system() # Windows ; Darwin ; Linux