From bbce8e638bc4d0cf00441f15bef131f7cbf1225c Mon Sep 17 00:00:00 2001 From: Madiba Hudson-Quansah Date: Fri, 21 Jul 2023 00:12:56 +0000 Subject: [PATCH] Added image checksum to colorschemes --- pywal/colors.py | 3 ++- pywal/theme.py | 3 +++ pywal/util.py | 8 ++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pywal/colors.py b/pywal/colors.py index fd8033f..3d7b42b 100644 --- a/pywal/colors.py +++ b/pywal/colors.py @@ -30,6 +30,7 @@ def normalize_img_path(img: str): def colors_to_dict(colors, img): """Convert list of colors to pywal format.""" return { + "checksum": util.get_img_checksum(img), "wallpaper": normalize_img_path(img), "alpha": util.Color.alpha_num, @@ -132,7 +133,7 @@ def get(img, light=False, backend="wal", cache_dir=CACHE_DIR, sat=""): cache_name = cache_fname(img, backend, light, cache_dir, sat) cache_file = os.path.join(*cache_name) - if os.path.isfile(cache_file): + if os.path.isfile(cache_file) and theme.parse(cache_file)["checksum"] == util.get_img_checksum(img): colors = theme.file(cache_file) colors["alpha"] = util.Color.alpha_num logging.info("Found cached colorscheme.") diff --git a/pywal/theme.py b/pywal/theme.py index 9dc4f13..a84bf71 100644 --- a/pywal/theme.py +++ b/pywal/theme.py @@ -78,6 +78,9 @@ def parse(theme_file): """Parse the theme file.""" data = util.read_file_json(theme_file) + if "checksum" not in data: + data["checksum"] = "None" + if "wallpaper" not in data: data["wallpaper"] = "None" diff --git a/pywal/util.py b/pywal/util.py index 1fdae31..0557b26 100644 --- a/pywal/util.py +++ b/pywal/util.py @@ -10,6 +10,7 @@ import re import shutil import subprocess import sys +from hashlib import md5 class Color: @@ -141,6 +142,13 @@ def save_file_json(data, export_file): with open(export_file, "w") as file: json.dump(data, file, indent=4) +def get_img_checksum(img): + checksum = md5() + with open(img, "rb") as f: + for chunk in iter(lambda: f.read(4096), b""): + checksum.update(chunk) + return checksum.hexdigest() + def create_dir(directory): """Alias to create the cache dir."""