diff options
| author | Cody Logan <cody@lokken.dev> | 2023-11-08 09:56:10 -0800 |
|---|---|---|
| committer | Cody Logan <cody@lokken.dev> | 2023-11-08 09:56:10 -0800 |
| commit | e8da17d8c6b7fd879e196ae425b8e62c78e579fe (patch) | |
| tree | d782d7c1b8a88fcab69820d18a788e377f82c454 | |
| parent | 456316f882834c33e90a2ddfa8d4c5e47966dc5e (diff) | |
| download | wikiget-e8da17d8c6b7fd879e196ae425b8e62c78e579fe.tar.gz wikiget-e8da17d8c6b7fd879e196ae425b8e62c78e579fe.zip | |
Switch to pathlib for file processing
| -rw-r--r-- | src/wikiget/dl.py | 6 | ||||
| -rw-r--r-- | src/wikiget/validations.py | 3 | ||||
| -rw-r--r-- | tests/test_dl.py | 2 |
3 files changed, 6 insertions, 5 deletions
diff --git a/src/wikiget/dl.py b/src/wikiget/dl.py index 980ce1d..85f1ccf 100644 --- a/src/wikiget/dl.py +++ b/src/wikiget/dl.py @@ -16,10 +16,10 @@ # along with Wikiget. If not, see <https://www.gnu.org/licenses/>. import logging -import os 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 @@ -40,7 +40,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 os.path.isfile(file.dest) and not args.force: + if Path(file.dest).is_file() and not args.force: msg = f"[{file.dest}] File already exists; skipping download (use -f to force)" raise FileExistsError(msg) @@ -159,7 +159,7 @@ def download(f: File, args: Namespace) -> int: unit="B", unit_scale=True, unit_divisor=wikiget.CHUNKSIZE, - ) as progress_bar, open(dest, "wb") as fd: + ) as progress_bar, Path(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/validations.py b/src/wikiget/validations.py index cfbc5ef..ee73b87 100644 --- a/src/wikiget/validations.py +++ b/src/wikiget/validations.py @@ -19,6 +19,7 @@ from __future__ import annotations import hashlib import re +from pathlib import Path from wikiget import BLOCKSIZE @@ -68,7 +69,7 @@ def verify_hash(filename: str) -> str: :rtype: str """ hasher = hashlib.sha1() # noqa: S324 - with open(filename, "rb") as dl: + with Path(filename).open("rb") as dl: buf = dl.read(BLOCKSIZE) while len(buf) > 0: hasher.update(buf) diff --git a/tests/test_dl.py b/tests/test_dl.py index 6075515..c9f26dc 100644 --- a/tests/test_dl.py +++ b/tests/test_dl.py @@ -378,7 +378,7 @@ class TestDownload: def test_download_os_error( self, mock_file: File, caplog: pytest.LogCaptureFixture ) -> None: - with patch("wikiget.dl.open") as mock_open: + with patch("wikiget.dl.Path.open") as mock_open: mock_open.side_effect = OSError("write error") args = parse_args(["File:Example.jpg"]) errors = download(mock_file, args) |
