Skip to content

Commit

Permalink
feat: more v1 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Feb 14, 2025
1 parent 3ce03ed commit 7f2214c
Show file tree
Hide file tree
Showing 90 changed files with 18,339 additions and 119 deletions.
8 changes: 4 additions & 4 deletions docs/api/general.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
::: pystac.read_file
::: pystac.read_dict
::: pystac.write_file
::: pystac.PystacError
::: pystac.StacError
::: pystac.PystacWarning
::: pystac.StacWarning
::: pystac.PySTACError
::: pystac.STACError
::: pystac.PySTACWarning
::: pystac.STACWarning
16 changes: 8 additions & 8 deletions src/pystac/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from .constants import DEFAULT_STAC_VERSION
from .container import Container
from .errors import (
PystacError,
PystacWarning,
StacError,
StacWarning,
PySTACError,
PySTACWarning,
STACError,
STACWarning,
)
from .extent import Extent, SpatialExtent, TemporalExtent
from .functions import get_stac_version, read_dict, set_stac_version
Expand Down Expand Up @@ -37,10 +37,10 @@ def __getattr__(name: str) -> Any:
"Collection",
"DEFAULT_STAC_VERSION",
"Container",
"PystacError",
"PystacWarning",
"StacError",
"StacWarning",
"PySTACError",
"PySTACWarning",
"STACError",
"STACWarning",
"Extent",
"SpatialExtent",
"TemporalExtent",
Expand Down
29 changes: 21 additions & 8 deletions src/pystac/asset.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from __future__ import annotations

import copy
from typing import Any, Protocol, runtime_checkable
from typing import TYPE_CHECKING, Any

from typing_extensions import Self

from . import deprecate, utils

if TYPE_CHECKING:
from .stac_object import STACObject


class ItemAsset:
"""An asset without a href.
Expand Down Expand Up @@ -70,15 +75,23 @@ def __init__(
title=title, description=description, type=type, roles=roles, **kwargs
)

@deprecate.function(
"assets aren't owned anymore, all this method does is make the asset's "
"relative href absolute using the 'owner's href"
)
def set_owner(self, owner: STACObject) -> None:
if owner.href:
self.href = utils.make_absolute_href(self.href, owner.href)

@deprecate.function("prefer to use `STACObject.render()` then `asset.href`")
def get_absolute_href(self) -> str | None:
if utils.is_absolute_href(self.href):
return self.href
else:
return None

def to_dict(self) -> dict[str, Any]:
"""Converts this asset to a dictionary."""
d = {"href": self.href}
d.update(super().to_dict())
return d


@runtime_checkable
class Assets(Protocol):
"""A protocol for things that have assets (Collections and Items)"""

assets: dict[str, Asset]
19 changes: 9 additions & 10 deletions src/pystac/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@ def __init__(
else:
self.extent = Extent.from_dict(extent)

if assets is None:
self.assets = None
else:
self.assets = dict(
(key, asset if isinstance(asset, Asset) else Asset.from_dict(asset))
for key, asset in assets.items()
)

if item_assets is None:
self.item_assets = None
else:
Expand All @@ -77,7 +69,14 @@ def __init__(
for key, item_asset in item_assets.items()
)

super().__init__(id, stac_version, stac_extensions, links, **kwargs)
super().__init__(
id=id,
stac_version=stac_version,
stac_extensions=stac_extensions,
links=links,
assets=assets,
**kwargs,
)

def _to_dict(self) -> dict[str, Any]:
"""Converts this collection to a dictionary."""
Expand All @@ -100,7 +99,7 @@ def _to_dict(self) -> dict[str, Any]:
if self.summaries is not None:
d["summaries"] = self.summaries
d["links"] = [link.to_dict() for link in self.iter_links()]
if self.assets is not None:
if self.assets:
d["assets"] = dict(
(key, asset.to_dict()) for key, asset in self.assets.items()
)
Expand Down
2 changes: 2 additions & 0 deletions src/pystac/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@
"""The root relation type, for links."""
SELF = "self"
"""The self relation type, for links."""
COLLECTION = "collection"
"""The collection relation type, for links."""
9 changes: 9 additions & 0 deletions src/pystac/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ def add_child(self, child: Container) -> None:
"""Adds a child to this container."""
self.add_link(Link.child(child))

def get_child(
self, id: str, recursive: bool = False, sort_links_by_id: bool = True
) -> Container | None:
# TODO handle sort links by id
for child in self.get_children(recursive=recursive):
if child.id == id:
return child
return None

def get_children(self, recursive: bool = False) -> Iterator[Container]:
"""Iterates over all children in this container.
Expand Down
10 changes: 5 additions & 5 deletions src/pystac/errors.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
class PystacError(Exception):
class PySTACError(Exception):
"""A custom error class for this library."""


