From 27ae152f5766b51e926ca3fed4e1cc5dc5a18ea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Sat, 11 Mar 2017 01:47:57 +0100 Subject: [PATCH] use logging to report errors --- gallery_dl/__init__.py | 23 +++++++++++++---------- gallery_dl/config.py | 3 ++- gallery_dl/job.py | 13 ++++++------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/gallery_dl/__init__.py b/gallery_dl/__init__.py index c3151036..2123bd14 100644 --- a/gallery_dl/__init__.py +++ b/gallery_dl/__init__.py @@ -26,6 +26,8 @@ import json from . import config, extractor, job, exception from .version import __version__ +log = logging.getLogger("gallery-dl") + def build_cmdline_parser(): parser = argparse.ArgumentParser( @@ -114,18 +116,21 @@ def parse_option(opt): pass config.set(key.split("."), value) except ValueError: - print("Invalid 'key=value' pair:", opt, file=sys.stderr) + log.warning("Invalid 'key=value' pair: %s", opt) def initialize_logging(): - logging.basicConfig( - format="[%(name)s][%(levelname)s] %(message)s", - level=logging.INFO - ) # convert levelnames to lowercase for level in (10, 20, 30, 40, 50): name = logging.getLevelName(level) logging.addLevelName(level, name.lower()) + # setup basic logging to stderr + formatter = logging.Formatter("[%(name)s][%(levelname)s] %(message)s") + handler = logging.StreamHandler() + handler.setFormatter(formatter) + root = logging.getLogger() + root.setLevel(logging.INFO) + root.addHandler(handler) def sanatize_input(file): @@ -139,6 +144,7 @@ def main(): try: initialize_logging() config.load() + parser = build_cmdline_parser() args = parser.parse_args() @@ -146,7 +152,6 @@ def main(): config.load(*args.cfgfiles, strict=True) if args.yamlfiles: config.load(*args.yamlfiles, format="yaml", strict=True) - if args.dest: config.set(("base-directory",), args.dest) if args.username: @@ -157,7 +162,6 @@ def main(): config.set(("images",), args.images) if args.chapters: config.set(("chapters",), args.chapters) - for opt in args.option: parse_option(opt) @@ -194,14 +198,13 @@ def main(): import itertools urls = itertools.chain(urls, sanatize_input(file)) except OSError as e: - print(e) + log.error(e) for url in urls: try: jobtype(url).run() except exception.NoExtractorError: - print("No suitable extractor found for URL '", url, "'", - sep="", file=sys.stderr) + log.error("No suitable extractor found for '%s'", url) except KeyboardInterrupt: print("\nKeyboardInterrupt", file=sys.stderr) diff --git a/gallery_dl/config.py b/gallery_dl/config.py index 85a1d5ce..5500aa27 100644 --- a/gallery_dl/config.py +++ b/gallery_dl/config.py @@ -13,13 +13,14 @@ import json import os.path import logging +log = logging.getLogger("config") + # -------------------------------------------------------------------- # public interface def load(*files, format="json", strict=False): """Load JSON configuration files""" - log = logging.getLogger("config") configfiles = files or _default_configs if format == "yaml": diff --git a/gallery_dl/job.py b/gallery_dl/job.py index 28350376..da041a54 100644 --- a/gallery_dl/job.py +++ b/gallery_dl/job.py @@ -6,7 +6,6 @@ # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. -import sys import json import hashlib from . import extractor, downloader, config, util, path, output, exception @@ -37,18 +36,18 @@ class Job(): def run(self): """Execute or run the job""" try: + log = self.extractor.log for msg in self.extractor: self.dispatch(msg) except exception.AuthenticationError: - print("Authentication failed. Please provide a valid " - "username/password pair.", file=sys.stderr) + log.error("Authentication failed. Please provide a valid " + "username/password pair.") except exception.AuthorizationError: - print("You do not have permission to access the resource ", - "at '", self.url, "'", sep="", file=sys.stderr) + log.error("You do not have permission to access the resource " + "at '%s'", self.url) except exception.NotFoundError as err: res = str(err) or "resource (gallery/image/user)" - print("The ", res, " at '", self.url, "' does not exist", - sep="", file=sys.stderr) + log.error("The %s at '%s' does not exist", res, self.url) except exception.StopExtraction: pass