Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mikf
GitHub Repository: mikf/gallery-dl
Path: blob/master/gallery_dl/postprocessor/common.py
8752 views
1
# -*- coding: utf-8 -*-
2
3
# Copyright 2018-2025 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-pragma"),
47
job.pathfmt.kwdict,
48
"_archive_" + self.name,
49
)
50
except Exception as exc:
51
self.log.warning(
52
"Failed to open %s archive at '%s' (%s: %s)",
53
self.name, archive_path, exc.__class__.__name__, exc)
54
else:
55
self.log.debug(
56
"Using %s archive '%s'", self.name, archive_path)
57
return True
58
59
self.archive = None
60
return False
61
62
def _archive_register(self, job):
63
job.register_hooks({"finalize": self._archive_close})
64
65
def _archive_close(self, _):
66
self.archive.close()
67
68