class StacError(PystacError):
"""A subclass of [PystacError][pystac.PystacError] for errors related to the
class STACError(PySTACError):
"""A subclass of [PySTACError][pystac.PySTACError] for errors related to the
STAC specification itself."""


class PystacWarning(Warning):
class PySTACWarning(Warning):
"""A custom warning class for this library."""


class StacWarning(PystacWarning):
class STACWarning(PySTACWarning):
"""A warning about something incorrect per the STAC specification."""
6 changes: 3 additions & 3 deletions src/pystac/extensions/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from typing_extensions import Self

from ..errors import PystacWarning
from ..errors import PySTACWarning
from .protocols import Extendable


Expand Down Expand Up @@ -87,11 +87,11 @@ def version(self) -> str | None:
return parts[1][1:]
else:
warnings.warn(
f"Invalid extension version: {parts[1]}", PystacWarning
f"Invalid extension version: {parts[1]}", PySTACWarning
)
return None
else:
warnings.warn(f"Invalid extension url: {url}", PystacWarning)
warnings.warn(f"Invalid extension url: {url}", PySTACWarning)
return None
else:
return None
Expand Down
12 changes: 6 additions & 6 deletions src/pystac/extent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from . import deprecate
from .constants import DEFAULT_BBOX, DEFAULT_INTERVAL
from .errors import StacWarning
from .errors import STACWarning
from .types import PermissiveBbox, PermissiveInterval


Expand Down Expand Up @@ -115,7 +115,7 @@ def __init__(
warnings.warn(
"Invalid temporal interval (trailing single values), "
"discarding",
StacWarning,
STACWarning,
)
else:
self.interval.append(_create_interval(interval)) # type: ignore
Expand All @@ -133,7 +133,7 @@ def datetime_str(value: datetime.datetime | str | None) -> str | None:
return value
else:
warnings.warn(
f"Invalid interval value ({type(value)}), converting to None", StacWarning
f"Invalid interval value ({type(value)}), converting to None", STACWarning
)
return None

Expand All @@ -142,15 +142,15 @@ def _create_interval(
interval: list[str | datetime.datetime | None],
) -> list[str | None]:
if len(interval) == 0:
warnings.warn("Invalid interval value (empty list)", StacWarning)
warnings.warn("Invalid interval value (empty list)", STACWarning)
interval = [None, None]
elif len(interval) == 1:
warnings.warn("Invalid interval value (single entry list)", StacWarning)
warnings.warn("Invalid interval value (single entry list)", STACWarning)
interval.append(None)
elif len(interval) > 2:
warnings.warn(
f"Invalid interval value ({len(interval)} values), truncating",
StacWarning,
STACWarning,
)
interval = interval[0:2]
return [datetime_str(v) for v in interval]
4 changes: 2 additions & 2 deletions src/pystac/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from typing import Any, Protocol

from . import deprecate
from .errors import PystacError
from .errors import PySTACError
from .stac_object import STACObject


Expand Down Expand Up @@ -78,7 +78,7 @@ def write_file(
if dest_href is None:
dest_href = obj.href
if dest_href is None:
raise PystacError(f"cannot write {obj} without an href")
raise PySTACError(f"cannot write {obj} without an href")
d = obj.to_dict()
if isinstance(dest_href, Path):
writer.write_json_to_path(d, dest_href)
Expand Down
Loading

0 comments on commit 7f2214c

Please sign in to comment.