Skip to content
Snippets Groups Projects
Commit 4e877729 authored by Cresson Remi's avatar Cresson Remi
Browse files

Merge branch 'support_collections' into 'main'

enh: support collections

See merge request !5
parents 0e98a1fd 7ca5b000
No related branches found
No related tags found
1 merge request!5enh: support collections
Pipeline #243016 passed with warnings
from .core import create_extension_cls
__version__ = "0.0.21"
\ No newline at end of file
__version__ = "0.0.22"
\ No newline at end of file
......@@ -44,7 +44,7 @@ def create_extension_cls(
def __init__(self, obj: T):
if isinstance(obj, pystac.Item):
self.properties = obj.properties
elif isinstance(obj, pystac.Asset):
elif isinstance(obj, (pystac.Asset, pystac.Collection)):
self.properties = obj.extra_fields
else:
raise pystac.ExtensionTypeError(
......@@ -109,12 +109,12 @@ def create_extension_cls(
) -> model_cls.__name__:
if isinstance(obj, pystac.Item):
cls.ensure_has_extension(obj, add_if_missing)
return cast(CustomExtension[T],
ItemCustomExtension(obj))
return cast(CustomExtension[T], ItemCustomExtension(obj))
elif isinstance(obj, pystac.Asset):
cls.ensure_owner_has_extension(obj, add_if_missing)
return cast(CustomExtension[T],
AssetCustomExtension(obj))
return cast(CustomExtension[T], AssetCustomExtension(obj))
elif isinstance(obj, pystac.Collection):
return cast(CustomExtension[T], CollectionCustomExtension(obj))
raise pystac.ExtensionTypeError(
f"{model_cls.__name__} does not apply to type "
f"{type(obj).__name__}"
......@@ -134,5 +134,12 @@ def create_extension_cls(
if asset.owner and isinstance(asset.owner, pystac.Item):
self.additional_read_properties = [asset.owner.properties]
class CollectionCustomExtension(CustomExtension[pystac.Collection]):
properties: dict[str, Any]
additional_read_properties: Iterable[dict[str, Any]] | None = None
def __init__(self, collection: pystac.Collection):
self.properties = collection.extra_fields
CustomExtension.__name__ = f"CustomExtensionFrom{model_cls.__name__}"
return CustomExtension
......@@ -50,7 +50,7 @@ def create_dummy_item(date=None):
)
col.add_item(item)
return item
return item, col
def basic_test(
......@@ -58,6 +58,7 @@ def basic_test(
ext_cls,
item_test: bool = True,
asset_test: bool = True,
collection_test: bool = True,
validate: bool = True
):
print(
......@@ -92,7 +93,7 @@ def basic_test(
"""
Test extension against item
"""
item = create_dummy_item()
item, _ = create_dummy_item()
apply(item)
print_item(item)
if validate:
......@@ -104,7 +105,7 @@ def basic_test(
"""
Test extension against asset
"""
item = create_dummy_item()
item, _ = create_dummy_item()
apply(item.assets["ndvi"])
print_item(item)
if validate:
......@@ -112,12 +113,28 @@ def basic_test(
# Check that we can retrieve the extension metadata from the asset
comp(item.assets["ndvi"])
def test_collection():
"""
Test extension against collection
"""
item, col = create_dummy_item()
print_item(col)
apply(col)
print_item(col)
if validate:
col.validate() # <--- This will try to read the actual schema URI
# Check that we can retrieve the extension metadata from the asset
comp(col)
if item_test:
print("Test item")
test_item()
if asset_test:
print("Test asset")
test_asset()
if collection_test:
print("Test collection")
test_collection()
def is_schema_url_synced(cls):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment