From bf3a7db6cf83fa7d224d84c47b40cfe32603019c Mon Sep 17 00:00:00 2001 From: Cody Logan Date: Wed, 15 Nov 2023 10:36:18 -0800 Subject: Convert File.dest from a string to a Path (#14) --- src/wikiget/dl.py | 7 +++---- src/wikiget/file.py | 3 ++- src/wikiget/validations.py | 9 ++++++--- 3 files changed, 11 insertions(+), 8 deletions(-) (limited to 'src/wikiget') diff --git a/src/wikiget/dl.py b/src/wikiget/dl.py index 9b2777f..60405d0 100644 --- a/src/wikiget/dl.py +++ b/src/wikiget/dl.py @@ -19,7 +19,6 @@ import logging import sys from argparse import Namespace from concurrent.futures import ThreadPoolExecutor -from pathlib import Path from mwclient import APIError, InvalidResponse, LoginError from requests import ConnectionError, HTTPError @@ -54,7 +53,7 @@ def prep_download(dl: str, args: Namespace) -> File: file = get_dest(dl, args) # check if the destination file already exists; don't overwrite unless the user says - if Path(file.dest).is_file() and not args.force: + if file.dest.is_file() and not args.force: msg = f"[{file.dest}] File already exists; skipping download (use -f to force)" raise FileExistsError(msg) @@ -203,13 +202,13 @@ def download(f: File, args: Namespace) -> int: try: with tqdm( - desc=dest, + desc=str(dest), leave=args.verbose >= wikiget.STD_VERBOSE, total=file_size, unit="B", unit_scale=True, unit_divisor=wikiget.CHUNKSIZE, - ) as progress_bar, Path(dest).open("wb") as fd: + ) as progress_bar, dest.open("wb") as fd: # download the file using the existing Site session res = site.connection.get(file_url, stream=True) for chunk in res.iter_content(wikiget.CHUNKSIZE): diff --git a/src/wikiget/file.py b/src/wikiget/file.py index cbd738a..e36feba 100644 --- a/src/wikiget/file.py +++ b/src/wikiget/file.py @@ -15,6 +15,7 @@ # You should have received a copy of the GNU General Public License # along with Wikiget. If not, see . +from pathlib import Path from typing import TYPE_CHECKING if TYPE_CHECKING: @@ -47,7 +48,7 @@ class File: """ self.image: Image = None self.name = name - self.dest = dest if dest else name + self.dest = Path(dest) if dest else Path(name) self.site = site if site else DEFAULT_SITE def __eq__(self, other: object) -> bool: diff --git a/src/wikiget/validations.py b/src/wikiget/validations.py index c7cc2dd..7c2b5ae 100644 --- a/src/wikiget/validations.py +++ b/src/wikiget/validations.py @@ -19,10 +19,13 @@ from __future__ import annotations import hashlib import re -from pathlib import Path +from typing import TYPE_CHECKING from wikiget import BLOCKSIZE +if TYPE_CHECKING: + from pathlib import Path + def valid_file(search_string: str) -> re.Match | None: """Determine if the given string contains a valid file name. @@ -60,7 +63,7 @@ def valid_site(search_string: str) -> re.Match | None: return site_regex.search(search_string) -def verify_hash(filename: str) -> str: +def verify_hash(file: Path) -> str: """Calculate the SHA1 hash of the given file for comparison with a known value. Despite being insecure, SHA1 is used since that's what the MediaWiki API returns for @@ -72,7 +75,7 @@ def verify_hash(filename: str) -> str: :rtype: str """ hasher = hashlib.sha1() # noqa: S324 - with Path(filename).open("rb") as dl: + with file.open("rb") as dl: buf = dl.read(BLOCKSIZE) while len(buf) > 0: hasher.update(buf) -- cgit v1.2.3