Skip to content

Commit

Permalink
--wip-- [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Oct 13, 2023
1 parent e456289 commit 37282b3
Show file tree
Hide file tree
Showing 24 changed files with 954 additions and 1,218 deletions.
4 changes: 4 additions & 0 deletions pystac/extensions/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ def table(self) -> TableExtension[T]:
def view(self) -> ViewExtension[T]:
return ViewExtension.ext(self.stac_object)

@property
def version(self) -> VersionExtension[T]:
return VersionExtension.ext(self.stac_object)


@dataclass
class AssetExt(_AssetExt[pystac.Asset]):
Expand Down
48 changes: 41 additions & 7 deletions pystac/extensions/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
from pystac.errors import DeprecatedWarning
from pystac.extensions.base import ExtensionManagementMixin, PropertiesExtension
from pystac.extensions.hooks import ExtensionHooks
from pystac.utils import StringEnum, get_required, map_opt
from pystac.utils import StringEnum, map_opt

T = TypeVar("T", pystac.Collection, pystac.Item)
T = TypeVar("T", pystac.Collection, pystac.Item, pystac.Asset)

SCHEMA_URI = "https://stac-extensions.github.io/version/v1.0.0/schema.json"
SCHEMA_URI = "https://stac-extensions.github.io/version/v1.2.0/schema.json"

# STAC fields - These are unusual for an extension in that they do not have
# a prefix. e.g. nothing like "ver:"
Expand Down Expand Up @@ -106,14 +106,14 @@ def apply(
self.successor = successor

@property
def version(self) -> str:
def version(self) -> str | None:
"""Get or sets a version string of the :class:`~pystac.Item` or
:class:`pystac.Collection`."""
return get_required(self._get_property(VERSION, str), self, VERSION)
return self._get_property(VERSION, str)

@version.setter
def version(self, v: str) -> None:
self._set_property(VERSION, v, pop_if_none=False)
self._set_property(VERSION, v, pop_if_none=True)

@property
def deprecated(self) -> bool | None:
Expand Down Expand Up @@ -205,6 +205,14 @@ def successor(self, item_or_collection: T | None) -> None:
)
)

@property
def experimental(self) -> bool | None:
return self._get_property("experimental", bool)

@experimental.setter
def experimental(self, experimental: bool) -> None:
self._set_property("experimental", experimental, pop_if_none=True)

@classmethod
def get_schema_uri(cls) -> str:
return SCHEMA_URI
Expand All @@ -224,9 +232,12 @@ def ext(cls, obj: T, add_if_missing: bool = False) -> VersionExtension[T]:
if isinstance(obj, pystac.Collection):
cls.ensure_has_extension(obj, add_if_missing)
return cast(VersionExtension[T], CollectionVersionExtension(obj))
if isinstance(obj, pystac.Item):
elif isinstance(obj, pystac.Item):
cls.ensure_has_extension(obj, add_if_missing)
return cast(VersionExtension[T], ItemVersionExtension(obj))
elif isinstance(obj, pystac.Asset):
cls.ensure_owner_has_extension(obj, add_if_missing)
return cast(VersionExtension[T], AssetVersionExtension(obj))
else:
raise pystac.ExtensionTypeError(cls._ext_error_message(obj))

Expand Down Expand Up @@ -278,6 +289,29 @@ def __repr__(self) -> str:
return f"<ItemVersionExtension Item id={self.item.id}>"


class AssetVersionExtension(VersionExtension[pystac.Asset]):
"""A concrete implementation of :class:`VersionExtension` on an
:class:`~pystac.Asset` that extends the fields of the Asset to include properties
defined in the :stac-ext:`Versioning Indicators Extension <version>`.
This class should generally not be instantiated directly. Instead, call
:meth:`VersionExtension.ext` on an :class:`~pystac.Asset` to extend it.
"""

properties: dict[str, Any]

def __init__(self, asset: pystac.Asset):
self.asset = asset
self.properties = asset.extra_fields
super().__init__(asset)

def __repr__(self) -> str:
return (
f"<AssetVersionExtension Asset in item id={self.asset.owner.id} and "
f"href={self.asset.href}>"
)


