use logging to report errors

This commit is contained in:
Mike Fährmann
2017-03-11 01:47:57 +01:00
parent dfe2c2dced
commit 27ae152f57
3 changed files with 21 additions and 18 deletions

View File

@@ -26,6 +26,8 @@ import json
from . import config, extractor, job, exception from . import config, extractor, job, exception
from .version import __version__ from .version import __version__
log = logging.getLogger("gallery-dl")
def build_cmdline_parser(): def build_cmdline_parser():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
@@ -114,18 +116,21 @@ def parse_option(opt):
pass pass
config.set(key.split("."), value) config.set(key.split("."), value)
except ValueError: except ValueError:
print("Invalid 'key=value' pair:", opt, file=sys.stderr) log.warning("Invalid 'key=value' pair: %s", opt)
def initialize_logging(): def initialize_logging():
logging.basicConfig(
format="[%(name)s][%(levelname)s] %(message)s",
level=logging.INFO
)
# convert levelnames to lowercase # convert levelnames to lowercase
for level in (10, 20, 30, 40, 50): for level in (10, 20, 30, 40, 50):
name = logging.getLevelName(level) name = logging.getLevelName(level)
logging.addLevelName(level, name.lower()) 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): def sanatize_input(file):
@@ -139,6 +144,7 @@ def main():
try: try:
initialize_logging() initialize_logging()
config.load() config.load()
parser = build_cmdline_parser() parser = build_cmdline_parser()
args = parser.parse_args() args = parser.parse_args()
@@ -146,7 +152,6 @@ def main():
config.load(*args.cfgfiles, strict=True) config.load(*args.cfgfiles, strict=True)
if args.yamlfiles: if args.yamlfiles:
config.load(*args.yamlfiles, format="yaml", strict=True) config.load(*args.yamlfiles, format="yaml", strict=True)
if args.dest: if args.dest:
config.set(("base-directory",), args.dest) config.set(("base-directory",), args.dest)
if args.username: if args.username:
@@ -157,7 +162,6 @@ def main():
config.set(("images",), args.images) config.set(("images",), args.images)
if args.chapters: if args.chapters:
config.set(("chapters",), args.chapters) config.set(("chapters",), args.chapters)
for opt in args.option: for opt in args.option:
parse_option(opt) parse_option(opt)
@@ -194,14 +198,13 @@ def main():
import itertools import itertools
urls = itertools.chain(urls, sanatize_input(file)) urls = itertools.chain(urls, sanatize_input(file))
except OSError as e: except OSError as e:
print(e) log.error(e)
for url in urls: for url in urls:
try: try:
jobtype(url).run() jobtype(url).run()
except exception.NoExtractorError: except exception.NoExtractorError:
print("No suitable extractor found for URL '", url, "'", log.error("No suitable extractor found for '%s'", url)
sep="", file=sys.stderr)
except KeyboardInterrupt: except KeyboardInterrupt:
print("\nKeyboardInterrupt", file=sys.stderr) print("\nKeyboardInterrupt", file=sys.stderr)

View File

@@ -13,13 +13,14 @@ import json
import os.path import os.path
import logging import logging
log = logging.getLogger("config")
# -------------------------------------------------------------------- # --------------------------------------------------------------------
# public interface # public interface
def load(*files, format="json", strict=False): def load(*files, format="json", strict=False):
"""Load JSON configuration files""" """Load JSON configuration files"""
log = logging.getLogger("config")
configfiles = files or _default_configs configfiles = files or _default_configs
if format == "yaml": if format == "yaml":

View File

@@ -6,7 +6,6 @@
# it under the terms of the GNU General Public License version 2 as # it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation. # published by the Free Software Foundation.
import sys
import json import json
import hashlib import hashlib
from . import extractor, downloader, config, util, path, output, exception from . import extractor, downloader, config, util, path, output, exception
@@ -37,18 +36,18 @@ class Job():
def run(self): def run(self):
"""Execute or run the job""" """Execute or run the job"""
try: try:
log = self.extractor.log
for msg in self.extractor: for msg in self.extractor:
self.dispatch(msg) self.dispatch(msg)
except exception.AuthenticationError: except exception.AuthenticationError:
print("Authentication failed. Please provide a valid " log.error("Authentication failed. Please provide a valid "
"username/password pair.", file=sys.stderr) "username/password pair.")
except exception.AuthorizationError: except exception.AuthorizationError:
print("You do not have permission to access the resource ", log.error("You do not have permission to access the resource "
"at '", self.url, "'", sep="", file=sys.stderr) "at '%s'", self.url)
except exception.NotFoundError as err: except exception.NotFoundError as err:
res = str(err) or "resource (gallery/image/user)" res = str(err) or "resource (gallery/image/user)"
print("The ", res, " at '", self.url, "' does not exist", log.error("The %s at '%s' does not exist", res, self.url)
sep="", file=sys.stderr)
except exception.StopExtraction: except exception.StopExtraction:
pass pass