Skip to content

Commit

Permalink
refactor: only allow one of specific link types TDE-1298 (#1182)
Browse files Browse the repository at this point in the history
### Motivation

Refactoring to support TDE-1298 - only allow one link of each of the
types `Collection`, `Parent`, `Self`.

### Modifications
Added logic in `ImageryItem`s `add_link` method to recognise when a link
of each type already exists.
Added `__str__` method to `StacMediaType` and `Relation` types, and made
sure the `Link` class will contain strings for `rel` and `type`.

### Verification

`pytest`
  • Loading branch information
schmidtnz authored Nov 21, 2024
1 parent 4bf9fba commit 119a2cd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
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

0 comments on commit 119a2cd

Please sign in to comment.