Skip to content

Fix paths to saved sub-catalogs #714

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

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- Use `PropertiesExtension._get_property` to properly set return type in `TableExtension` ([#712](https://github.com/stac-utils/pystac/pull/712))
- `DatacubeExtension.variables` now has a setter ([#699](https://github.com/stac-utils/pystac/pull/699)])
- Landsat STAC tutorial is now up-to-date with all package changes ([#692](https://github.com/stac-utils/pystac/pull/674))
- Paths to sub-catalog files when using `Catalog.save` ([#714](https://github.com/stac-utils/pystac/pull/714))

### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion pystac/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ def save(
child_dest_href = make_absolute_href(
rel_href, dest_href, start_is_dir=True
)
child.save(dest_href=child_dest_href)
child.save(dest_href=os.path.dirname(child_dest_href))
else:
child.save()

Expand Down
65 changes: 64 additions & 1 deletion tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
HIERARCHICAL_LINKS,
)
from pystac.extensions.label import LabelClasses, LabelExtension, LabelType
from pystac.utils import is_absolute_href, join_path_or_url, JoinType
from pystac.utils import (
is_absolute_href,
join_path_or_url,
JoinType,
make_absolute_href,
make_relative_href,
)
from tests.utils import (
TestCases,
ARBITRARY_GEOM,
Expand Down Expand Up @@ -332,6 +338,63 @@ def test_save_to_provided_href(self) -> None:
for link in result_cat.get_child_links():
self.assertTrue(cast(str, link.target).startswith(href))

def test_subcatalogs_saved_to_correct_path(self) -> None:
with tempfile.TemporaryDirectory() as tmp_dir:
catalog = TestCases.test_case_1()
href = "http://test.com"

catalog.normalize_hrefs(href)
catalog.save(catalog_type=CatalogType.ABSOLUTE_PUBLISHED, dest_href=tmp_dir)

# Check the root catalog path
expected_root_catalog_path = os.path.join(tmp_dir, "catalog.json")
self.assertTrue(
os.path.exists(expected_root_catalog_path),
msg=f"{expected_root_catalog_path} does not exist.",
)
self.assertTrue(
os.path.isfile(expected_root_catalog_path),
msg=f"{expected_root_catalog_path} is not a file.",
)

# Check each child catalog
for child_catalog in catalog.get_children():
relative_path = make_relative_href(
child_catalog.self_href, catalog.self_href, start_is_dir=False
)
expected_child_path = make_absolute_href(
relative_path,
expected_root_catalog_path,
start_is_dir=False,
)
self.assertTrue(
os.path.exists(expected_child_path),
msg=f"{expected_child_path} does not exist.",
)
self.assertTrue(
os.path.isfile(expected_child_path),
msg=f"{expected_child_path} is not a file.",
)

# Check each item
for item in catalog.get_all_items():
relative_path = make_relative_href(
item.self_href, catalog.self_href, start_is_dir=False
)
expected_item_path = make_absolute_href(
relative_path,
expected_root_catalog_path,
start_is_dir=False,
)
self.assertTrue(
os.path.exists(expected_item_path),
msg=f"{expected_item_path} does not exist.",
)
self.assertTrue(
os.path.isfile(expected_item_path),
msg=f"{expected_item_path} is not a file.",
)

def test_clone_uses_previous_catalog_type(self) -> None:
catalog = TestCases.test_case_1()
assert catalog.catalog_type == CatalogType.SELF_CONTAINED
Expand Down