Skip to content

Commit

Permalink
Get link title from target, when possible (#607)
Browse files Browse the repository at this point in the history
* Get link title from target, when possible

* Update CHANGELOG for #607

* Add tests for serialization and title in init
  • Loading branch information
Jon Duckworth authored Aug 23, 2021
1 parent 6cbdd88 commit 98edeaa
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Enable [strict
mode](https://mypy.readthedocs.io/en/latest/command_line.html?highlight=strict%20mode#cmdoption-mypy-strict)
for `mypy` ([#591](https://github.com/stac-utils/pystac/pull/591))
- Links will get their `title` from their target if no `title` is provided ([#607](https://github.com/stac-utils/pystac/pull/607))

### Fixed

Expand Down
20 changes: 17 additions & 3 deletions pystac/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ class Link:
"""Optional description of the media type. Registered Media Types are preferred.
See :class:`~pystac.MediaType` for common media types."""

title: Optional[str]
"""Optional title for this link."""

extra_fields: Dict[str, Any]
"""Optional, additional fields for this link. This is used by extensions as a
way to serialize and deserialize properties on link object JSON."""
Expand All @@ -70,6 +67,7 @@ class Link:

_target_href: Optional[str]
_target_object: Optional["STACObject_Type"]
_title: Optional[str]

def __init__(
self,
Expand Down Expand Up @@ -103,6 +101,22 @@ def set_owner(self, owner: Optional["STACObject_Type"]) -> "Link":
self.owner = owner
return self

@property
def title(self) -> Optional[str]:
"""Optional title for this link. If not provided during instantiation, this will
attempt to get the title from the STAC object that the link references."""
if self._title is not None:
return self._title
if self._target_object is not None and isinstance(
self._target_object, pystac.Catalog
):
return self._target_object.title
return None

@title.setter
def title(self, v: Optional[str]) -> None:
self._title = v

@property
def href(self) -> str:
"""Returns the HREF for this link.
Expand Down
56 changes: 56 additions & 0 deletions tests/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,62 @@ def test_relative_self_href(self) -> None:
finally:
os.chdir(previous)

def test_auto_title_when_resolved(self) -> None:
extent = pystac.Extent.from_items([self.item])
collection = pystac.Collection(
id="my_collection",
description="Test Collection",
extent=extent,
title="Collection Title",
)
link = pystac.Link("my rel", target=collection)

self.assertEqual(collection.title, link.title)

def test_auto_title_not_found(self) -> None:
extent = pystac.Extent.from_items([self.item])
collection = pystac.Collection(
id="my_collection",
description="Test Collection",
extent=extent,
)
link = pystac.Link("my rel", target=collection)

self.assertEqual(None, link.title)

def test_auto_title_is_serialized(self) -> None:
extent = pystac.Extent.from_items([self.item])
collection = pystac.Collection(
id="my_collection",
description="Test Collection",
extent=extent,
title="Collection Title",
)
link = pystac.Link("my rel", target=collection)

assert link.to_dict().get("title") == collection.title

def test_no_auto_title_if_not_resolved(self) -> None:
link = pystac.Link(
"my rel", target="https://www.some-domain.com/path/to/thing.txt"
)

assert link.title is None

def test_title_as_init_argument(self) -> None:
link_title = "Link title"
extent = pystac.Extent.from_items([self.item])
collection = pystac.Collection(
id="my_collection",
description="Test Collection",
extent=extent,
title="Collection Title",
)
link = pystac.Link("my rel", title=link_title, target=collection)

assert link.title == link_title
assert link.to_dict().get("title") == link_title


class StaticLinkTest(unittest.TestCase):
def setUp(self) -> None:
Expand Down

0 comments on commit 98edeaa

Please sign in to comment.