Skip to content

Commit 610e686

Browse files
committed
Separate method to validate Asset owner extensions
1 parent d7de312 commit 610e686

File tree

11 files changed

+48
-28
lines changed

11 files changed

+48
-28
lines changed

pystac/extensions/base.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
from abc import ABC, abstractmethod
2-
from typing import Generic, Iterable, List, Optional, Dict, Any, Type, TypeVar, Union
2+
from typing import (
3+
cast,
4+
Generic,
5+
Iterable,
6+
List,
7+
Optional,
8+
Dict,
9+
Any,
10+
Type,
11+
TypeVar,
12+
Union,
13+
)
314

415
import pystac
516

@@ -126,29 +137,40 @@ def has_extension(cls, obj: S) -> bool:
126137
)
127138

128139
@classmethod
129-
def validate_has_extension(cls, obj: Optional[S], add_if_missing: bool) -> None:
140+
def validate_owner_has_extension(
141+
cls, asset: pystac.Asset, add_if_missing: bool
142+
) -> None:
143+
"""Given an :class:`~pystac.Asset`, checks if the asset's owner has this
144+
extension's schema URI in its :attr:`~pystac.STACObject.stac_extensions` list.
145+
If ``add_if_missing`` is ``True``, the schema URI will be added to the owner.
146+
147+
Raises:
148+
STACError : If ``add_if_missing`` is ``True`` and ``asset.owner`` is
149+
``None``.
150+
"""
151+
if asset.owner is None:
152+
if add_if_missing:
153+
raise pystac.STACError(
154+
"Can only add schema URIs to Assets with an owner."
155+
)
156+
else:
157+
return
158+
return cls.validate_has_extension(cast(S, asset.owner), add_if_missing)
159+
160+
@classmethod
161+
def validate_has_extension(cls, obj: S, add_if_missing: bool) -> None:
130162
"""Given a :class:`~pystac.STACObject`, checks if the object has this
131-
extension's schema URI in it's :attr:`~pystac.STACObject.stac_extensions` list.
163+
extension's schema URI in its :attr:`~pystac.STACObject.stac_extensions` list.
132164
If ``add_if_missing`` is ``True``, the schema URI will be added to the object.
133165
134166
Args:
135167
obj : The object to validate.
136-
add_if_missing : Whether to add the schema URI to the object if the URI is
168+
add_if_missing : Whether to add the schema URI to the object if it is
137169
not already present.
138-
139-
Raises:
140-
STACError : If ``add_if_missing`` is ``True`` and ``extensible`` is None.
141170
"""
142171
if add_if_missing:
143-
if obj is None:
144-
raise pystac.STACError(
145-
"Can only add schema URIs to Assets with an owner."
146-
)
147172
cls.add_to(obj)
148173

