diff options
| author | Cody Logan <clpo13@gmail.com> | 2021-12-06 16:19:03 -0800 |
|---|---|---|
| committer | Cody Logan <clpo13@gmail.com> | 2021-12-06 16:19:03 -0800 |
| commit | d96ad1118691003506c9b666af7bd93514296916 (patch) | |
| tree | 99ee245f7134fabe2f36c3630336458fb9bbc44e /wikiget/wikiget.py | |
| parent | 5014eb73f0ee9b9aff4926b0f0538f0147b29460 (diff) | |
| download | wikiget-d96ad1118691003506c9b666af7bd93514296916.tar.gz wikiget-d96ad1118691003506c9b666af7bd93514296916.zip | |
Consistent message logging
Use Python's logging faciility for messages
instead of printing to stdout (except for download
progress bars).
Diffstat (limited to 'wikiget/wikiget.py')
| -rw-r--r-- | wikiget/wikiget.py | 42 |
1 files changed, 32 insertions, 10 deletions
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 |
