Skip to content

Commit 971c881

Browse files
committed
Implement add_if_missing on ext method
1 parent cea40f3 commit 971c881

28 files changed

+424
-15
lines changed

pystac/extensions/datacube.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,20 @@ def get_schema_uri(cls) -> str:
338338
return SCHEMA_URI
339339

340340
@classmethod
341-
def ext(cls, obj: T) -> "DatacubeExtension[T]":
341+
def ext(cls, obj: T, add_if_missing: bool = False) -> "DatacubeExtension[T]":
342342
if isinstance(obj, pystac.Collection):
343+
if add_if_missing:
344+
cls.add_to(obj)
343345
cls.validate_has_extension(obj)
344346
return cast(DatacubeExtension[T], CollectionDatacubeExtension(obj))
345347
if isinstance(obj, pystac.Item):
348+
if add_if_missing:
349+
cls.add_to(obj)
346350
cls.validate_has_extension(obj)
347351
return cast(DatacubeExtension[T], ItemDatacubeExtension(obj))
348352
elif isinstance(obj, pystac.Asset):
353+
if add_if_missing and obj.owner is not None:
354+
cls.add_to(obj.owner)
349355
cls.validate_has_extension(obj)
350356
return cast(DatacubeExtension[T], AssetDatacubeExtension(obj))
351357
else:

pystac/extensions/eo.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def get_schema_uri(cls) -> str:
347347
return SCHEMA_URI
348348

349349
@classmethod
350-
def ext(cls, obj: T) -> "EOExtension[T]":
350+
def ext(cls, obj: T, add_if_missing: bool = False) -> "EOExtension[T]":
351351
"""Extends the given STAC Object with properties from the :stac-ext:`Electro-Optical
352352
Extension <eo>`.
353353
@@ -359,9 +359,13 @@ def ext(cls, obj: T) -> "EOExtension[T]":
359359
pystac.ExtensionTypeError : If an invalid object type is passed.
360360
"""
361361
if isinstance(obj, pystac.Item):
362+
if add_if_missing:
363+
cls.add_to(obj)
362364
cls.validate_has_extension(obj)
363365
return cast(EOExtension[T], ItemEOExtension(obj))
364366
elif isinstance(obj, pystac.Asset):
367+
if add_if_missing and isinstance(obj.owner, pystac.Item):
368+
cls.add_to(obj.owner)
365369
cls.validate_has_extension(obj)
366370
return cast(EOExtension[T], AssetEOExtension(obj))
367371
else:

pystac/extensions/file.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,15 @@ def get_schema_uri(cls) -> str:
193193
return SCHEMA_URI
194194

195195
@classmethod
196-
def ext(cls, obj: pystac.Asset) -> "FileExtension":
196+
def ext(cls, obj: pystac.Asset, add_if_missing: bool = False) -> "FileExtension":
197197
"""Extends the given STAC Object with properties from the :stac-ext:`File Info
198198
Extension <file>`.
199199
200200
This extension can be applied to instances of :class:`~pystac.Asset`.
201201
"""
202202
if isinstance(obj, pystac.Asset):
203+
if add_if_missing and isinstance(obj.owner, pystac.Item):
204+
cls.add_to(obj.owner)
203205
cls.validate_has_extension(obj)
204206
return cls(obj)
205207
else:

pystac/extensions/item_assets.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,17 @@ def get_schema_uri(cls) -> str:
117117
return SCHEMA_URI
118118

119119
@classmethod
120-
def ext(cls, collection: pystac.Collection) -> "ItemAssetsExtension":
121-
return cls(collection)
120+
def ext(
121+
cls, obj: pystac.Collection, add_if_missing: bool = False
122+
) -> "ItemAssetsExtension":
123+
if isinstance(obj, pystac.Collection):
124+
if add_if_missing:
125+
cls.add_to(obj)
126+
return cls(obj)
127+
else:
128+
raise pystac.ExtensionTypeError(
129+
f"Item Assets extension does not apply to type {type(obj)}"
130+
)
122131

123132

124133
class ItemAssetsExtensionHooks(ExtensionHooks):

pystac/extensions/label.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,13 +639,15 @@ def get_schema_uri(cls) -> str:
639639
return SCHEMA_URI
640640

641641
@classmethod
642-
def ext(cls, obj: pystac.Item) -> "LabelExtension":
642+
def ext(cls, obj: pystac.Item, add_if_missing: bool = False) -> "LabelExtension":
643643
"""Extends the given STAC Object with properties from the :stac-ext:`Label
644644
Extension <label>`.
645645
646646
This extension can be applied to instances of :class:`~pystac.Item`.
647647
"""
648648
if isinstance(obj, pystac.Item):
649+
if add_if_missing:
650+
cls.add_to(obj)
649651
cls.validate_has_extension(obj)
650652
return cls(obj)
651653
else:

