aboutsummaryrefslogtreecommitdiff
path: root/src/wikiget/wikiget.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/wikiget/wikiget.py')
-rw-r--r--src/wikiget/wikiget.py71
1 files changed, 62 insertions, 9 deletions
diff --git a/src/wikiget/wikiget.py b/src/wikiget/wikiget.py
index ba36766..b9a227f 100644
--- a/src/wikiget/wikiget.py
+++ b/src/wikiget/wikiget.py
@@ -101,30 +101,83 @@ def main():
"filename per line",
action="store_true",
)
+ parser.add_argument(
+ "-l",
+ "--logfile",
+ default="",
+ help="save log output to LOGFILE"
+ )
args = parser.parse_args()
- # print API and debug messages in verbose mode
+ loglevel = logging.WARNING
if args.verbose >= wikiget.VERY_VERBOSE:
- logging.basicConfig(level=logging.DEBUG)
+ # this includes API and library messages
+ loglevel = logging.DEBUG
elif args.verbose >= wikiget.STD_VERBOSE:
- logging.basicConfig(level=logging.WARNING)
+ loglevel = logging.INFO
+ elif args.quiet:
+ loglevel = logging.ERROR
+
+ # configure logging:
+ # console log level is set via -v, -vv, and -q options
+ # file log level is always info (TODO: add debug option)
+ if args.logfile:
+ # log to console and file
+ logging.basicConfig(
+ level=logging.INFO,
+ format="%(asctime)s [%(levelname)-7s] %(message)s",
+ filename=args.logfile
+ )
+
+ console = logging.StreamHandler()
+ # TODO: even when loglevel is set to logging.DEBUG,
+ # debug messages aren't printing to console
+ console.setLevel(loglevel)
+ console.setFormatter(
+ logging.Formatter("[%(levelname)s] %(message)s")
+ )
+ logging.getLogger("").addHandler(console)
+ else:
+ # log only to console
+ logging.basicConfig(
+ level=loglevel,
+ format="[%(levelname)s] %(message)s"
+ )
+
+ # log events are appended to the file if it already exists,
+ # so note the start of a new download session
+ logging.info(f"Starting download session using wikiget {wikiget.wikiget_version}")
+ # logging.info(f"Log level is set to {loglevel}")
if args.batch:
# batch download mode
input_file = args.FILE
- if args.verbose >= wikiget.STD_VERBOSE:
- print(f"Info: using batch file '{input_file}'")
+ dl_list = []
+
+ logging.info(f"Using batch file '{input_file}'.")
+
try:
- fd = open(input_file)
+ fd = open(input_file, "r")
except OSError 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):
- download(line.strip(), 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 '{url}' at line {line_num}:")
+ download(url, args)
else:
# single download mode
dl = args.FILE