aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCody Logan <cody@lokken.dev>2023-11-01 13:11:35 -0700
committerCody Logan <cody@lokken.dev>2023-11-01 13:11:35 -0700
commitbf7d8c97620471fed7e9276462984b1f33b512d0 (patch)
tree006e0a99b4e9c06028e9192cf6da33a68b59329f
parent235b3e6a723e3e18962212c7d2c0f19619c2fa6f (diff)
downloadwikiget-bf7d8c97620471fed7e9276462984b1f33b512d0.tar.gz
wikiget-bf7d8c97620471fed7e9276462984b1f33b512d0.zip
Condense parser construction logic
-rw-r--r--src/wikiget/wikiget.py8
-rw-r--r--tests/test_client.py4
-rw-r--r--tests/test_dl.py12
-rw-r--r--tests/test_logging.py14
-rw-r--r--tests/test_parse.py10
5 files changed, 22 insertions, 26 deletions
diff --git a/src/wikiget/wikiget.py b/src/wikiget/wikiget.py
index 6b54886..ca655b0 100644
--- a/src/wikiget/wikiget.py
+++ b/src/wikiget/wikiget.py
@@ -18,6 +18,7 @@
import argparse
import logging
import sys
+from typing import List
import wikiget
from wikiget.dl import process_download
@@ -26,7 +27,7 @@ from wikiget.logging import configure_logging
logger = logging.getLogger(__name__)
-def construct_parser() -> argparse.ArgumentParser:
+def construct_parser(argv: List[str]) -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="""
A tool for downloading files from MediaWiki sites using the file name or
@@ -116,13 +117,12 @@ def construct_parser() -> argparse.ArgumentParser:
action="store_true",
)
- return parser
+ return parser.parse_args(argv)
def main() -> None:
# setup our environment
- parser = construct_parser()
- args = parser.parse_args()
+ args = construct_parser(sys.argv[1:])
configure_logging(verbosity=args.verbose, logfile=args.logfile, quiet=args.quiet)
# log events are appended to the file if it already exists, so note the start of a
diff --git a/tests/test_client.py b/tests/test_client.py
index cf6e29c..650c2cf 100644
--- a/tests/test_client.py
+++ b/tests/test_client.py
@@ -37,7 +37,7 @@ class TestQueryApi:
"""
caplog.set_level(logging.DEBUG)
mock_site.return_value = MagicMock()
- args = construct_parser().parse_args(["File:Example.jpg"])
+ args = construct_parser(["File:Example.jpg"])
_ = connect_to_site("commons.wikimedia.org", args)
assert mock_site.called
assert "Connecting to commons.wikimedia.org" in caplog.text
@@ -49,7 +49,7 @@ class TestQueryApi:
agent we're sending to the API.
"""
caplog.set_level(logging.DEBUG)
- args = construct_parser().parse_args(["File:Example.jpg"])
+ args = construct_parser(["File:Example.jpg"])
site = connect_to_site("commons.wikimedia.org", args)
_ = query_api("Example.jpg", site)
assert USER_AGENT in caplog.text
diff --git a/tests/test_dl.py b/tests/test_dl.py
index ba9ce32..cbda95c 100644
--- a/tests/test_dl.py
+++ b/tests/test_dl.py
@@ -32,7 +32,7 @@ class TestPrepDownload:
"""
The prep_download function should create a file object.
"""
- args = construct_parser().parse_args(["File:Example.jpg"])
+ args = construct_parser(["File:Example.jpg"])
file = prep_download(args.FILE, args)
assert file is not None
@@ -43,7 +43,7 @@ class TestPrepDownload:
"""
tmp_file = tmp_path / "File:Example.jpg"
tmp_file.write_text("nothing")
- args = construct_parser().parse_args(["File:Example.jpg", "-o", str(tmp_file)])
+ args = construct_parser(["File:Example.jpg", "-o", str(tmp_file)])
with pytest.raises(FileExistsError):
_ = prep_download(args.FILE, args)
@@ -54,7 +54,7 @@ class TestProcessDownload:
"""
A successful batch download should not return any errors.
"""
- args = construct_parser().parse_args(["-a", "batch.txt"])
+ args = construct_parser(["-a", "batch.txt"])
mock_batch_download.return_value = 0
exit_code = process_download(args)
assert mock_batch_download.called
@@ -68,7 +68,7 @@ class TestProcessDownload:
Any errors during batch download should create a log message containing the
number of errors and result in a non-zero exit code.
"""
- args = construct_parser().parse_args(["-a", "batch.txt"])
+ args = construct_parser(["-a", "batch.txt"])
mock_batch_download.return_value = 4
exit_code = process_download(args)
assert mock_batch_download.called
@@ -83,7 +83,7 @@ class TestProcessDownload:
"""
A successful download should not return any errors.
"""
- args = construct_parser().parse_args(["File:Example.jpg"])
+ args = construct_parser(["File:Example.jpg"])
mock_download.return_value = 0
mock_prep_download.return_value = MagicMock(File)
exit_code = process_download(args)
@@ -99,7 +99,7 @@ class TestProcessDownload:
"""
Any errors during download should result in a non-zero exit code.
"""
- args = construct_parser().parse_args(["File:Example.jpg"])
+ args = construct_parser(["File:Example.jpg"])
mock_download.return_value = 1
mock_prep_download.return_value = MagicMock(File)
exit_code = process_download(args)
diff --git a/tests/test_logging.py b/tests/test_logging.py
index b5ee6a0..4e0428e 100644
--- a/tests/test_logging.py
+++ b/tests/test_logging.py
@@ -30,7 +30,7 @@ def test_custom_log_adapter(caplog: LogCaptureFixture) -> None:
"""
The custom log adapter should prepend the filename to log messages.
"""
- args = construct_parser().parse_args(["File:Example.jpg"])
+ args = construct_parser(["File:Example.jpg"])
configure_logging(args.verbose, args.logfile, quiet=args.quiet)
adapter = FileLogAdapter(logger, {"filename": "Example.jpg"})
adapter.warning("test log")
@@ -42,9 +42,7 @@ def test_file_logging(tmp_path: Path) -> None:
Logging to a file should create the file in the specified location.
"""
logfile_location = tmp_path / "test.log"
- args = construct_parser().parse_args(
- ["File:Example.jpg", "-l", str(logfile_location)]
- )
+ args = construct_parser(["File:Example.jpg", "-l", str(logfile_location)])
configure_logging(args.verbose, args.logfile, quiet=args.quiet)
assert logfile_location.is_file()
@@ -53,7 +51,7 @@ def test_default_logging() -> None:
"""
The default log level should be set to WARNING.
"""
- args = construct_parser().parse_args(["File:Example.jpg"])
+ args = construct_parser(["File:Example.jpg"])
configure_logging(args.verbose, args.logfile, quiet=args.quiet)
# each call of configure_logging() adds a new handler to the logger, so we need to
# grab the most recently added one to test
@@ -65,7 +63,7 @@ def test_verbose_logging() -> None:
"""
When -v is passed, the log level should be set to INFO.
"""
- args = construct_parser().parse_args(["File:Example.jpg", "-v"])
+ args = construct_parser(["File:Example.jpg", "-v"])
configure_logging(args.verbose, args.logfile, quiet=args.quiet)
handler = logger.handlers[-1]
assert handler.level == logging.INFO
@@ -75,7 +73,7 @@ def test_very_verbose_logging() -> None:
"""
When -vv is passed, the log level should be set to DEBUG.
"""
- args = construct_parser().parse_args(["File:Example.jpg", "-vv"])
+ args = construct_parser(["File:Example.jpg", "-vv"])
configure_logging(args.verbose, args.logfile, quiet=args.quiet)
handler = logger.handlers[-1]
assert handler.level == logging.DEBUG
@@ -85,7 +83,7 @@ def test_quiet_logging() -> None:
"""
When -q is passed, the log level should be set to ERROR.
"""
- args = construct_parser().parse_args(["File:Example.jpg", "-q"])
+ args = construct_parser(["File:Example.jpg", "-q"])
configure_logging(args.verbose, args.logfile, quiet=args.quiet)
handler = logger.handlers[-1]
assert handler.level == logging.ERROR
diff --git a/tests/test_parse.py b/tests/test_parse.py
index 3cad21c..8d8b6a6 100644
--- a/tests/test_parse.py
+++ b/tests/test_parse.py
@@ -35,7 +35,7 @@ class TestGetDest:
When a filename is passed to get_dest, it should create a File object with the
correct name and dest and the default site.
"""
- args = construct_parser().parse_args(["File:Example.jpg"])
+ args = construct_parser(["File:Example.jpg"])
return get_dest(args.FILE, args)
def test_get_dest_name_with_filename(self, file_with_filename: File) -> None:
@@ -53,9 +53,7 @@ class TestGetDest:
When a URL is passed to get_dest, it should create a File object with the
correct name and dest and the site from the URL.
"""
- args = construct_parser().parse_args(
- ["https://en.wikipedia.org/wiki/File:Example.jpg"]
- )
+ args = construct_parser(["https://en.wikipedia.org/wiki/File:Example.jpg"])
return get_dest(args.FILE, args)
def test_get_dest_name_with_url(self, file_with_url: File) -> None:
@@ -71,7 +69,7 @@ class TestGetDest:
"""
The get_dest function should raise a ParseError if the filename is invalid.
"""
- args = construct_parser().parse_args(["Example.jpg"])
+ args = construct_parser(["Example.jpg"])
with pytest.raises(ParseError):
_ = get_dest(args.FILE, args)
@@ -82,7 +80,7 @@ class TestGetDest:
If a URL is passed to get_dest and a site is also given on the command line,
the site in the URL should be used and a warning log message created.
"""
- args = construct_parser().parse_args(
+ args = construct_parser(
[
"https://commons.wikimedia.org/wiki/File:Example.jpg",
"--site",