Debian Codegraph @

gallery_dl/postprocessor/hash.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2024-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 """Compute file hash digests"""
10
11 from .common import PostProcessor
12 import hashlib
13
14
15 class HashPP(PostProcessor):
16
17 def __init__(self, job, options):
18 PostProcessor.__init__(self, job)
19
20 self.chunk_size = options.get("chunk-size", 32768)
21 self.filename = options.get("filename")
22
23 hashes = options.get("mode") or options.get("hashes")
24 if isinstance(hashes, dict):
25 self.hashes = list(hashes.items())
26 elif isinstance(hashes, str):
27 self.hashes = []
28 for h in hashes.split(","):
29 name, sep, key = h.partition(":")
30 self.hashes.append((key if sep else name, name))
31 elif hashes:
32 self.hashes = hashes
33 else:
34 self.hashes = (("md5", "md5"), ("sha1", "sha1"))
35
36 events = options.get("event")
37 if events is None:
38 events = ("file",)
39 elif isinstance(events, str):
40 events = events.split(",")
41 job.register_hooks({event: self.run for event in events}, options)
42
43 def run(self, pathfmt):
44 hashes = [
45 (key, hashlib.new(name))
46 for key, name in self.hashes
47 ]
48
49 size = self.chunk_size
50 with self._open(pathfmt) as fp:
51 while True:
52 data = fp.read(size)
53 if not data:
54 break
55 for _, h in hashes:
56 h.update(data)
57
58 for key, h in hashes:
59 pathfmt.kwdict[key] = h.hexdigest()
60
61 if self.filename:
62 pathfmt.build_path()
63
64 def _open(self, pathfmt):
65 try:
66 return open(pathfmt.temppath, "rb")
67 except OSError:
68 return open(pathfmt.realpath, "rb")
69
70
71 __postprocessor__ = HashPP