|
22 | 22 |
|
23 | 23 | if TYPE_CHECKING: |
24 | 24 | from .catalog import Catalog |
| 25 | + from .container import Container |
25 | 26 | from .io import Read, Write |
26 | 27 |
|
27 | 28 |
|
@@ -82,7 +83,7 @@ def from_dict( |
82 | 83 | d: dict[str, Any], |
83 | 84 | *, |
84 | 85 | href: str | None = None, |
85 | | - root: Catalog | None = None, # TODO deprecation warning |
| 86 | + root: Catalog | None = None, |
86 | 87 | migrate: bool = False, |
87 | 88 | preserve_dict: bool = True, # TODO deprecation warning |
88 | 89 | reader: Read | None = None, |
@@ -127,6 +128,15 @@ def from_dict( |
127 | 128 | raise StacError(f"unknown type field: {type_value}") |
128 | 129 |
|
129 | 130 | if isinstance(stac_object, cls): |
| 131 | + if root: |
| 132 | + warnings.warn( |
| 133 | + "The `root` argument is deprecated in PySTAC v2 and " |
| 134 | + "will be removed in a future version. Prefer to use " |
| 135 | + "`stac_object.set_link(Link.root(catalog))` " |
| 136 | + "after object creation.", |
| 137 | + FutureWarning, |
| 138 | + ) |
| 139 | + stac_object.set_link(Link.root(root)) |
130 | 140 | return stac_object |
131 | 141 | else: |
132 | 142 | raise PystacError(f"Expected {cls} but got a {type(stac_object)}") |
@@ -244,6 +254,19 @@ def save_object( |
244 | 254 | else: |
245 | 255 | raise PystacError("cannot save an object without an href") |
246 | 256 |
|
| 257 | + def get_root(self) -> Container | None: |
| 258 | + """Returns the container at this object's root link, if there is one.""" |
| 259 | + from .container import Container |
| 260 | + |
| 261 | + if link := self.get_link(ROOT_REL): |
| 262 | + stac_object = link.get_stac_object() |
| 263 | + if isinstance(stac_object, Container): |
| 264 | + return stac_object |
| 265 | + else: |
| 266 | + return None |
| 267 | + else: |
| 268 | + return None |
| 269 | + |
247 | 270 | def get_link(self, rel: str) -> Link | None: |
248 | 271 | return next((link for link in self._links if link.rel == rel), None) |
249 | 272 |
|
|
0 commit comments