summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDylan Araps <dylan.araps@gmail.com>2018-10-18 06:39:20 +1100
committerGitHub <noreply@github.com>2018-10-18 06:39:20 +1100
commitf38d630a63d9d33f3d43443d0e0f7c9a3702bf24 (patch)
treecd84210556e405003c34c7461466e0750a40ad20
parent79a4069af56b26366b9ada4abf5d80e45ca78cba (diff)
parent51af3c1e03875716d224e40878f821dac931fc9a (diff)
Merge pull request #308 from dgrisham/feat/hash-file-for-cache-fname
Hash file for cache filename
-rw-r--r--pywal/colors.py5
-rw-r--r--pywal/util.py12
2 files changed, 14 insertions, 3 deletions
diff --git a/pywal/colors.py b/pywal/colors.py
index a87ee4c..246318c 100644
--- a/pywal/colors.py
+++ b/pywal/colors.py
@@ -4,7 +4,6 @@ Generate a palette using various backends.
import logging
import os
import random
-import re
import sys
from . import theme
@@ -86,9 +85,9 @@ def saturate_colors(colors, amount):
def cache_fname(img, backend, light, cache_dir, sat=""):
"""Create the cache file name."""
color_type = "light" if light else "dark"
- file_name = re.sub("[/|\\|.]", "_", img)
+ file_hash = util.hashf(img)
- file_parts = [file_name, color_type, backend, sat, __cache_version__]
+ file_parts = [file_hash, color_type, backend, sat, __cache_version__]
return [cache_dir, "schemes", "%s_%s_%s_%s_%s.json" % (*file_parts,)]
diff --git a/pywal/util.py b/pywal/util.py
index 0557cc4..10fb42c 100644
--- a/pywal/util.py
+++ b/pywal/util.py
@@ -2,6 +2,7 @@
Misc helper functions.
"""
import colorsys
+import hashlib
import json
import logging
import os
@@ -178,3 +179,14 @@ def get_pid(name):
return False
return True
+
+
+def hashf(fpath):
+ """Get the md5 hash of a file."""
+ return hashlib.md5(file_bytes(open(fpath, 'rb'))).hexdigest()
+
+
+def file_bytes(fpath):
+ """Helper function to read file."""
+ with fpath:
+ return fpath.read()