Skip to content

Commit 84a9f5b

Browse files
committed
Issue #573 avoid repeated warnings from get_bands_from_item_assets
e.g. when eo extension is not declared
1 parent 0448aff commit 84a9f5b

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

openeo/metadata.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import functools
34
import logging
45
import warnings
56
from typing import Any, Callable, Dict, List, NamedTuple, Optional, Set, Tuple, Union
@@ -637,7 +638,7 @@ def get_bands_from_eo_bands(self, eo_bands: List[Union[dict, pystac.extensions.e
637638
return [self._get_band_from_eo_bands_item(band) for band in eo_bands]
638639

639640
def _get_bands_from_item_asset(
640-
self, item_asset: pystac.extensions.item_assets.AssetDefinition
641+
self, item_asset: pystac.extensions.item_assets.AssetDefinition, *, _warn: Callable[[str], None] = _log.warning
641642
) -> Union[List[Band], None]:
642643
"""Get bands from a STAC 'item_assets' asset definition."""
643644
if _PYSTAC_1_9_EXTENSION_INTERFACE and item_asset.ext.has("eo"):
@@ -646,7 +647,7 @@ def _get_bands_from_item_asset(
646647
elif "eo:bands" in item_asset.properties:
647648
# TODO: skip this in strict mode?
648649
if _PYSTAC_1_9_EXTENSION_INTERFACE:
649-
_log.warning("Extracting band info from 'eo:bands' metadata, but 'eo' STAC extension was not declared.")
650+
_warn("Extracting band info from 'eo:bands' metadata, but 'eo' STAC extension was not declared.")
650651
return self.get_bands_from_eo_bands(item_asset.properties["eo:bands"])
651652

652653
def get_bands_from_item_assets(
@@ -662,8 +663,10 @@ def get_bands_from_item_assets(
662663
:param item_assets: a STAC `item_assets` mapping
663664
"""
664665
bands = set()
666+
# Trick to just warn once per collection
667+
_warn = functools.lru_cache()(_log.warning)
665668
for item_asset in item_assets.values():
666-
asset_bands = self._get_bands_from_item_asset(item_asset)
669+
asset_bands = self._get_bands_from_item_asset(item_asset, _warn=_warn)
667670
if asset_bands:
668671
bands.update(asset_bands)
669672
return bands

tests/test_metadata.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -867,5 +867,8 @@ def test_metadata_from_stac_collection_bands_from_item_assets(test_data, tmp_pat
867867
"vapour_pressure",
868868
]
869869

870-
if not eo_extension_is_declared:
871-
assert "Extracting band info from 'eo:bands' metadata, but 'eo' STAC extension was not declared." in caplog.text
870+
warn_count = sum(
871+
"Extracting band info from 'eo:bands' metadata, but 'eo' STAC extension was not declared." in m
872+
for m in caplog.messages
873+
)
874+
assert warn_count == (0 if eo_extension_is_declared else 1)

0 commit comments

Comments
 (0)