Skip to content

Commit e07214e

Browse files
pjonssonalexgleith
authored andcommitted
utils: handle not having a self-href
The current code in odc-stac will happily return a self-href that is None, so handle that situation without immediately crashing with a UnboundLocalError in item_to_meta_uri.
1 parent 269aa28 commit e07214e

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

apps/dc_tools/odc/apps/dc_tools/s3_to_dc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,13 @@ def dump_to_odc(
148148
dataset["properties"][prop] = "false"
149149
if transform:
150150
item = Item.from_dict(dataset)
151-
dataset, uri, stac = item_to_meta_uri(
151+
dataset, new_uri, stac = item_to_meta_uri(
152152
item,
153153
dc,
154154
rename_product=rename_product,
155155
url_string_replace=url_string_replace,
156156
)
157+
uri = new_uri or uri
157158
try:
158159
index_update_dataset(
159160
dataset,

apps/dc_tools/odc/apps/dc_tools/stac_api_to_dc.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ def process_item(
7878
**kwargs,
7979
) -> None:
8080
dataset, uri, stac = item_to_meta_uri(item, dc, rename_product, url_string_replace)
81+
if uri is None:
82+
raise ValueError(
83+
f"The links field did not contain a self-reference for item {item}"
84+
)
8185
doc2ds = Doc2Dataset(dc.index, **kwargs)
8286
index_update_dataset(
8387
dataset,

apps/dc_tools/odc/apps/dc_tools/utils.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,29 +301,34 @@ def statsd_gauge_reporting(value, tags=None, statsd_setting="localhost:8125") ->
301301
statsd.gauge("datacube_index", value, tags=tags)
302302

303303

304-
def item_to_meta_uri(
305-
item: Item,
306-
dc: Datacube,
307-
rename_product: Optional[str] = None,
308-
url_string_replace: tuple[str, str] | None = None,
309-
) -> Tuple[Dataset, str, Dict[str, Any]]:
304+
def get_self_link(item: Item) -> str | None:
305+
uri = None
310306
for link in item.links:
311307
if link.rel == "self":
312308
uri = link.target
313309

314310
# Override self with canonical
315311
if link.rel == "canonical":
316-
uri = link.target
317-
break
312+
return link.target
313+
return uri
318314

315+
316+
def item_to_meta_uri(
317+
item: Item,
318+
dc: Datacube,
319+
rename_product: Optional[str] = None,
320+
url_string_replace: tuple[str, str] | None = None,
321+
) -> Tuple[Dataset, str | None, Dict[str, Any]]:
319322
if rename_product is not None:
320323
item.properties["odc:product"] = rename_product
321324

322325
# If we need to modify URLs, do it for the main URL and the asset links
326+
uri = get_self_link(item)
323327
if url_string_replace is not None:
324328
old_url, new_url = url_string_replace
325329

326-
uri = uri.replace(old_url, new_url)
330+
if uri is not None:
331+
uri = uri.replace(old_url, new_url)
327332

328333
for asset in item.assets.values():
329334
asset.href = asset.href.replace(old_url, new_url)

0 commit comments

Comments
 (0)