move hashf() to util.py

This commit is contained in:
dgrisham 2018-09-27 10:50:13 -06:00
parent 499b1c4ff5
commit b08648c720
2 changed files with 11 additions and 9 deletions

View File

@ -6,7 +6,6 @@ import os
import random import random
import re import re
import sys import sys
import hashlib
from . import theme from . import theme
from . import util from . import util
@ -87,19 +86,12 @@ def saturate_colors(colors, amount):
def cache_fname(img, backend, light, cache_dir, sat=""): def cache_fname(img, backend, light, cache_dir, sat=""):
"""Create the cache file name.""" """Create the cache file name."""
color_type = "light" if light else "dark" color_type = "light" if light else "dark"
file_hash = hashf(img) file_hash = util.hashf(img)
file_parts = [file_hash, 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,)] return [cache_dir, "schemes", "%s_%s_%s_%s_%s.json" % (*file_parts,)]
def hashf(fpath):
return hashlib.md5(file_bytes(open(fpath, 'rb'))).hexdigest()
def file_bytes(fpath):
with fpath:
return fpath.read()
def get_backend(backend): def get_backend(backend):
"""Figure out which backend to use.""" """Figure out which backend to use."""
if backend == "random": if backend == "random":

View File

@ -2,6 +2,7 @@
Misc helper functions. Misc helper functions.
""" """
import colorsys import colorsys
import hashlib
import json import json
import logging import logging
import os import os
@ -178,3 +179,12 @@ def get_pid(name):
return False return False
return True return True
def hashf(fpath):
return hashlib.md5(file_bytes(open(fpath, 'rb'))).hexdigest()
def file_bytes(fpath):
with fpath:
return fpath.read()