149-
if obj is None:
150-
return
151-
152174
if cls.get_schema_uri() not in obj.stac_extensions:
153175
raise pystac.ExtensionNotImplemented(
154176
f"Could not find extension schema URI {cls.get_schema_uri()} in object."

pystac/extensions/datacube.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "DatacubeExtension[T]":
346346
cls.validate_has_extension(obj, add_if_missing)
347347
return cast(DatacubeExtension[T], ItemDatacubeExtension(obj))
348348
elif isinstance(obj, pystac.Asset):
349-
cls.validate_has_extension(obj.owner, add_if_missing)
349+
cls.validate_owner_has_extension(obj, add_if_missing)
350350
return cast(DatacubeExtension[T], AssetDatacubeExtension(obj))
351351
else:
352352
raise pystac.ExtensionTypeError(

pystac/extensions/eo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "EOExtension[T]":
364364
cls.validate_has_extension(obj, add_if_missing)
365365
return cast(EOExtension[T], ItemEOExtension(obj))
366366
elif isinstance(obj, pystac.Asset):
367-
cls.validate_has_extension(obj.owner, add_if_missing)
367+
cls.validate_owner_has_extension(obj, add_if_missing)
368368
return cast(EOExtension[T], AssetEOExtension(obj))
369369
else:
370370
raise pystac.ExtensionTypeError(

pystac/extensions/file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def ext(cls, obj: pystac.Asset, add_if_missing: bool = False) -> "FileExtension"
199199
This extension can be applied to instances of :class:`~pystac.Asset`.
200200
"""
201201
if isinstance(obj, pystac.Asset):
202-
cls.validate_has_extension(obj.owner, add_if_missing)
202+
cls.validate_owner_has_extension(obj, add_if_missing)
203203
return cls(obj)
204204
else:
205205
raise pystac.ExtensionTypeError(

pystac/extensions/pointcloud.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,10 +530,9 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "PointcloudExtension[T]":
530530
elif isinstance(obj, pystac.Asset):
531531
if obj.owner is not None and not isinstance(obj.owner, pystac.Item):
532532
raise pystac.ExtensionTypeError(
533-
"Pointcloud extension does not apply to Assets owned by anything "
534-
"other than an Item."
533+
"Pointcloud extension does not apply to Collection Assets."
535534
)
536-
cls.validate_has_extension(obj.owner, add_if_missing)
535+
cls.validate_owner_has_extension(obj, add_if_missing)
537536
return cast(PointcloudExtension[T], AssetPointcloudExtension(obj))
538537
else:
539538
raise pystac.ExtensionTypeError(

pystac/extensions/projection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "ProjectionExtension[T]":
275275
cls.validate_has_extension(obj, add_if_missing)
276276
return cast(ProjectionExtension[T], ItemProjectionExtension(obj))
277277
elif isinstance(obj, pystac.Asset):
278-
cls.validate_has_extension(obj.owner, add_if_missing)
278+
cls.validate_owner_has_extension(obj, add_if_missing)
279279
return cast(ProjectionExtension[T], AssetProjectionExtension(obj))
280280
else:
281281
raise pystac.ExtensionTypeError(

pystac/extensions/raster.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ def ext(cls, obj: pystac.Asset, add_if_missing: bool = False) -> "RasterExtensio
704704
pystac.ExtensionTypeError : If an invalid object type is passed.
705705
"""
706706
if isinstance(obj, pystac.Asset):
707-
cls.validate_has_extension(obj.owner, add_if_missing)
707+
cls.validate_owner_has_extension(obj, add_if_missing)
708708
return cls(obj)
709709
else:
710710
raise pystac.ExtensionTypeError(

pystac/extensions/sar.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,9 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "SarExtension[T]":
320320
elif isinstance(obj, pystac.Asset):
321321
if obj.owner is not None and not isinstance(obj.owner, pystac.Item):
322322
raise pystac.ExtensionTypeError(
323-
"SAR extension does not apply to Assets owned by anything "
324-
"other than an Item."
323+
"SAR extension does not apply to Collection Assets."
325324
)
326-
cls.validate_has_extension(obj.owner, add_if_missing)
325+
cls.validate_owner_has_extension(obj, add_if_missing)
327326
return cast(SarExtension[T], AssetSarExtension(obj))
328327
else:
329328
raise pystac.ExtensionTypeError(

pystac/extensions/sat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "SatExtension[T]":
152152
cls.validate_has_extension(obj, add_if_missing)
153153
return cast(SatExtension[T], ItemSatExtension(obj))
154154
elif isinstance(obj, pystac.Asset):
155-
cls.validate_has_extension(obj.owner, add_if_missing)
155+
cls.validate_owner_has_extension(obj, add_if_missing)
156156
return cast(SatExtension[T], AssetSatExtension(obj))
157157
else:
158158
raise pystac.ExtensionTypeError(

pystac/extensions/timestamps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> "TimestampsExtension[T]":
132132
cls.validate_has_extension(obj, add_if_missing)
133133
return cast(TimestampsExtension[T], ItemTimestampsExtension(obj))
134134
elif isinstance(obj, pystac.Asset):
135-
cls.validate_has_extension(obj.owner, add_if_missing)
135+
cls.validate_owner_has_extension(obj, add_if_missing)
136136
return cast(TimestampsExtension[T], AssetTimestampsExtension(obj))
137137
else:
138138
raise pystac.ExtensionTypeError(

0 commit comments

Comments
 (0)