From e8e434f604cd293b98524ea9292f8722cbfc5a2e Mon Sep 17 00:00:00 2001 From: teutoburg Date: Thu, 22 Jun 2023 12:28:27 +0200 Subject: [PATCH] Use proper (double) with block instead of try/finally --- scopesim/server/database.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/scopesim/server/database.py b/scopesim/server/database.py index 33f307f8..ee5997dd 100644 --- a/scopesim/server/database.py +++ b/scopesim/server/database.py @@ -360,17 +360,13 @@ def _initiate_download(pkg_url: str, def _handle_download(response, save_path: Path, pkg_name: str, padlen: int, chunk_size: int = 128) -> None: - try: - tqdm_kwargs = _make_tqdm_kwargs(f"Downloading {pkg_name:<{padlen}}") - with tqdm.wrapattr(save_path.open("wb"), "write", miniters=1, - total=int(response.headers.get("content-length", 0)), - **tqdm_kwargs) as file: - for chunk in response.iter_content(chunk_size=chunk_size): - file.write(chunk) - except Exception as error: - raise error - finally: - file.close() + tqdm_kwargs = _make_tqdm_kwargs(f"Downloading {pkg_name:<{padlen}}") + total = int(response.headers.get("content-length", 0)) + with (save_path.open("wb") as file_outer, + tqdm.wrapattr(file_outer, "write", miniters=1, total=total, + **tqdm_kwargs) as file_inner): + for chunk in response.iter_content(chunk_size=chunk_size): + file_inner.write(chunk) def _handle_unzipping(save_path: Path, save_dir: Path,