| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2023-2026 Mike Fährmann |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License version 2 as |
| 7 |
# published by the Free Software Foundation. |
| 8 |
|
| 9 |
""" """ |
| 10 |
|
| 11 |
import time |
| 12 |
import logging |
| 13 |
import operator |
| 14 |
import functools |
| 15 |
from . import util, exception |
| 16 |
|
| 17 |
|
| 18 |
def parse(spec): |
| 19 |
if isinstance(spec, str): |
| 20 |
type, _, args = spec.partition(" ") |
| 21 |
before, after = ACTIONS[type](args) |
| 22 |
return before if after is None else after |
| 23 |
|
| 24 |
actions_before, actions_after = [], [] |
| 25 |
for s in spec: |
| 26 |
type, _, args = s.partition(" ") |
| 27 |
before, after = ACTIONS[type](args) |
| 28 |
if before is not None: |
| 29 |
actions_before.append(before) |
| 30 |
if after is not None: |
| 31 |
actions_after.append(after) |
| 32 |
actions_before.extend(actions_after) |
| 33 |
return _chain_actions(actions_before) |
| 34 |
|
| 35 |
|
| 36 |
def parse_logging(actionspec): |
| 37 |
if isinstance(actionspec, dict): |
| 38 |
actionspec = actionspec.items() |
| 39 |
|
| 40 |
actions = {} |
| 41 |
actions[-logging.DEBUG] = actions_bd = [] |
| 42 |
actions[-logging.INFO] = actions_bi = [] |
| 43 |
actions[-logging.WARNING] = actions_bw = [] |
| 44 |
actions[-logging.ERROR] = actions_be = [] |
| 45 |
actions[logging.DEBUG] = actions_ad = [] |
| 46 |
actions[logging.INFO] = actions_ai = [] |
| 47 |
actions[logging.WARNING] = actions_aw = [] |
| 48 |
actions[logging.ERROR] = actions_ae = [] |
| 49 |
|
| 50 |
for event, spec in actionspec: |
| 51 |
level, _, pattern = event.partition(":") |
| 52 |
search = util.re(pattern).search if pattern else util.true |
| 53 |
|
| 54 |
if isinstance(spec, str): |
| 55 |
type, _, args = spec.partition(" ") |
| 56 |
before, after = ACTIONS[type](args) |
| 57 |
else: |
| 58 |
actions_before = [] |
| 59 |
actions_after = [] |
| 60 |
for s in spec: |
| 61 |
type, _, args = s.partition(" ") |
| 62 |
before, after = ACTIONS[type](args) |
| 63 |
if before: |
| 64 |
actions_before.append(before) |
| 65 |
if after: |
| 66 |
actions_after.append(after) |
| 67 |
before = _chain_actions(actions_before) |
| 68 |
after = _chain_actions(actions_after) |
| 69 |
|
| 70 |
level = level.strip() |
| 71 |
if not level or level == "*": |
| 72 |
if before: |
| 73 |
action = (search, before) |
| 74 |
actions_bd.append(action) |
| 75 |
actions_bi.append(action) |
| 76 |
actions_bw.append(action) |
| 77 |
actions_be.append(action) |
| 78 |
if after: |
| 79 |
action = (search, after) |
| 80 |
actions_ad.append(action) |
| 81 |
actions_ai.append(action) |
| 82 |
actions_aw.append(action) |
| 83 |
actions_ae.append(action) |
| 84 |
else: |
| 85 |
level = _level_to_int(level) |
| 86 |
if before: |
| 87 |
actions[-level].append((search, before)) |
| 88 |
if after: |
| 89 |
actions[level].append((search, after)) |
| 90 |
|
| 91 |
return actions |
| 92 |
|
| 93 |
|
| 94 |
def parse_signals(actionspec): |
| 95 |
import signal |
| 96 |
|
| 97 |
if isinstance(actionspec, dict): |
| 98 |
actionspec = actionspec.items() |
| 99 |
|
| 100 |
for signal_name, spec in actionspec: |
| 101 |
signal_num = getattr(signal, signal_name, None) |
| 102 |
if signal_num is None: |
| 103 |
log = logging.getLogger("gallery-dl") |
| 104 |
log.warning("signal '%s' is not defined", signal_name) |
| 105 |
continue |
| 106 |
signal.signal(signal_num, signals_handler(parse(spec))) |
| 107 |
|
| 108 |
|
| 109 |
class LoggerAdapter(): |
| 110 |
|
| 111 |
def __init__(self, logger, job): |
| 112 |
self.logger = logger |
| 113 |
self.extra = job._logger_extra |
| 114 |
self.actions = job._logger_actions |
| 115 |
|
| 116 |
self.debug = functools.partial(self.log, logging.DEBUG) |
| 117 |
self.info = functools.partial(self.log, logging.INFO) |
| 118 |
self.warning = functools.partial(self.log, logging.WARNING) |
| 119 |
self.error = functools.partial(self.log, logging.ERROR) |
| 120 |
|
| 121 |
def log(self, level, msg, *args, **kwargs): |
| 122 |
msg = str(msg) |
| 123 |
if args: |
| 124 |
msg = msg % args |
| 125 |
|
| 126 |
before = self.actions[-level] |
| 127 |
after = self.actions[level] |
| 128 |
|
| 129 |
if before: |
| 130 |
args = self.extra.copy() |
| 131 |
args["level"] = level |
| 132 |
|
| 133 |
for cond, action in before: |
| 134 |
if cond(msg): |
| 135 |
action(args) |
| 136 |
|
| 137 |
level = args["level"] |
| 138 |
|
| 139 |
if self.logger.isEnabledFor(level): |
| 140 |
kwargs["extra"] = self.extra |
| 141 |
self.logger._log(level, msg, (), **kwargs) |
| 142 |
|
| 143 |
if after: |
| 144 |
args = self.extra.copy() |
| 145 |
for cond, action in after: |
| 146 |
if cond(msg): |
| 147 |
action(args) |
| 148 |
|
| 149 |
def traceback(self, exc): |
| 150 |
if self.logger.isEnabledFor(logging.DEBUG): |
| 151 |
self.logger._log( |
| 152 |
logging.DEBUG, "", None, exc_info=exc, extra=self.extra) |
| 153 |
|
| 154 |
|
| 155 |
def _level_to_int(level): |
| 156 |
try: |
| 157 |
return logging._nameToLevel[level] |
| 158 |
except KeyError: |
| 159 |
return int(level) |
| 160 |
|
| 161 |
|
| 162 |
def _chain_actions(actions): |
| 163 |
def _chain(args): |
| 164 |
for action in actions: |
| 165 |
action(args) |
| 166 |
return _chain |
| 167 |
|
| 168 |
|
| 169 |
def signals_handler(action, args={}): |
| 170 |
def handler(signal_num, frame): |
| 171 |
action(args) |
| 172 |
return handler |
| 173 |
|
| 174 |
|
| 175 |
# -------------------------------------------------------------------- |
| 176 |
|
| 177 |
def action_print(opts): |
| 178 |
def _print(_): |
| 179 |
print(opts) |
| 180 |
return None, _print |
| 181 |
|
| 182 |
|
| 183 |
def action_status(opts): |
| 184 |
op, value = util.re(r"\s*([&|^=])=?\s*(\d+)").match(opts).groups() |
| 185 |
|
| 186 |
op = { |
| 187 |
"&": operator.and_, |
| 188 |
"|": operator.or_, |
| 189 |
"^": operator.xor, |
| 190 |
"=": lambda x, y: y, |
| 191 |
}[op] |
| 192 |
|
| 193 |
value = int(value) |
| 194 |
|
| 195 |
def _status(args): |
| 196 |
args["job"].status = op(args["job"].status, value) |
| 197 |
return _status, None |
| 198 |
|
| 199 |
|
| 200 |
def action_level(opts): |
| 201 |
level = _level_to_int(opts.lstrip(" ~=")) |
| 202 |
|
| 203 |
def _level(args): |
| 204 |
args["level"] = level |
| 205 |
return _level, None |
| 206 |
|
| 207 |
|
| 208 |
def action_exec(opts): |
| 209 |
def _exec(_): |
| 210 |
util.Popen(opts, shell=True).wait() |
| 211 |
return None, _exec |
| 212 |
|
| 213 |
|
| 214 |
def action_wait(opts): |
| 215 |
if opts: |
| 216 |
seconds = util.build_duration_func(opts) |
| 217 |
|
| 218 |
def _wait(args): |
| 219 |
time.sleep(seconds()) |
| 220 |
else: |
| 221 |
def _wait(args): |
| 222 |
input("Press Enter to continue") |
| 223 |
|
| 224 |
return None, _wait |
| 225 |
|
| 226 |
|
| 227 |
def action_flag(opts): |
| 228 |
flag, value = util.re( |
| 229 |
r"(?i)(file|post|child|download|clear)(?:\s*[= ]\s*(.+))?" |
| 230 |
).match(opts).groups() |
| 231 |
flag = flag.upper() |
| 232 |
|
| 233 |
if flag == "CLEAR": |
| 234 |
return (lambda _: util.FLAGS.clear()), None |
| 235 |
|
| 236 |
if value is None: |
| 237 |
value = "stop" |
| 238 |
elif value == "skip": |
| 239 |
value = False |
| 240 |
elif value == "clear": |
| 241 |
value = None |
| 242 |
elif value == "toggle": |
| 243 |
def _flag_toggle(args): |
| 244 |
util.FLAGS.__dict__[flag] = \ |
| 245 |
"stop" if util.FLAGS.__dict__[flag] is None else None |
| 246 |
del value |
| 247 |
return _flag_toggle, None |
| 248 |
else: |
| 249 |
value = value.lower() |
| 250 |
|
| 251 |
def _flag(args): |
| 252 |
util.FLAGS.__dict__[flag] = value |
| 253 |
return _flag, None |
| 254 |
|
| 255 |
|
| 256 |
def action_keyword(opts): |
| 257 |
name, _, value = opts.partition(" ") |
| 258 |
|
| 259 |
try: |
| 260 |
value = value.strip() |
| 261 |
value = util.json_loads(value) |
| 262 |
except Exception: |
| 263 |
pass |
| 264 |
|
| 265 |
def _keyword(args): |
| 266 |
args["job"].kwdict[name] = value |
| 267 |
return _keyword, None |
| 268 |
|
| 269 |
|
| 270 |
def action_raise(opts): |
| 271 |
name, _, arg = opts.partition(" ") |
| 272 |
|
| 273 |
exc = getattr(exception, name, None) |
| 274 |
if exc is None: |
| 275 |
import builtins |
| 276 |
exc = getattr(builtins, name, Exception) |
| 277 |
|
| 278 |
if arg: |
| 279 |
def _raise(args): |
| 280 |
raise exc(arg) |
| 281 |
else: |
| 282 |
def _raise(args): |
| 283 |
raise exc() |
| 284 |
|
| 285 |
return None, _raise |
| 286 |
|
| 287 |
|
| 288 |
def action_abort(opts): |
| 289 |
def _abort(_): |
| 290 |
raise exception.StopExtraction(opts or None) |
| 291 |
return None, _abort |
| 292 |
|
| 293 |
|
| 294 |
def action_terminate(opts): |
| 295 |
def _terminate(_): |
| 296 |
raise exception.TerminateExtraction(opts) |
| 297 |
return None, _terminate |
| 298 |
|
| 299 |
|
| 300 |
def action_restart(opts): |
| 301 |
def _restart(_): |
| 302 |
raise exception.RestartExtraction(opts) |
| 303 |
return None, _restart |
| 304 |
|
| 305 |
|
| 306 |
def action_exit(opts): |
| 307 |
try: |
| 308 |
opts = int(opts) |
| 309 |
except ValueError: |
| 310 |
pass |
| 311 |
|
| 312 |
def _exit(_): |
| 313 |
raise SystemExit(opts) |
| 314 |
return None, _exit |
| 315 |
|
| 316 |
|
| 317 |
ACTIONS = { |
| 318 |
"abort" : action_abort, |
| 319 |
"exec" : action_exec, |
| 320 |
"exit" : action_exit, |
| 321 |
"flag" : action_flag, |
| 322 |
"keyword" : action_keyword, |
| 323 |
"level" : action_level, |
| 324 |
"print" : action_print, |
| 325 |
"raise" : action_raise, |
| 326 |
"restart" : action_restart, |
| 327 |
"status" : action_status, |
| 328 |
"terminate": action_terminate, |
| 329 |
"wait" : action_wait, |
| 330 |
} |