diff options
| author | Cody Logan <cody@lokken.dev> | 2023-10-31 10:24:49 -0700 |
|---|---|---|
| committer | Cody Logan <cody@lokken.dev> | 2023-10-31 10:24:49 -0700 |
| commit | 1f68ee9d3b377549a636cd06b3dd89b9ea3171ac (patch) | |
| tree | fb0e9415a72cf18ff8cc824197cec7d9254171ad /src | |
| parent | d4dc4184e6b5117c584aee4e93eb8d44566e4634 (diff) | |
| download | wikiget-1f68ee9d3b377549a636cd06b3dd89b9ea3171ac.tar.gz wikiget-1f68ee9d3b377549a636cd06b3dd89b9ea3171ac.zip | |
Return early during dry runs; update dev version number
Diffstat (limited to 'src')
| -rw-r--r-- | src/wikiget/dl.py | 85 | ||||
| -rw-r--r-- | src/wikiget/file.py | 1 | ||||
| -rw-r--r-- | src/wikiget/version.py | 2 |
3 files changed, 45 insertions, 43 deletions
diff --git a/src/wikiget/dl.py b/src/wikiget/dl.py index 2fd7388..e36b954 100644 --- a/src/wikiget/dl.py +++ b/src/wikiget/dl.py @@ -113,49 +113,50 @@ def download(f: File, args: Namespace) -> int: if args.dry_run: adapter.warning("Dry run; download skipped") + return + + try: + fd = open(dest, "wb") + except OSError as e: + adapter.error(f"File could not be written: {e}") + errors += 1 + return errors + # download the file(s) + leave_bars = args.verbose >= wikiget.STD_VERBOSE + with tqdm( + desc=dest, + leave=leave_bars, + total=file_size, + unit="B", + unit_scale=True, + unit_divisor=wikiget.CHUNKSIZE, + ) as progress_bar: + with fd: + res = site.connection.get(file_url, stream=True) + for chunk in res.iter_content(wikiget.CHUNKSIZE): + fd.write(chunk) + progress_bar.update(len(chunk)) + + # verify file integrity and log details + try: + dl_sha1 = verify_hash(dest) + except OSError as e: + adapter.error(f"File downloaded but could not be verified: {e}") + errors += 1 + return errors + + adapter.info(f"Remote file SHA1 is {file_sha1}") + adapter.info(f"Local file SHA1 is {dl_sha1}") + if dl_sha1 == file_sha1: + adapter.info("Hashes match!") + # at this point, we've successfully downloaded the file + success_log = f"'{filename}' downloaded" + if args.output: + success_log += f" to '{dest}'" + adapter.info(success_log) else: - try: - fd = open(dest, "wb") - except OSError as e: - adapter.error(f"File could not be written: {e}") - errors += 1 - return errors - # download the file(s) - leave_bars = args.verbose >= wikiget.STD_VERBOSE - with tqdm( - desc=dest, - leave=leave_bars, - total=file_size, - unit="B", - unit_scale=True, - unit_divisor=wikiget.CHUNKSIZE, - ) as progress_bar: - with fd: - res = site.connection.get(file_url, stream=True) - for chunk in res.iter_content(wikiget.CHUNKSIZE): - fd.write(chunk) - progress_bar.update(len(chunk)) - - # verify file integrity and log details - try: - dl_sha1 = verify_hash(dest) - except OSError as e: - adapter.error(f"File downloaded but could not be verified: {e}") - errors += 1 - return errors - - adapter.info(f"Remote file SHA1 is {file_sha1}") - adapter.info(f"Local file SHA1 is {dl_sha1}") - if dl_sha1 == file_sha1: - adapter.info("Hashes match!") - # at this point, we've successfully downloaded the file - success_log = f"'{filename}' downloaded" - if args.output: - success_log += f" to '{dest}'" - adapter.info(success_log) - else: - adapter.error("Hash mismatch! Downloaded file may be corrupt.") - errors += 1 + adapter.error("Hash mismatch! Downloaded file may be corrupt.") + errors += 1 else: # no file information returned diff --git a/src/wikiget/file.py b/src/wikiget/file.py index 16402b0..0f639d3 100644 --- a/src/wikiget/file.py +++ b/src/wikiget/file.py @@ -37,6 +37,7 @@ class File: :type dest: str, optional :param site: name of the site hosting the file; if not specified, defaults to the global default site + :type site: str, optional """ self.image: Image = None self.name = name diff --git a/src/wikiget/version.py b/src/wikiget/version.py index ff19600..479f200 100644 --- a/src/wikiget/version.py +++ b/src/wikiget/version.py @@ -15,4 +15,4 @@ # You should have received a copy of the GNU General Public License # along with Wikiget. If not, see <https://www.gnu.org/licenses/>. -__version__ = "0.7.0" +__version__ = "0.8.0.dev0" |
