Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: only allow one of specific link types TDE-1298 #1182

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion scripts/stac/imagery/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,11 @@ def add_collection(self, collection_id: str) -> None:
self.add_link(Link(path="./collection.json", rel=Relation.PARENT, media_type=StacMediaType.JSON))

def add_link(self, link: Link) -> None:
self.stac["links"].append(link.stac)
if self.stac.get("links") and link.stac["rel"] in [
Relation.COLLECTION,
Relation.PARENT,
Relation.SELF,
]: # STAC specification prescribes there can be only one of these
self.stac["links"][:] = [l for l in self.stac["links"] if l.get("rel") != link.stac["rel"]]

self.stac.setdefault("links", []).append(link.stac)
11 changes: 7 additions & 4 deletions scripts/stac/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,29 @@ class Relation(str, Enum):
DERIVED_FROM = "derived_from"
""" https://github.com/radiantearth/stac-spec/blob/master/best-practices.md#derived-from-relation-derived_from"""

def __str__(self) -> str:
return self.value


# pylint: disable=too-few-public-methods
class Link:
"""Represents a STAC Link Object (https://github.com/radiantearth/stac-spec/blob/master/commons/links.md#link-object).

Attributes:
path: A string that represents the actual link in the format of an URL.
rel: A string that represents the relationship that the link has to the object it will be added to.
rel: `Relation` that represents the relationship that the link has to the object it will be added to.
media_type: `StacMediaType` of the link file.
file_content: Optional. The content of the file that will be used to store the checksum in `file:checksum`.
It assumes using the STAC `file` extension.
"""

stac: dict[str, str]

def __init__(self, path: str, rel: str, media_type: StacMediaType, file_content: bytes | None = None) -> None:
def __init__(self, path: str, rel: Relation, media_type: StacMediaType, file_content: bytes | None = None) -> None:
self.stac = {
"href": path,
"rel": rel,
"type": media_type,
"rel": str(rel),
"type": str(media_type),
}

if file_content:
Expand Down
3 changes: 3 additions & 0 deletions scripts/stac/util/media_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ class StacMediaType(str, Enum):

For STAC Item
"""

def __str__(self) -> str:
return self.value
Loading