pystac/extensions/pointcloud.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,11 +523,15 @@ def get_schema_uri(cls) -> str:
523523
return SCHEMA_URI
524524

525525
@classmethod
526-
def ext(cls, obj: T) -> "PointcloudExtension[T]":
526+
def ext(cls, obj: T, add_if_missing: bool = False) -> "PointcloudExtension[T]":
527527
if isinstance(obj, pystac.Item):
528+
if add_if_missing:
529+
cls.add_to(obj)
528530
cls.validate_has_extension(obj)
529531
return cast(PointcloudExtension[T], ItemPointcloudExtension(obj))
530532
elif isinstance(obj, pystac.Asset):
533+
if add_if_missing and isinstance(obj.owner, pystac.Item):
534+
cls.add_to(obj.owner)
531535
cls.validate_has_extension(obj)
532536
return cast(PointcloudExtension[T], AssetPointcloudExtension(obj))
533537
else:

pystac/extensions/projection.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def get_schema_uri(cls) -> str:
238238
return SCHEMA_URI
239239

240240
@classmethod
241-
def ext(cls, obj: T) -> "ProjectionExtension[T]":
241+
def ext(cls, obj: T, add_if_missing: bool = False) -> "ProjectionExtension[T]":
242242
"""Extends the given STAC Object with properties from the :stac-ext:`Projection
243243
Extension <projection>`.
244244
@@ -250,9 +250,13 @@ def ext(cls, obj: T) -> "ProjectionExtension[T]":
250250
pystac.ExtensionTypeError : If an invalid object type is passed.
251251
"""
252252
if isinstance(obj, pystac.Item):
253+
if add_if_missing:
254+
cls.add_to(obj)
253255
cls.validate_has_extension(obj)
254256
return cast(ProjectionExtension[T], ItemProjectionExtension(obj))
255257
elif isinstance(obj, pystac.Asset):
258+
if add_if_missing and isinstance(obj.owner, pystac.Item):
259+
cls.add_to(obj.owner)
256260
cls.validate_has_extension(obj)
257261
return cast(ProjectionExtension[T], AssetProjectionExtension(obj))
258262
else:

pystac/extensions/raster.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ def get_schema_uri(cls) -> str:
689689
return SCHEMA_URI
690690

691691
@classmethod
692-
def ext(cls, obj: pystac.Asset) -> "RasterExtension":
692+
def ext(cls, obj: pystac.Asset, add_if_missing: bool = False) -> "RasterExtension":
693693
"""Extends the given STAC Object with properties from the :stac-ext:`Raster
694694
Extension <raster>`.
695695
@@ -700,6 +700,8 @@ def ext(cls, obj: pystac.Asset) -> "RasterExtension":
700700
pystac.ExtensionTypeError : If an invalid object type is passed.
701701
"""
702702
if isinstance(obj, pystac.Asset):
703+
if add_if_missing and isinstance(obj.owner, pystac.Item):
704+
cls.add_to(obj.owner)
703705
cls.validate_has_extension(obj)
704706
return cls(obj)
705707
else:

pystac/extensions/sar.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,15 @@ def get_schema_uri(cls) -> str:
305305
return SCHEMA_URI
306306

307307
@classmethod
308-
def ext(cls, obj: T) -> "SarExtension[T]":
308+
def ext(cls, obj: T, add_if_missing: bool = False) -> "SarExtension[T]":
309309
if isinstance(obj, pystac.Item):
310+
if add_if_missing:
311+
cls.add_to(obj)
310312
cls.validate_has_extension(obj)
311313
return cast(SarExtension[T], ItemSarExtension(obj))
312314
elif isinstance(obj, pystac.Asset):
315+
if add_if_missing and isinstance(obj.owner, pystac.Item):
316+
cls.add_to(obj.owner)
313317
cls.validate_has_extension(obj)
314318
return cast(SarExtension[T], AssetSarExtension(obj))
315319
else:

pystac/extensions/sat.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,15 @@ def get_schema_uri(cls) -> str:
9696
return SCHEMA_URI
9797

9898
@classmethod
99-
def ext(cls, obj: T) -> "SatExtension[T]":
99+
def ext(cls, obj: T, add_if_missing: bool = False) -> "SatExtension[T]":
100100
if isinstance(obj, pystac.Item):
101+
if add_if_missing:
102+
cls.add_to(obj)
101103
cls.validate_has_extension(obj)
102104
return cast(SatExtension[T], ItemSatExtension(obj))
103105
elif isinstance(obj, pystac.Asset):
106+
if add_if_missing and isinstance(obj.owner, pystac.Item):
107+
cls.add_to(obj.owner)
104108
cls.validate_has_extension(obj)
105109
return cast(SatExtension[T], AssetSatExtension(obj))
106110
else:

0 commit comments

Comments
 (0)