implement -e/--error-file as a logging handler
similar to --write-unsupported
This commit is contained in:
@@ -4422,14 +4422,17 @@ Description
|
||||
output.errorfile
|
||||
----------------
|
||||
Type
|
||||
|Path|_
|
||||
* |Path|_
|
||||
* |Logging Configuration|_
|
||||
Description
|
||||
File to write input URLs which returned an error to.
|
||||
|
||||
The default format string here is also ``"{message}"``.
|
||||
|
||||
When combined with
|
||||
``-I``/``--input-file-comment`` or
|
||||
``-x``/``--input-file-delete``,
|
||||
this option will cause all input URLs from these files
|
||||
this option will cause *all* input URLs from these files
|
||||
to be commented/deleted after processing them
|
||||
and not just successful ones.
|
||||
|
||||
|
||||
@@ -226,18 +226,26 @@ def main():
|
||||
else:
|
||||
jobtype = args.jobtype or job.DownloadJob
|
||||
|
||||
input_manager = InputManager()
|
||||
input_manager.log = input_log = logging.getLogger("inputfile")
|
||||
|
||||
# unsupported file logging handler
|
||||
handler = output.setup_logging_handler(
|
||||
"unsupportedfile", fmt="{message}")
|
||||
if handler:
|
||||
ulog = logging.getLogger("unsupported")
|
||||
ulog = job.Job.ulog = logging.getLogger("unsupported")
|
||||
ulog.addHandler(handler)
|
||||
ulog.propagate = False
|
||||
job.Job.ulog = ulog
|
||||
|
||||
# error file logging handler
|
||||
handler = output.setup_logging_handler(
|
||||
"errorfile", fmt="{message}", mode="a")
|
||||
if handler:
|
||||
elog = input_manager.err = logging.getLogger("errorfile")
|
||||
elog.addHandler(handler)
|
||||
elog.propagate = False
|
||||
|
||||
# collect input URLs
|
||||
input_manager = InputManager()
|
||||
input_manager.log = input_log = logging.getLogger("inputfile")
|
||||
input_manager.add_list(args.urls)
|
||||
|
||||
if args.input_files:
|
||||
@@ -249,11 +257,6 @@ def main():
|
||||
input_log.error(exc)
|
||||
return getattr(exc, "code", 128)
|
||||
|
||||
error_file = (args.error_file or
|
||||
config.get(("output",), "errorfile"))
|
||||
if error_file:
|
||||
input_manager.error_file(error_file)
|
||||
|
||||
pformat = config.get(("output",), "progress", True)
|
||||
if pformat and len(input_manager.urls) > 1 and \
|
||||
args.loglevel < logging.ERROR:
|
||||
@@ -308,12 +311,12 @@ class InputManager():
|
||||
def __init__(self):
|
||||
self.urls = []
|
||||
self.files = ()
|
||||
self.log = self.err = None
|
||||
|
||||
self._url = ""
|
||||
self._item = None
|
||||
self._index = 0
|
||||
self._pformat = None
|
||||
self._error_fp = None
|
||||
|
||||
def add_url(self, url):
|
||||
self.urls.append(url)
|
||||
@@ -438,15 +441,6 @@ class InputManager():
|
||||
else:
|
||||
append(url)
|
||||
|
||||
def error_file(self, path):
|
||||
try:
|
||||
path = util.expand_path(path)
|
||||
self._error_fp = open(path, "a", encoding="utf-8")
|
||||
except Exception as exc:
|
||||
self.log.warning(
|
||||
"Unable to open error file (%s: %s)",
|
||||
exc.__class__.__name__, exc)
|
||||
|
||||
def progress(self, pformat=True):
|
||||
if pformat is True:
|
||||
pformat = "[{current}/{total}] {url}\n"
|
||||
@@ -462,21 +456,17 @@ class InputManager():
|
||||
self._rewrite()
|
||||
|
||||
def error(self):
|
||||
if self._error_fp:
|
||||
if self.err:
|
||||
if self._item:
|
||||
url, path, action, indicies = self._item
|
||||
lines = self.files[path]
|
||||
out = "".join(lines[i] for i in indicies)
|
||||
if out and out[-1] == "\n":
|
||||
out = out[:-1]
|
||||
self._rewrite()
|
||||
else:
|
||||
out = str(self._url) + "\n"
|
||||
|
||||
try:
|
||||
self._error_fp.write(out)
|
||||
except Exception as exc:
|
||||
self.log.warning(
|
||||
"Unable to update '%s' (%s: %s)",
|
||||
self._error_fp.name, exc.__class__.__name__, exc)
|
||||
out = str(self._url)
|
||||
self.err.info(out)
|
||||
|
||||
def _rewrite(self):
|
||||
url, path, action, indicies = self._item
|
||||
|
||||
@@ -288,7 +288,7 @@ def build_parser():
|
||||
)
|
||||
output.add_argument(
|
||||
"-e", "--error-file",
|
||||
dest="error_file", metavar="FILE",
|
||||
dest="errorfile", metavar="FILE", action=ConfigAction,
|
||||
help="Add input URLs which returned an error to FILE",
|
||||
)
|
||||
output.add_argument(
|
||||
|
||||
@@ -210,7 +210,7 @@ def configure_logging(loglevel):
|
||||
root.setLevel(minlevel)
|
||||
|
||||
|
||||
def setup_logging_handler(key, fmt=LOG_FORMAT, lvl=LOG_LEVEL):
|
||||
def setup_logging_handler(key, fmt=LOG_FORMAT, lvl=LOG_LEVEL, mode="w"):
|
||||
"""Setup a new logging handler"""
|
||||
opts = config.interpolate(("output",), key)
|
||||
if not opts:
|
||||
@@ -219,7 +219,7 @@ def setup_logging_handler(key, fmt=LOG_FORMAT, lvl=LOG_LEVEL):
|
||||
opts = {"path": opts}
|
||||
|
||||
path = opts.get("path")
|
||||
mode = opts.get("mode", "w")
|
||||
mode = opts.get("mode", mode)
|
||||
encoding = opts.get("encoding", "utf-8")
|
||||
try:
|
||||
path = util.expand_path(path)
|
||||
|
||||
Reference in New Issue
Block a user