-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
273 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
"""Blocking interfaces for functions. | ||
These should only be used from fully synchronous code. If you have _any_ async | ||
code in your application, prefer the top-level functions. | ||
""" | ||
|
||
import asyncio | ||
from asyncio import Queue | ||
from pathlib import Path | ||
from typing import List, Optional | ||
|
||
from pystac import Asset, Collection, Item, ItemCollection | ||
|
||
from . import _functions | ||
from .client import Client, Clients | ||
from .config import Config | ||
from .messages import Message | ||
from .types import AnyQueue, PathLikeObject | ||
|
||
|
||
def download_item( | ||
item: Item, | ||
directory: PathLikeObject, | ||
file_name: Optional[str] = None, | ||
infer_file_name: bool = True, | ||
config: Optional[Config] = None, | ||
queue: Optional[AnyQueue] = None, | ||
clients: Optional[List[Client]] = None, | ||
) -> Item: | ||
"""Downloads an item to the local filesystem, synchronously. | ||
Args: | ||
item: The :py:class:`pystac.Item`. | ||
directory: The output directory that will hold the items and assets. | ||
file_name: The name of the item file to save. If not provided, will not | ||
be saved. | ||
infer_file_name: If ``file_name`` is None, infer the file name from the | ||
item's id. This argument is unused if ``file_name`` is not None. | ||
config: The download configuration | ||
queue: An optional queue to use for progress reporting | ||
clients: Pre-configured clients to use for access | ||
Returns: | ||
Item: The `~pystac.Item`, with the updated asset hrefs and self href. | ||
Raises: | ||
ValueError: Raised if the item doesn't have any assets. | ||
""" | ||
return asyncio.run( | ||
_functions.download_item( | ||
item=item, | ||
directory=directory, | ||
file_name=file_name, | ||
infer_file_name=infer_file_name, | ||
config=config, | ||
queue=queue, | ||
clients=clients, | ||
) | ||
) | ||
|
||
|
||
def download_collection( | ||
collection: Collection, | ||
directory: PathLikeObject, | ||
file_name: Optional[str] = "collection.json", | ||
config: Optional[Config] = None, | ||
queue: Optional[AnyQueue] = None, | ||
clients: Optional[List[Client]] = None, | ||
) -> Collection: | ||
"""Downloads a collection to the local filesystem, synchronously. | ||
Does not download the collection's items' assets -- use | ||
:py:func:`download_item_collection` to download multiple items. | ||
Args: | ||
collection: A pystac collection | ||
directory: The destination directory | ||
file_name: The name of the collection file to save. If not provided, | ||
will not be saved. | ||
config: The download configuration | ||
queue: An optional queue to use for progress reporting | ||
clients: Pre-configured clients to use for access | ||
Returns: | ||
Collection: The collection, with updated asset hrefs | ||
Raises: | ||
CantIncludeAndExclude: Raised if both include and exclude are not None. | ||
""" | ||
return asyncio.run( | ||
_functions.download_collection( | ||
collection=collection, | ||
directory=directory, | ||
file_name=file_name, | ||
config=config, | ||
queue=queue, | ||
clients=clients, | ||
) | ||
) | ||
|
||
|
||
def download_item_collection( | ||
item_collection: ItemCollection, | ||
directory: PathLikeObject, | ||
file_name: Optional[str] = "item-collection.json", | ||
config: Optional[Config] = None, | ||
queue: Optional[AnyQueue] = None, | ||
clients: Optional[List[Client]] = None, | ||
) -> ItemCollection: | ||
"""Downloads an item collection to the local filesystem, synchronously. | ||
Args: | ||
item_collection: The item collection to download | ||
directory: The destination directory | ||
file_name: The name of the item collection file to save. If not | ||
provided, will not be saved. | ||
config: The download configuration | ||
queue: An optional queue to use for progress reporting | ||
clients: Pre-configured clients to use for access | ||
Returns: | ||
ItemCollection: The item collection, with updated asset hrefs | ||
Raises: | ||
CantIncludeAndExclude: Raised if both include and exclude are not None. | ||
""" | ||
return asyncio.run( | ||
_functions.download_item_collection( | ||
item_collection=item_collection, | ||
directory=directory, | ||
file_name=file_name, | ||
config=config, | ||
queue=queue, | ||
clients=clients, | ||
) | ||
) | ||
|
||
|
||
def download_asset( | ||
key: str, | ||
asset: Asset, | ||
path: Path, | ||
config: Config, | ||
messages: Optional[Queue[Message]] = None, | ||
clients: Optional[Clients] = None, | ||
) -> Asset: | ||
"""Downloads an asset, synchronously. | ||
Args: | ||
key: The asset key | ||
asset: The asset | ||
path: The path to which the asset will be downloaded | ||
config: The download configuration | ||
messages: An optional queue to use for progress reporting | ||
clients: A async-safe cache of clients. If not provided, a new one | ||
will be created. | ||
Returns: | ||
Asset: The asset with an updated href | ||
Raises: | ||
ValueError: Raised if the asset does not have an absolute href | ||
""" | ||
return asyncio.run( | ||
_functions.download_asset( | ||
key=key, | ||
asset=asset, | ||
path=path, | ||
config=config, | ||
messages=messages, | ||
clients=clients, | ||
) | ||
) | ||
|
||
|
||
def assert_asset_exists( | ||
asset: Asset, | ||
config: Optional[Config] = None, | ||
clients: Optional[List[Client]] = None, | ||
) -> None: | ||
"""Asserts that an asset exists, synchronously. | ||
Raises the source error if it does not. | ||
Args: | ||
asset: The asset the check for existence | ||
config: The download configuration to use for the existence check | ||
clients: Any pre-configured clients to use for the existence check | ||
Raises: | ||
Exception: An exception from the underlying client. | ||
""" | ||
asyncio.run(_functions.assert_asset_exists(asset, config, clients)) | ||
|
||
|
||
def asset_exists( | ||
asset: Asset, | ||
config: Optional[Config] = None, | ||
clients: Optional[List[Client]] = None, | ||
) -> bool: | ||
"""Returns true if an asset exists, synchronously. | ||
Args: | ||
asset: The asset the check for existence | ||
config: The download configuration to use for the existence check | ||
clients: Any pre-configured clients to use for the existence check | ||
Returns: | ||
bool: Whether the asset exists or not | ||
""" | ||
return asyncio.run(_functions.asset_exists(asset, config, clients)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from pathlib import Path | ||
|
||
import stac_asset.blocking | ||
from pystac import Collection, Item, ItemCollection | ||
from stac_asset import Config | ||
|
||
|
||
def test_download_item(tmp_path: Path, item: Item) -> None: | ||
item = stac_asset.blocking.download_item(item, tmp_path) | ||
item.validate() | ||
|
||
|
||
def test_download_collection(tmp_path: Path, collection: Collection) -> None: | ||
collection = stac_asset.blocking.download_collection(collection, tmp_path) | ||
collection.validate() | ||
|
||
|
||
def test_download_item_collection( | ||
tmp_path: Path, item_collection: ItemCollection | ||
) -> None: | ||
item_collection = stac_asset.blocking.download_item_collection( | ||
item_collection, tmp_path | ||
) | ||
for item in item_collection: | ||
item.validate() | ||
|
||
|
||
def test_download_asset(tmp_path: Path, item: Item) -> None: | ||
asset = stac_asset.blocking.download_asset( | ||
"data", item.assets["data"], tmp_path / "image.jpg", Config() | ||
) | ||
assert asset.href == tmp_path / "image.jpg" | ||
|
||
|
||
def test_assert_asset_exists(tmp_path: Path, item: Item) -> None: | ||
stac_asset.blocking.assert_asset_exists(item.assets["data"]) | ||
|
||
|
||
def test_asset_exists(tmp_path: Path, item: Item) -> None: | ||
assert stac_asset.blocking.asset_exists(item.assets["data"]) |