Skip to content

Commit

Permalink
add 'hooks' option
Browse files Browse the repository at this point in the history
Very much a work in progress.

At the moment, it allows to
- wait and restart an extractor (mikf#3338)
- change the exit code (mikf#3630)
- change the log level of a logging message
based on the contents of a logging message
  • Loading branch information
mikf committed Feb 13, 2023
1 parent 8fb043e commit d37e7f4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
15 changes: 15 additions & 0 deletions gallery_dl/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# 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
Expand All @@ -32,6 +33,16 @@ def __init__(self, extr, parent=None):
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
]

path_proxy = output.PathfmtProxy(self)
self._logger_extra = {
"job" : self,
Expand Down Expand Up @@ -202,6 +213,10 @@ def get_logger(self, name):
def _wrap_logger(self, logger):
return output.LoggerAdapter(logger, self._logger_extra)

def _wrap_logger_hooks(self, logger):
return output.LoggerAdapterEx(
logger, self._logger_extra, self)

def _write_unsupported(self, url):
if self.ulog:
self.ulog.info(url)
Expand Down
42 changes: 39 additions & 3 deletions gallery_dl/output.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

# Copyright 2015-2022 Mike Fährmann
# Copyright 2015-2023 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
Expand All @@ -10,8 +10,9 @@
import sys
import shutil
import logging
import functools
import unicodedata
from . import config, util, formatter
from . import config, util, formatter, exception


# --------------------------------------------------------------------
Expand All @@ -23,7 +24,7 @@


class Logger(logging.Logger):
"""Custom logger that includes extra info in log records"""
"""Custom Logger that includes extra info in log records"""

def makeRecord(self, name, level, fn, lno, msg, args, exc_info,
func=None, extra=None, sinfo=None,
Expand Down Expand Up @@ -63,6 +64,41 @@ def error(self, msg, *args, **kwargs):
self.logger._log(logging.ERROR, msg, args, **kwargs)


class LoggerAdapterEx():

def __init__(self, logger, extra, job):
self.logger = logger
self.extra = extra
self.job = job

self.debug = functools.partial(self.log, logging.DEBUG)
self.info = functools.partial(self.log, logging.INFO)
self.warning = functools.partial(self.log, logging.WARNING)
self.error = functools.partial(self.log, logging.ERROR)

def log(self, level, msg, *args, **kwargs):
if args:
msg = msg % args
args = None

for search, action in self.job._logger_hooks:
match = search(msg)
if match:
if action == "wait+restart":
kwargs["extra"] = self.extra
self.logger._log(level, msg, args, **kwargs)
input("Press Enter to continue")
raise exception.RestartExtraction()
elif action.startswith("~"):
level = logging._nameToLevel[action[1:]]
elif action.startswith("|"):
self.job.status |= int(action[1:])

if self.logger.isEnabledFor(level):
kwargs["extra"] = self.extra
self.logger._log(level, msg, args, **kwargs)


class PathfmtProxy():
__slots__ = ("job",)

Expand Down

0 comments on commit d37e7f4

Please sign in to comment.