implement 'actions'

continuation of d37e7f48
but more versatile and extendable

Example:

"actions": [
    # change debug messages to info
    ["debug", "level ~info"],

    # change exit status to a non-zero value
    ["info:^No results for", "status |= 1"],

    # exit with status 2 on 429
    ["warning:429", "exit 2"],

    # restart extractor when no cookies found
    ["warning:^[Nn]o .*cookies", "restart"]
]
This commit is contained in:
Mike Fährmann
2023-03-10 22:08:10 +01:00
parent 817fc0fbd1
commit 4235d412c4
3 changed files with 138 additions and 35 deletions

View File

@@ -6,7 +6,6 @@
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
import re
import sys
import errno
import logging
@@ -33,15 +32,11 @@ class Job():
self.kwdict = {}
self.status = 0
hooks = extr.config("hooks")
if hooks:
if isinstance(hooks, dict):
hooks = hooks.items()
self._wrap_logger = self._wrap_logger_hooks
self._logger_hooks = [
(re.compile(pattern).search, hook)
for pattern, hook in hooks
]
actions = extr.config("actions")
if actions:
from .actions import parse
self._logger_actions = parse(actions)
self._wrap_logger = self._wrap_logger_actions
path_proxy = output.PathfmtProxy(self)
self._logger_extra = {
@@ -211,11 +206,10 @@ class Job():
return self._wrap_logger(logging.getLogger(name))
def _wrap_logger(self, logger):
return output.LoggerAdapter(logger, self._logger_extra)
return output.LoggerAdapter(logger, self)
def _wrap_logger_hooks(self, logger):
return output.LoggerAdapterEx(
logger, self._logger_extra, self)
def _wrap_logger_actions(self, logger):
return output.LoggerAdapterActions(logger, self)
def _write_unsupported(self, url):
if self.ulog: