Skip to content

Commit

Permalink
feat: add overwrite option
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Aug 15, 2023
1 parent 1b3b7f0 commit 20c24e4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
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

0 comments on commit 20c24e4

Please sign in to comment.