From d96ad1118691003506c9b666af7bd93514296916 Mon Sep 17 00:00:00 2001 From: Cody Logan Date: Mon, 6 Dec 2021 16:19:03 -0800 Subject: Consistent message logging Use Python's logging faciility for messages instead of printing to stdout (except for download progress bars). --- wikiget/wikiget.py | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) (limited to 'wikiget/wikiget.py') diff --git a/wikiget/wikiget.py b/wikiget/wikiget.py index 1e2e9ed..dfc6027 100644 --- a/wikiget/wikiget.py +++ b/wikiget/wikiget.py @@ -81,29 +81,51 @@ def main(): args = parser.parse_args() - # print API and debug messages in verbose mode + loglevel = logging.WARNING if args.verbose >= 2: - logging.basicConfig(level=logging.DEBUG) + # this includes API and library messages + loglevel = logging.DEBUG elif args.verbose >= 1: - logging.basicConfig(level=logging.WARNING) + loglevel = logging.INFO + elif args.quiet: + loglevel = logging.ERROR + + # set up logger + # TODO: optionally save to log file + logging.basicConfig( + level=loglevel, + # format="%(asctime)s [%(levelname)s] %(message)s" + format="[%(levelname)s] %(message)s" + ) if args.batch: # batch download mode input_file = args.FILE - if args.verbose >= 1: - print(f"Info: using batch file '{input_file}'") + dl_list = [] + + logging.info(f"Using batch file '{input_file}'.") + try: fd = open(input_file, 'r') except IOError as e: - print('File could not be read. ' - 'The following error was encountered:') - print(e) + logging.error("File could not be read. " + "The following error was encountered:") + logging.error(e) sys.exit(1) else: with fd: + # store file contents in memory in case something + # happens to the file while we're downloading for _, line in enumerate(fd): - line = line.strip() - download(line, args) + dl_list.append(line) + + # TODO: validate file contents before download process starts + for line_num, url in enumerate(dl_list, start=1): + url = url.strip() + # keep track of batch file line numbers for + # debugging/logging purposes + logging.info(f"Downloading file {line_num} ({url}):") + download(url, args) else: # single download mode dl = args.FILE -- cgit v1.2.3