Skip to content

Commit

Permalink
Convert File.dest from a string to a Path (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
clpo13 committed Nov 15, 2023
1 parent 55a5311 commit bf3a7db
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 16 deletions.
7 changes: 3 additions & 4 deletions src/wikiget/dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion src/wikiget/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with Wikiget. If not, see <https://www.gnu.org/licenses/>.

from pathlib import Path
from typing import TYPE_CHECKING

if TYPE_CHECKING:
Expand Down Expand Up @@ -47,7 +48,7 @@ def __init__(self, name: str, dest: str = "", site: str = "") -> None:
"""
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:
Expand Down
9 changes: 6 additions & 3 deletions src/wikiget/validations.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@

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.
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def test_download_with_output(

with patch("wikiget.dl.verify_hash") as mock_verify_hash:
mock_verify_hash.return_value = "d01b79a6781c72ac9bfff93e5e2cfbeef4efc840"
args = parse_args(["-o", tmp_file, "File:Example.jpg"])
args = parse_args(["-o", str(tmp_file), "File:Example.jpg"])
errors = download(mock_file, args)

assert caplog.record_tuples[0] == (
Expand Down Expand Up @@ -414,7 +414,7 @@ def test_download_os_error(
If the downloaded file cannot be created, an error log message should be created
with details on the exception.
"""
with patch("wikiget.dl.Path.open") as mock_open:
with patch("pathlib.Path.open") as mock_open:
mock_open.side_effect = OSError("write error")
args = parse_args(["File:Example.jpg"])
errors = download(mock_file, args)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_file_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ def test_file_with_name(self, file_with_name: File) -> None:

def test_file_with_name_dest(self, file_with_name: File) -> None:
"""The file dest attribute should be the same as the name."""
assert file_with_name.dest == file_with_name.name
assert file_with_name.dest.match(file_with_name.name)

def test_file_with_name_site(self, file_with_name: File) -> None:
"""The file site attribute should equal the default site."""
assert file_with_name.site == DEFAULT_SITE

def test_file_with_name_and_dest(self, file_with_name_and_dest: File) -> None:
"""The file dest attribute should equal what was passed in."""
assert file_with_name_and_dest.dest == "bazqux.jpg"
assert file_with_name_and_dest.dest.match("bazqux.jpg")

def test_name_and_dest_are_different(self, file_with_name_and_dest: File) -> None:
"""The file name and dest attributes should not be the same."""
assert file_with_name_and_dest.dest != file_with_name_and_dest.name
assert not file_with_name_and_dest.dest.match(file_with_name_and_dest.name)

def test_file_with_name_and_site(self) -> None:
"""Test the attributes of a File created with a name and site.
Expand Down
4 changes: 2 additions & 2 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_get_dest_with_filename(self, file_with_filename: File) -> None:
Unless otherwise specified, it should match the filename.
"""
assert file_with_filename.dest == "Example.jpg"
assert file_with_filename.dest.match("Example.jpg")

def test_get_dest_site_with_filename(self, file_with_filename: File) -> None:
"""Test that the file's site attribute is set correctly.
Expand Down Expand Up @@ -88,7 +88,7 @@ def test_get_dest_name_with_url(self, file_with_url: File) -> None:

def test_get_dest_with_url(self, file_with_url: File) -> None:
"""Test that the file's dest attribute is set correctly."""
assert file_with_url.dest == "Example.jpg"
assert file_with_url.dest.match("Example.jpg")

def test_get_dest_site_with_url(self, file_with_url: File) -> None:
"""Test that the file's site attribute is set correctly.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_validations.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,4 @@ def test_verify_hash(self, test_file: Path) -> None:
"""
expected_sha1 = "cd19c009a30ca9b68045415a3a0838e64f3c2443"

assert verify_hash(str(test_file)) == expected_sha1
assert verify_hash(test_file) == expected_sha1

0 comments on commit bf3a7db

Please sign in to comment.