diff --git a/src/Mod/CAM/Path/Tool/assets/manager.py b/src/Mod/CAM/Path/Tool/assets/manager.py index e61539179d..3b02677f0b 100644 --- a/src/Mod/CAM/Path/Tool/assets/manager.py +++ b/src/Mod/CAM/Path/Tool/assets/manager.py @@ -528,10 +528,25 @@ class AssetManager: """ Returns True if the asset exists, False otherwise. """ + async def _exists_async(): + asset_uri_obj = AssetUri(uri) if isinstance(uri, str) else uri + logger.debug( + f"ExistsAsync (internal): Looking up store '{store}'. Available stores: {list(self.stores.keys())}" + ) + try: + selected_store = self.stores[store] + except KeyError: + raise ValueError(f"No store registered for name: {store}") + return await selected_store.exists(asset_uri_obj) + try: - return self.get_raw(uri, store) is not None - except FileNotFoundError: - return False + return asyncio.run(_exists_async()) + except Exception as e: + logger.error( + f"AssetManager.exists: Error during asyncio.run for '{uri}': {e}", + exc_info=False, + ) + raise def fetch( self, diff --git a/src/Mod/CAM/Path/Tool/assets/store/base.py b/src/Mod/CAM/Path/Tool/assets/store/base.py index 3722d08839..b93d463c0d 100644 --- a/src/Mod/CAM/Path/Tool/assets/store/base.py +++ b/src/Mod/CAM/Path/Tool/assets/store/base.py @@ -53,6 +53,22 @@ class AssetStore(abc.ABC): """ raise NotImplementedError + async def exists(self, uri: AssetUri) -> bool: + """ + Check if the asset exists at the given URI. + + Args: + uri: The unique identifier for the asset. + + Returns: + True if the asset exists, False otherwise. + """ + try: + await self.get(uri) + return True + except FileNotFoundError: + return False + @abc.abstractmethod async def delete(self, uri: AssetUri) -> None: """ diff --git a/src/Mod/CAM/Path/Tool/assets/store/filestore.py b/src/Mod/CAM/Path/Tool/assets/store/filestore.py index 753d8d9cac..a8bb246c5c 100644 --- a/src/Mod/CAM/Path/Tool/assets/store/filestore.py +++ b/src/Mod/CAM/Path/Tool/assets/store/filestore.py @@ -266,6 +266,17 @@ class FileStore(AssetStore): except IsADirectoryError: raise FileNotFoundError(f"Asset URI {uri} resolved to a directory: {path_to_read}") + async def exists(self, uri: AssetUri) -> bool: + """Check if the asset exists at the given URI.""" + try: + await self.get(uri) + return True + except FileNotFoundError: + return False + except IsADirectoryError: + # If the path is a directory, it means the asset exists but is not a file. + return False + async def delete(self, uri: AssetUri) -> None: """Delete the asset at the given URI.""" paths_to_delete: List[pathlib.Path] = [] diff --git a/src/Mod/CAM/Path/Tool/assets/store/memory.py b/src/Mod/CAM/Path/Tool/assets/store/memory.py index 6410b5acef..2dc4712914 100644 --- a/src/Mod/CAM/Path/Tool/assets/store/memory.py +++ b/src/Mod/CAM/Path/Tool/assets/store/memory.py @@ -52,6 +52,17 @@ class MemoryStore(AssetStore): return self._data[asset_type][asset_id][version] + async def exists(self, uri: AssetUri) -> bool: + asset_type = uri.asset_type + asset_id = uri.asset_id + version = uri.version or self._get_latest_version(asset_type, asset_id) + + return ( + asset_type in self._data + and asset_id in self._data[asset_type] + and version in self._data[asset_type][asset_id] + ) + async def delete(self, uri: AssetUri) -> None: asset_type = uri.asset_type asset_id = uri.asset_id