CAM: Add explicit implementation of FileStore.exists(), so AssetManager.exists() does not log FileNotFound warnings

This commit is contained in:
Samuel Abels
2025-05-20 00:40:48 +02:00
parent 5502ab750b
commit c8b53ed015
4 changed files with 56 additions and 3 deletions

View File

@@ -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,

View File

@@ -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:
"""

View File

@@ -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] = []

View File

@@ -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