Skip to content
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

Add overwrite option #67

Merged
merged 1 commit into from
Aug 15, 2023
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Changed

- Use `Config` instead of standalone arguments ([#45](https://github.com/stac-utils/stac-asset/pull/45))
- Use `Config` instead of standalone arguments ([#45](https://github.com/stac-utils/stac-asset/pull/45), [#67](https://github.com/stac-utils/stac-asset/pull/67))
- s/CantIncludeAndExclude/CannotIncludeAndExclude/ ([#45](https://github.com/stac-utils/stac-asset/pull/45))
- Re-use the same client for an entire item collection ([#59](https://github.com/stac-utils/stac-asset/pull/59))

Expand Down
18 changes: 16 additions & 2 deletions src/stac_asset/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ def cli() -> None:
is_flag=True,
show_default=True,
)
@click.option(
"--overwrite",
help="Overwrite existing files if they exist on the filesystem",
default=False,
is_flag=True,
show_default=True,
)
# TODO add option to disable content type checking
def download(
href: Optional[str],
Expand All @@ -109,6 +116,7 @@ def download(
s3_retry_mode: str,
s3_max_attempts: int,
warn: bool,
overwrite: bool,
) -> None:
"""Download STAC assets from an item or item collection.

Expand Down Expand Up @@ -142,6 +150,7 @@ def download(
s3_retry_mode,
s3_max_attempts,
warn,
overwrite,
)
)

Expand All @@ -158,16 +167,21 @@ async def download_async(
s3_retry_mode: str,
s3_max_attempts: int,
warn: bool,
overwrite: bool,
) -> None:
if warn:
download_strategy = DownloadStrategy.DELETE
else:
download_strategy = DownloadStrategy.ERROR
config = Config(
alternate_assets=alternate_assets,
include=include,
exclude=exclude,
s3_requester_pays=s3_requester_pays,
s3_retry_mode=s3_retry_mode,
s3_max_attempts=s3_max_attempts,
# TODO allow configuring of download strategy
download_strategy=DownloadStrategy.DELETE,
download_strategy=download_strategy,
overwrite=overwrite,
)

if href is None or href == "-":
Expand Down
15 changes: 9 additions & 6 deletions src/stac_asset/_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,18 @@ class Download:
asset: Asset
path: Path
client: Client
overwrite: bool

async def download(
self, make_directory: bool, clean: bool, queue: Optional[AnyQueue]
) -> Union[Download, WrappedError]:
try:
self.asset = await self.client.download_asset(
self.key, self.asset, self.path, make_directory, clean, queue
)
except Exception as error:
return WrappedError(self, error)
if not self.overwrite and not os.path.exists(self.path):
try:
await self.client.download_asset(
self.key, self.asset, self.path, make_directory, clean, queue
)
except Exception as error:
return WrappedError(self, error)
self.asset.href = str(self.path)
return self

Expand Down Expand Up @@ -122,6 +124,7 @@ async def add(
asset=asset,
path=root / asset_file_name,
client=client,
overwrite=self.config.overwrite,
)
)

Expand Down
3 changes: 3 additions & 0 deletions src/stac_asset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class Config:
clean: bool = True
"""If true, clean up the downloaded file if it errors."""

overwrite: bool = False
"""Download files even if they already exist locally."""

earthdata_token: Optional[str] = None
"""A token for logging in to Earthdata."""

Expand Down