Pi66

merodi
A markdown-based static site generator.
git clone https://git.pi66.xyz/merodi

← back to log

hash: global cache, clear functions

author: pi66
date: 2026-07-21 10:14
hash: 13c7c98a7f8bf09727642b3150a17382bacbb19f

Diffstat:

M

src/hash.py
21 +++++++++++++++++++++---------
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
diff --git a/src/hash.py b/src/hash.py
index 68c5f91..40465b3 100644
--- a/src/hash.py
+++ b/src/hash.py
@@ -12,16 +12,25 @@ HASH_FILE_CONTENT: dict | None = None
 def read_hash_file(config):
     global HASH_FILE_CONTENT
     if HASH_FILE_CONTENT is None:
-        with open(config.cache.hash, "r") as f:
-            HASH_FILE_CONTENT = json.load(f)
-
-    return
+        try:
+            with open(config.cache.hash, "r") as f:
+                HASH_FILE_CONTENT = json.load(f)
+        except (FileNotFoundError, json.JSONDecodeError):
+            HASH_FILE_CONTENT = {}


 def write_hash_file(config):
     global HASH_FILE_CONTENT
-    with open(config.cache.hash, "w") as f:
-        json.dump(HASH_FILE_CONTENT, f)
+    write_file(config.cache.hash, json.dumps(HASH_FILE_CONTENT, indent=2))
+
+def clear_all_hashes():
+    global HASH_FILE_CONTENT
+    HASH_FILE_CONTENT = {}
+
+def clear_file_hash(md_path):
+    global HASH_FILE_CONTENT
+    if HASH_FILE_CONTENT is not None and md_path in HASH_FILE_CONTENT:
+        del HASH_FILE_CONTENT[md_path]

 def md5_handler(md_path):
     md_content = read_file(md_path)