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

Add an AsIsLayoutStrategy #919

Merged
merged 2 commits into from
Jan 25, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Ability to only update resolved links when using `Catalog.normalize_hrefs` and `Catalog.normalize_and_save`, via a new `skip_unresolved` argument ([#900](https://github.com/stac-utils/pystac/pull/900))
- Add the optional argument `timespec` to `utils.datetime_to_str` ([#929](https://github.com/stac-utils/pystac/pull/929))
- `isort` ([#961](https://github.com/stac-utils/pystac/pull/961))
- `AsIsLayoutStrategy` ([#919](https://github.com/stac-utils/pystac/pull/919))
- `__geo_interface__` for items ([#885](https://github.com/stac-utils/pystac/pull/885))

### Removed
Expand Down
38 changes: 38 additions & 0 deletions pystac/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,3 +513,41 @@ def get_item_href(self, item: "Item_Type", parent_dir: str) -> str:
item_root = join_path_or_url(join_type, parent_dir, "{}".format(item.id))

return join_path_or_url(join_type, item_root, "{}.json".format(item.id))


class AsIsLayoutStrategy(HrefLayoutStrategy):
"""Layout strategy that simply preserves the current href of all objects.

If any object doesn't have a self href, a ValueError is raised.
"""

def get_catalog_href(
self, cat: "Catalog_Type", parent_dir: str, is_root: bool
) -> str:
href = cat.self_href
if href is None:
raise ValueError(
f"Catalog is missing href, required for AsIsLayoutStrategy: {cat}"
)
else:
return href

def get_collection_href(
self, col: "Collection_Type", parent_dir: str, is_root: bool
) -> str:
href = col.self_href
if href is None:
raise ValueError(
f"Collection is missing href, required for AsIsLayoutStrategy: {col}"
)
else:
return href

def get_item_href(self, item: "Item_Type", parent_dir: str) -> str:
href = item.self_href
if href is None:
raise ValueError(
f"Item is missing href, required for AsIsLayoutStrategy: {item}"
)
else:
return href
39 changes: 39 additions & 0 deletions tests/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pystac
from pystac.collection import Collection
from pystac.layout import (
AsIsLayoutStrategy,
BestPracticesLayoutStrategy,
CustomLayoutStrategy,
LayoutTemplate,
Expand Down Expand Up @@ -420,3 +421,41 @@ def test_produces_layout_for_item(self) -> None:
href = self.strategy.get_href(item, parent_dir="http://example.com")
expected = "http://example.com/{}/{}.json".format(item.id, item.id)
self.assertEqual(href, expected)


class AsIsLayoutStrategyTest(unittest.TestCase):
def setUp(self) -> None:
self.strategy = AsIsLayoutStrategy()

def test_catalog(self) -> None:
cat = pystac.Catalog(id="test", description="test desc")
with self.assertRaises(ValueError):
self.strategy.get_href(cat, parent_dir="http://example.com", is_root=True)
cat.set_self_href("/an/href")
href = self.strategy.get_href(
cat, parent_dir="https://example.com", is_root=True
)
self.assertEqual(href, "/an/href")

def test_collection(self) -> None:
collection = TestCases.case_8()
collection.set_self_href(None)
with self.assertRaises(ValueError):
self.strategy.get_href(
collection, parent_dir="http://example.com", is_root=True
)
collection.set_self_href("/an/href")
href = self.strategy.get_href(
collection, parent_dir="https://example.com", is_root=True
)
self.assertEqual(href, "/an/href")

def test_item(self) -> None:
collection = TestCases.case_8()
item = next(iter(collection.get_all_items()))
item.set_self_href(None)
with self.assertRaises(ValueError):
self.strategy.get_href(item, parent_dir="http://example.com")
item.set_self_href("/an/href")
href = self.strategy.get_href(item, parent_dir="http://example.com")
self.assertEqual(href, "/an/href")