class VersionExtensionHooks(ExtensionHooks):
schema_uri = SCHEMA_URI
prev_extension_ids = {"version"}
Expand Down
4 changes: 2 additions & 2 deletions tests/data-files/version/collection.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"stac_version": "1.0.0-rc.1",
"stac_version": "1.0.0",
"stac_extensions": [
"https://stac-extensions.github.io/version/v1.0.0/schema.json"
"https://stac-extensions.github.io/version/v1.2.0/schema.json"
],
"id": "merraclim",
"type": "Collection",
Expand Down
4 changes: 2 additions & 2 deletions tests/data-files/version/item.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"stac_version": "1.0.0-rc.1",
"stac_version": "1.0.0",
"stac_extensions": [
"https://stac-extensions.github.io/version/v1.0.0/schema.json"
"https://stac-extensions.github.io/version/v1.2.0/schema.json"
],
"id": "MERRAclim.2_5m_min_80s",
"type": "Feature",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,94 +9,75 @@ interactions:
User-Agent:
- Python-urllib/3.11
method: GET
uri: https://stac-extensions.github.io/version/v1.0.0/schema.json
uri: https://stac-extensions.github.io/version/v1.2.0/schema.json
response:
body:
string: "{\n \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n \"$id\":
\"https://stac-extensions.github.io/version/v1.0.0/schema.json#\",\n \"title\":
\"https://stac-extensions.github.io/version/v1.2.0/schema.json#\",\n \"title\":
\"Versioning Indicators Extension\",\n \"description\": \"STAC Versioning
Indicators Extension for STAC Items and STAC Collections.\",\n \"oneOf\":
[\n {\n \"$comment\": \"This is the schema for STAC Items. Remove
this object if this extension only applies to Collections.\",\n \"allOf\":
[\n {\n \"$comment\": \"This is the schema for STAC Items.\",\n \"allOf\":
[\n {\n \"$ref\": \"#/definitions/stac_extensions\"\n },\n
\ {\n \"type\": \"object\",\n \"required\": [\n \"type\",\n
\ \"properties\",\n \"assets\"\n ],\n \"properties\":
{\n \"type\": {\n \"const\": \"Feature\"\n },\n
\ \"properties\": {\n \"allOf\": [\n {\n
\ \"$comment\": \"Require fields here for item properties.\",\n
\ \"required\": [\n \"version\"\n ]\n
\ },\n {\n \"$ref\": \"#/definitions/fields\"\n
\ }\n ]\n },\n \"assets\":
{\n \"$comment\": \"This validates the fields in Item Assets,
but does not require them.\",\n \"type\": \"object\",\n \"additionalProperties\":
\ \"properties\": {\n \"$ref\": \"#/definitions/fields\"\n
\ },\n \"assets\": {\n \"type\": \"object\",\n
\ \"additionalProperties\": {\n \"$ref\": \"#/definitions/fields\"\n
\ }\n }\n }\n }\n ]\n },\n
\ {\n \"$comment\": \"This is the schema for STAC Collections.\",\n
\ \"type\": \"object\",\n \"allOf\": [\n {\n \"required\":
[\n \"type\"\n ],\n \"properties\": {\n \"type\":
{\n \"const\": \"Collection\"\n }\n }\n },\n
\ {\n \"$ref\": \"#/definitions/stac_extensions\"\n },\n
\ {\n \"$comment\": \"This is the schema for the top-level
fields in a Collection.\",\n \"allOf\": [\n {\n \"$ref\":
\"#/definitions/fields\"\n }\n ]\n },\n {\n
\ \"$comment\": \"This validates the fields in Collection Assets.\",\n
\ \"properties\": {\n \"assets\": {\n \"type\":
\"object\",\n \"additionalProperties\": {\n \"$ref\":
\"#/definitions/fields\"\n }\n }\n }\n },\n
\ {\n \"$comment\": \"This is the schema for the fields in
Item Asset Definitions.\",\n \"properties\": {\n \"item_assets\":
{\n \"type\": \"object\",\n \"additionalProperties\":
{\n \"$ref\": \"#/definitions/fields\"\n }\n }\n
\ }\n }\n ]\n },\n {\n \"$comment\": \"This
is the schema for STAC Collections.\",\n \"type\": \"object\",\n \"allOf\":
[\n {\n \"required\": [\n \"type\"\n ],\n
is the schema for STAC Catalog.\",\n \"allOf\": [\n {\n \"type\":
\"object\",\n \"required\": [\n \"type\"\n ],\n
\ \"properties\": {\n \"type\": {\n \"const\":
\"Collection\"\n }\n }\n },\n {\n \"$ref\":
\"#/definitions/stac_extensions\"\n }\n ],\n \"anyOf\": [\n
\ {\n \"$comment\": \"This is the schema for the top-level
fields in a Collection. Remove this if this extension does not define top-level
fields for Collections.\",\n \"allOf\": [\n {\n \"$comment\":
\"Require fields here for Collections (top-level).\",\n \"required\":
[\n \"version\"\n ]\n },\n {\n
\ \"$ref\": \"#/definitions/fields\"\n }\n ]\n
\ },\n {\n \"$comment\": \"This validates the fields
in Collection Assets, but does not require them.\",\n \"required\":
[\n \"assets\"\n ],\n \"properties\": {\n \"assets\":
{\n \"type\": \"object\",\n \"not\": {\n \"additionalProperties\":
{\n \"not\": {\n \"allOf\": [\n {\n
\ \"$ref\": \"#/definitions/require_any_field\"\n },\n
\ {\n \"$ref\": \"#/definitions/fields\"\n
\ }\n ]\n }\n }\n
\ }\n }\n }\n },\n {\n \"$comment\":
\"This is the schema for the fields in Item Asset Definitions. It doesn't
require any fields.\",\n \"required\": [\n \"item_assets\"\n
\ ],\n \"properties\": {\n \"item_assets\": {\n
\ \"type\": \"object\",\n \"not\": {\n \"additionalProperties\":
{\n \"not\": {\n \"allOf\": [\n {\n
\ \"$ref\": \"#/definitions/require_any_field\"\n },\n
\ {\n \"$ref\": \"#/definitions/fields\"\n
\ }\n ]\n }\n }\n
\ }\n }\n }\n },\n {\n \"$comment\":
\"This is the schema for the fields in Summaries. By default, only checks
the existance of the properties, but not the schema of the summaries.\",\n
\ \"required\": [\n \"summaries\"\n ],\n \"properties\":
{\n \"summaries\": {\n \"$ref\": \"#/definitions/require_any_field\"\n
\ }\n }\n }\n ]\n }\n ],\n \"definitions\":
\"Catalog\"\n }\n }\n },\n {\n \"$ref\":
\"#/definitions/stac_extensions\"\n },\n {\n \"$ref\":
\"#/definitions/fields\"\n }\n ]\n }\n ],\n \"definitions\":
{\n \"stac_extensions\": {\n \"type\": \"object\",\n \"required\":
[\n \"stac_extensions\"\n ],\n \"properties\": {\n \"stac_extensions\":
{\n \"type\": \"array\",\n \"contains\": {\n \"const\":
\"https://stac-extensions.github.io/version/v1.0.0/schema.json\"\n }\n
\ }\n }\n },\n \"require_any_field\": {\n \"$comment\":
\"Please list all fields here so that we can force the existance of one of
them in other parts of the schemas.\",\n \"anyOf\": [\n {\"required\":
[\"version\"]},\n {\"required\": [\"deprecated\"]}\n ]\n },\n
\ \"fields\": {\n \"type\": \"object\",\n \"properties\": {\n
\ \"version\": {\n \"type\": \"string\",\n \"title\":
\"Version\"\n },\n \"deprecated\": {\n \"type\": \"boolean\",\n
\ \"title\": \"Deprecated\",\n \"default\": false\n }\n
\"https://stac-extensions.github.io/version/v1.2.0/schema.json\"\n }\n
\ }\n }\n },\n \"fields\": {\n \"type\": \"object\",\n
\ \"properties\": {\n \"version\": {\n \"type\": \"string\",\n
\ \"title\": \"Version\"\n },\n \"deprecated\": {\n
\ \"type\": \"boolean\",\n \"title\": \"Deprecated\",\n \"default\":
false\n },\n \"experimental\": {\n \"type\": \"boolean\",\n
\ \"title\": \"Experimental\",\n \"default\": false\n }\n
\ }\n }\n }\n}\n"
headers:
Accept-Ranges:
- bytes
Access-Control-Allow-Origin:
- '*'
Age:
- '517'
- '443'
Cache-Control:
- max-age=600
Connection:
- close
Content-Length:
- '5009'
- '3405'
Content-Type:
- application/json; charset=utf-8
Date:
- Wed, 27 Sep 2023 21:20:50 GMT
- Fri, 13 Oct 2023 13:39:13 GMT
ETag:
- '"645249bd-1391"'
- '"645249bd-d4d"'
Last-Modified:
- Wed, 03 May 2023 11:47:09 GMT
Server:
Expand All @@ -110,17 +91,17 @@ interactions:
X-Cache:
- HIT
X-Cache-Hits:
- '2'
- '1'
X-Fastly-Request-ID:
- 8b9fffd9434fd81ec501ce6f8c825aa774cc06e7
- 8ba7479ba4057d23836c2d204c3baeda68d2722f
X-GitHub-Request-Id:
- 96AE:1981:28905C:3865AF:65149AAC
- B200:2C1D:709A53:995830:6529359A
X-Served-By:
- cache-lga21962-LGA
- cache-iad-kjyo7100036-IAD
X-Timer:
- S1695849651.615996,VS0,VE1
- S1697204353.008353,VS0,VE1
expires:
- Wed, 27 Sep 2023 21:22:13 GMT
- Fri, 13 Oct 2023 12:28:35 GMT
permissions-policy:
- interest-cohort=()
x-proxy-cache:
Expand Down
Loading

0 comments on commit 37282b3

Please sign in to comment.