Debian Codegraph @

gallery_dl/postprocessor/common.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018-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 """Common classes and constants used by postprocessor modules."""
10
11 from .. import archive
12
13
14 class PostProcessor():
15 """Base class for postprocessors"""
16
17 def __init__(self, job):
18 self.name = self.__class__.__name__[:-2].lower()
19 self.log = job.get_logger("postprocessor." + self.name)
20
21 def __repr__(self):
22 return self.__class__.__name__
23
24 def _archive_init(self, job, options, prefix=None):
25 if archive_path := options.get("archive"):
26 extr = job.extractor
27
28 archive_table = options.get("archive-table")
29 archive_prefix = options.get("archive-prefix")
30 if archive_prefix is None:
31 archive_prefix = extr.category if archive_table is None else ""
32
33 archive_format = options.get("archive-format")
34 if archive_format is None:
35 if prefix is None:
36 prefix = "_" + self.name.upper() + "_"
37 archive_format = prefix + extr.archive_fmt
38
39 try:
40 self.archive = archive.connect(
41 archive_path,
42 archive_prefix,
43 archive_format,
44 archive_table,
45 "file",
46 options.get("archive-reuse"),
47 options.get("archive-pragma"),
48 job.pathfmt,
49 "_archive_" + self.name,
50 )
51 except Exception as exc:
52 self.log.warning(
53 "Failed to open %s archive at '%s' (%s: %s)",
54 self.name, archive_path, exc.__class__.__name__, exc)
55 else:
56 self.log.debug(
57 "Using %s archive '%s'", self.name, archive_path)
58 return True
59
60 self.archive = None
61 return False
62
63 def _archive_register(self, job):
64 job.register_hooks({"finalize": self._archive_close})
65
66 def _archive_close(self, _):
67 self.archive.close()