diff options
| author | Cody Logan <clpo13@gmail.com> | 2023-09-26 11:04:55 -0700 |
|---|---|---|
| committer | Cody Logan <clpo13@gmail.com> | 2023-09-26 11:04:55 -0700 |
| commit | 6bc21723d97a83f72fb1dc56a9c2db7e0e0cb36e (patch) | |
| tree | bb1c8d463ed6e81ed83aa698d9d1bfd7660e8008 /wikiget | |
| parent | f6871103d61a1aed2e537bb6ec00b47948f0610d (diff) | |
| download | wikiget-6bc21723d97a83f72fb1dc56a9c2db7e0e0cb36e.tar.gz wikiget-6bc21723d97a83f72fb1dc56a9c2db7e0e0cb36e.zip | |
Get rid of magic numbers for verbosity checking
Diffstat (limited to 'wikiget')
| -rw-r--r-- | wikiget/__init__.py | 2 | ||||
| -rw-r--r-- | wikiget/dl.py | 30 | ||||
| -rw-r--r-- | wikiget/validations.py | 2 | ||||
| -rw-r--r-- | wikiget/wikiget.py | 20 |
4 files changed, 29 insertions, 25 deletions
diff --git a/wikiget/__init__.py b/wikiget/__init__.py index 93972fc..b68b0ec 100644 --- a/wikiget/__init__.py +++ b/wikiget/__init__.py @@ -27,3 +27,5 @@ DEFAULT_PATH = "/w/" USER_AGENT = "wikiget/{} (https://github.com/clpo13/wikiget) mwclient/{}".format( wikiget_version, mwclient_version ) +STD_VERBOSE = 1 +VERY_VERBOSE = 2 diff --git a/wikiget/dl.py b/wikiget/dl.py index aa79ae3..949f09e 100644 --- a/wikiget/dl.py +++ b/wikiget/dl.py @@ -23,7 +23,7 @@ from mwclient import APIError, InvalidResponse, LoginError, Site from requests import ConnectionError, HTTPError from tqdm import tqdm -from wikiget import CHUNKSIZE, DEFAULT_SITE, USER_AGENT +import wikiget from wikiget.validations import valid_file, verify_hash @@ -33,7 +33,7 @@ def download(dl, args): if url.netloc: filename = url.path site_name = url.netloc - if args.site is not DEFAULT_SITE and not args.quiet: + if args.site is not wikiget.DEFAULT_SITE and not args.quiet: # this will work even if the user specifies 'commons.wikimedia.org' print("Warning: target is a URL, ignoring site specified with --site") else: @@ -55,21 +55,21 @@ def download(dl, args): dest = args.output or filename - if args.verbose >= 2: - print(f"User agent: {USER_AGENT}") + if args.verbose >= wikiget.VERY_VERBOSE: + print(f"User agent: {wikiget.USER_AGENT}") # connect to site and identify ourselves - if args.verbose >= 1: + if args.verbose >= wikiget.STD_VERBOSE: print(f"Site name: {site_name}") try: - site = Site(site_name, path=args.path, clients_useragent=USER_AGENT) + site = Site(site_name, path=args.path, clients_useragent=wikiget.USER_AGENT) if args.username and args.password: site.login(args.username, args.password) except ConnectionError as e: # usually this means there is no such site, or there's no network # connection, though it could be a certificate problem print("Error: couldn't connect to specified site.") - if args.verbose >= 2: + if args.verbose >= wikiget.VERY_VERBOSE: print("Full error message:") print(e) sys.exit(1) @@ -79,7 +79,7 @@ def download(dl, args): "Error: couldn't find the specified wiki's api.php. " "Check the value of --path." ) - if args.verbose >= 2: + if args.verbose >= wikiget.VERY_VERBOSE: print("Full error message:") print(e) sys.exit(1) @@ -100,7 +100,7 @@ def download(dl, args): "Error: access denied. Try providing credentials with " "--username and --password." ) - if args.verbose >= 2: + if args.verbose >= wikiget.VERY_VERBOSE: print("Full error message:") for i in e.args: print(i) @@ -113,7 +113,7 @@ def download(dl, args): file_size = file.imageinfo["size"] file_sha1 = file.imageinfo["sha1"] - if args.verbose >= 1: + if args.verbose >= wikiget.STD_VERBOSE: print( f"Info: downloading '{filename}' " f"({file_size} bytes) from {site.host}", @@ -136,7 +136,7 @@ def download(dl, args): sys.exit(1) else: # download the file(s) - if args.verbose >= 1: + if args.verbose >= wikiget.STD_VERBOSE: leave_bars = True else: leave_bars = False @@ -145,23 +145,23 @@ def download(dl, args): total=file_size, unit="B", unit_scale=True, - unit_divisor=CHUNKSIZE, + unit_divisor=wikiget.CHUNKSIZE, ) as progress_bar: with fd: res = site.connection.get(file_url, stream=True) progress_bar.set_postfix(file=dest, refresh=False) - for chunk in res.iter_content(CHUNKSIZE): + for chunk in res.iter_content(wikiget.CHUNKSIZE): fd.write(chunk) progress_bar.update(len(chunk)) # verify file integrity and optionally print details dl_sha1 = verify_hash(dest) - if args.verbose >= 1: + if args.verbose >= wikiget.STD_VERBOSE: print(f"Info: downloaded file SHA1 is {dl_sha1}") print(f"Info: server file SHA1 is {file_sha1}") if dl_sha1 == file_sha1: - if args.verbose >= 1: + if args.verbose >= wikiget.STD_VERBOSE: print("Info: hashes match!") # at this point, we've successfully downloaded the file else: diff --git a/wikiget/validations.py b/wikiget/validations.py index c0aa90d..dc70df4 100644 --- a/wikiget/validations.py +++ b/wikiget/validations.py @@ -55,7 +55,7 @@ def verify_hash(filename): :param filename: name of the file to calculate a hash for :return: hash digest """ - hasher = hashlib.sha1() + hasher = hashlib.sha1() # noqa: S324 with open(filename, "rb") as dl: buf = dl.read(BLOCKSIZE) while len(buf) > 0: diff --git a/wikiget/wikiget.py b/wikiget/wikiget.py index 33d619b..ba36766 100644 --- a/wikiget/wikiget.py +++ b/wikiget/wikiget.py @@ -19,7 +19,7 @@ import argparse import logging import sys -from wikiget import DEFAULT_PATH, DEFAULT_SITE, wikiget_version +import wikiget from wikiget.dl import download @@ -54,7 +54,10 @@ def main(): """, ) parser.add_argument( - "-V", "--version", action="version", version=f"%(prog)s {wikiget_version}" + "-V", + "--version", + action="version", + version=f"%(prog)s {wikiget.wikiget_version}", ) message_options = parser.add_mutually_exclusive_group() message_options.add_argument( @@ -73,13 +76,13 @@ def main(): parser.add_argument( "-s", "--site", - default=DEFAULT_SITE, + default=wikiget.DEFAULT_SITE, help="MediaWiki site to download from (default: %(default)s)", ) parser.add_argument( "-p", "--path", - default=DEFAULT_PATH, + default=wikiget.DEFAULT_PATH, help="MediaWiki site path, where api.php is located (default: %(default)s)", ) parser.add_argument( @@ -102,15 +105,15 @@ def main(): args = parser.parse_args() # print API and debug messages in verbose mode - if args.verbose >= 2: + if args.verbose >= wikiget.VERY_VERBOSE: logging.basicConfig(level=logging.DEBUG) - elif args.verbose >= 1: + elif args.verbose >= wikiget.STD_VERBOSE: logging.basicConfig(level=logging.WARNING) if args.batch: # batch download mode input_file = args.FILE - if args.verbose >= 1: + if args.verbose >= wikiget.STD_VERBOSE: print(f"Info: using batch file '{input_file}'") try: fd = open(input_file) @@ -121,8 +124,7 @@ def main(): else: with fd: for _, line in enumerate(fd): - line = line.strip() - download(line, args) + download(line.strip(), args) else: # single download mode dl = args.FILE |
