Skip to content

Apply StacIO instance from root in Catalog.set_root #590

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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
- 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))
- `Catalog.set_root` also sets `Catalog.stac_io` for the calling instance to be the same
as `root.stac_io`, if that value is not `None`
([#590](https://github.com/stac-utils/pystac/pull/590))
- 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
10 changes: 6 additions & 4 deletions pystac/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CatalogType(str, Enum):

See:
:stac-spec:`The best practices documentation on self-contained catalogs
<best-practices.md#self-contained-catalogs>`
<best-practices.md#self-contained-catalogs>`
"""

ABSOLUTE_PUBLISHED = "ABSOLUTE_PUBLISHED"
Expand All @@ -55,7 +55,7 @@ class CatalogType(str, Enum):

See:
:stac-spec:`The best practices documentation on published catalogs
<best-practices.md#published-catalogs>`
<best-practices.md#published-catalogs>`
"""

RELATIVE_PUBLISHED = "RELATIVE_PUBLISHED"
Expand All @@ -65,7 +65,7 @@ class CatalogType(str, Enum):

See:
:stac-spec:`The best practices documentation on published catalogs
<best-practices.md#published-catalogs>`
<best-practices.md#published-catalogs>`
"""

@classmethod
Expand Down Expand Up @@ -181,11 +181,13 @@ def __repr__(self) -> str:
return "<Catalog id={}>".format(self.id)

def set_root(self, root: Optional["Catalog"]) -> None:
STACObject.set_root(self, root)
super().set_root(root)
if root is not None:
root._resolved_objects = ResolvedObjectCache.merge(
root._resolved_objects, self._resolved_objects
)
if root._stac_io is not None:
self._stac_io = root._stac_io

def is_relative(self) -> bool:
return self.catalog_type in [
Expand Down
14 changes: 14 additions & 0 deletions tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
HIERARCHICAL_LINKS,
)
from pystac.extensions.label import LabelClasses, LabelExtension, LabelType
from pystac.stac_io import DefaultStacIO
from pystac.utils import is_absolute_href, join_path_or_url, JoinType
from tests.utils import (
TestCases,
Expand Down Expand Up @@ -107,6 +108,19 @@ def test_from_dict_set_root(self) -> None:
collection = Catalog.from_dict(cat_dict, root=root_cat)
self.assertIs(collection.get_root(), root_cat)

def test_from_dict_uses_root_stac_io(self) -> None:
class CustomStacIO(DefaultStacIO):
pass

path = TestCases.get_path("data-files/catalogs/test-case-1/catalog.json")
with open(path) as f:
cat_dict = json.load(f)
root_cat = pystac.Catalog(id="test", description="test desc")
root_cat._stac_io = CustomStacIO()

collection = Catalog.from_dict(cat_dict, root=root_cat)
self.assertIsInstance(collection._stac_io, CustomStacIO)

def test_read_remote(self) -> None:
# TODO: Move this URL to the main stac-spec repo once the example JSON is fixed.
catalog_url = (
Expand Down