Skip to content

Commit 8e8cb56

Browse files
committed
Implement #370 for File Info Extension
1 parent 9ddab16 commit 8e8cb56

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

pystac/extensions/file.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,13 @@ def ext(cls, obj: pystac.Asset) -> "FileExtension":
199199
200200
This extension can be applied to instances of :class:`~pystac.Asset`.
201201
"""
202-
return cls(obj)
202+
if isinstance(obj, pystac.Asset):
203+
cls.validate_has_extension(obj)
204+
return cls(obj)
205+
else:
206+
raise pystac.ExtensionTypeError(
207+
f"File Info extension does not apply to type {type(obj)}"
208+
)
203209

204210

205211
class FileExtensionHooks(ExtensionHooks):

tests/extensions/test_file.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def test_apply(self) -> None:
5050
class FileTest(unittest.TestCase):
5151
FILE_ITEM_EXAMPLE_URI = TestCases.get_path("data-files/file/item.json")
5252
FILE_COLLECTION_EXAMPLE_URI = TestCases.get_path("data-files/file/collection.json")
53+
PLAIN_ITEM = TestCases.get_path("data-files/item/sample-item.json")
5354

5455
def setUp(self) -> None:
5556
self.maxDiff = None
@@ -188,3 +189,25 @@ def test_migrates_old_checksum(self) -> None:
188189
FileExtension.ext(item.assets["noises"]).checksum,
189190
"90e40210a30d1711e81a4b11ef67b28744321659",
190191
)
192+
193+
def test_extension_type_error(self) -> None:
194+
item = pystac.Item.from_file(self.FILE_ITEM_EXAMPLE_URI)
195+
196+
with self.assertRaises(pystac.ExtensionTypeError):
197+
_ = FileExtension.ext(item) # type: ignore
198+
199+
def test_extension_not_implemented(self) -> None:
200+
# Should raise exception if Item does not include extension URI
201+
item = pystac.Item.from_file(self.PLAIN_ITEM)
202+
asset = item.assets["thumbnail"]
203+
204+
with self.assertRaises(pystac.ExtensionNotImplemented):
205+
_ = FileExtension.ext(asset)
206+
207+
# Should succeed if Asset has no owner
208+
stac_io = pystac.StacIO.default()
209+
item_dict = stac_io.read_json(self.FILE_ITEM_EXAMPLE_URI)
210+
asset_dict = item_dict["assets"]["measurement"]
211+
ownerless_asset = pystac.Asset.from_dict(asset_dict)
212+
213+
_ = FileExtension.ext(ownerless_asset)

0 commit comments

Comments
 (0)