Skip to content

Commit 9c229d0

Browse files
committed
Add "public" Catalog.stac_io property
1 parent 10e1efe commit 9c229d0

File tree

5 files changed

+32
-17
lines changed

5 files changed

+32
-17
lines changed

pystac/catalog.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class CatalogType(str, Enum):
4444
4545
See:
4646
:stac-spec:`The best practices documentation on self-contained catalogs
47-
<best-practices.md#self-contained-catalogs>`
47+
<best-practices.md#self-contained-catalogs>`
4848
"""
4949

5050
ABSOLUTE_PUBLISHED = "ABSOLUTE_PUBLISHED"
@@ -54,7 +54,7 @@ class CatalogType(str, Enum):
5454
5555
See:
5656
:stac-spec:`The best practices documentation on published catalogs
57-
<best-practices.md#published-catalogs>`
57+
<best-practices.md#published-catalogs>`
5858
"""
5959

6060
RELATIVE_PUBLISHED = "RELATIVE_PUBLISHED"
@@ -64,7 +64,7 @@ class CatalogType(str, Enum):
6464
6565
See:
6666
:stac-spec:`The best practices documentation on published catalogs
67-
<best-practices.md#published-catalogs>`
67+
<best-practices.md#published-catalogs>`
6868
"""
6969

7070
@classmethod
@@ -135,10 +135,6 @@ class Catalog(STACObject):
135135
STAC_OBJECT_TYPE = pystac.STACObjectType.CATALOG
136136

137137
_stac_io: Optional[pystac.StacIO] = None
138-
"""Optional instance of StacIO that will be used by default
139-
for any IO operations on objects contained by this catalog.
140-
Set while reading in a catalog. This is set when a catalog
141-
is read by a StacIO instance."""
142138

143139
DEFAULT_FILE_NAME = "catalog.json"
144140
"""Default file name that will be given to this STAC object in
@@ -176,17 +172,36 @@ def __init__(
176172

177173
self._resolved_objects.cache(self)
178174

175+
@property
176+
def stac_io(self) -> Optional[pystac.StacIO]:
177+
"""Optional instance of :class:`~pystac.StacIO` that will be used by default
178+
for any IO operations on objects contained by this catalog. Set while reading
179+
in a catalog. This is set when a catalog is read by a :class:`~pystac.StacIO`
180+
instance."""
181+
return self._stac_io
182+
183+
@stac_io.setter
184+
def stac_io(self, v: Optional[pystac.StacIO]) -> None:
185+
self._stac_io = v
186+
179187
def __repr__(self) -> str:
180188
return "<Catalog id={}>".format(self.id)
181189

182190
def set_root(self, root: Optional["Catalog"]) -> None:
191+
"""Sets the root :class:`~pystac.Catalog` for this :class:`~pystac.Catalog`.
192+
This method also sets the :attr:`~pystac.Catalog.stac_io` for this ``Catalog``
193+
to be the same as ``root.stac_io``.
194+
195+
Args:
196+
root : A :class:`~pystac.Catalog` instance to set as the root.
197+
"""
183198
super().set_root(root)
184199
if root is not None:
185200
root._resolved_objects = ResolvedObjectCache.merge(
186201
root._resolved_objects, self._resolved_objects
187202
)
188-
if root._stac_io is not None:
189-
self._stac_io = root._stac_io
203+
if root.stac_io is not None:
204+
self.stac_io = root.stac_io
190205

191206
def is_relative(self) -> bool:
192207
return self.catalog_type in [
@@ -1012,7 +1027,7 @@ def from_file(cls, href: str, stac_io: Optional[pystac.StacIO] = None) -> "Catal
10121027
result = super().from_file(href, stac_io)
10131028
if not isinstance(result, Catalog):
10141029
raise pystac.STACTypeError(f"{result} is not a {Catalog}.")
1015-
result._stac_io = stac_io
1030+
result.stac_io = stac_io
10161031

10171032
return result
10181033

pystac/link.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,14 @@ def resolve_stac_object(self, root: Optional["Catalog_Type"] = None) -> "Link":
252252

253253
if root is not None:
254254
obj = root._resolved_objects.get_by_href(target_href)
255-
stac_io = root._stac_io
255+
stac_io = root.stac_io
256256

257257
if obj is None:
258258

259259
if stac_io is None:
260260
if self.owner is not None:
261261
if isinstance(self.owner, pystac.Catalog):
262-
stac_io = self.owner._stac_io
262+
stac_io = self.owner.stac_io
263263
if stac_io is None:
264264
stac_io = pystac.StacIO.default()
265265

pystac/stac_io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def stac_object_from_dict(
160160
result = pystac.Catalog.from_dict(
161161
d, href=href, root=root, migrate=False, preserve_dict=preserve_dict
162162
)
163-
result._stac_io = self
163+
result.stac_io = self
164164
return result
165165

166166
if info.object_type == pystac.STACObjectType.COLLECTION:

pystac/stac_object.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def save_object(
323323
if stac_io is None:
324324
root = self.get_root()
325325
if root is not None:
326-
root_stac_io = root._stac_io
326+
root_stac_io = root.stac_io
327327
if root_stac_io is not None:
328328
stac_io = root_stac_io
329329

tests/test_catalog.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ class CustomStacIO(DefaultStacIO):
116116
with open(path) as f:
117117
cat_dict = json.load(f)
118118
root_cat = pystac.Catalog(id="test", description="test desc")
119-
root_cat._stac_io = CustomStacIO()
119+
root_cat.stac_io = CustomStacIO()
120120

121121
collection = Catalog.from_dict(cat_dict, root=root_cat)
122-
self.assertIsInstance(collection._stac_io, CustomStacIO)
122+
self.assertIsInstance(collection.stac_io, CustomStacIO)
123123

124124
def test_read_remote(self) -> None:
125125
# TODO: Move this URL to the main stac-spec repo once the example JSON is fixed.
@@ -948,7 +948,7 @@ def test_collections_cache_correctly(self) -> None:
948948
catalogs = TestCases.all_test_catalogs()
949949
mock_io = MockStacIO()
950950
for cat in catalogs:
951-
cat._stac_io = mock_io
951+
cat.stac_io = mock_io
952952
expected_collection_reads = set([])
953953
for root, _, items in cat.walk():
954954
if isinstance(root, Collection) and root != cat:

0 commit comments

Comments
 (0)