From e1d230e0e306185422783e21b19ed28afcd8c1bb Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Thu, 7 Apr 2011 22:16:50 +0200 Subject: [PATCH] Added optional length argument to get_hexdigest utility. --- compressor/base.py | 12 +++++------- compressor/cache.py | 9 ++++++--- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/compressor/base.py b/compressor/base.py index d0213fe..d3f9e9d 100644 --- a/compressor/base.py +++ b/compressor/base.py @@ -63,16 +63,14 @@ class Compressor(object): @cached_property def mtimes(self): - for kind, value, elem in self.split_contents(): - if kind == 'file': - yield str(get_mtime(value)) + return [str(get_mtime(value)) + for kind, value, _ in self.split_contents() if kind == 'file'] @cached_property def cachekey(self): - cachestr = "".join( - chain([self.content], self.mtimes)).encode(self.charset) - return "django_compressor.%s.%s" % (socket.gethostname(), - get_hexdigest(cachestr)[:12]) + key = get_hexdigest(''.join( + [self.content] + self.mtimes).encode(self.charset), 12) + return "django_compressor.%s.%s" % (socket.gethostname(), key) @cached_property def hunks(self): diff --git a/compressor/cache.py b/compressor/cache.py index 4b9ebb4..f832b4d 100644 --- a/compressor/cache.py +++ b/compressor/cache.py @@ -8,8 +8,11 @@ from django.utils.hashcompat import sha_constructor from compressor.conf import settings -def get_hexdigest(plaintext): - return sha_constructor(plaintext).hexdigest() +def get_hexdigest(plaintext, length=None): + digest = sha_constructor(plaintext).hexdigest() + if length: + return digest[:length] + return digest def get_mtime_cachekey(filename): @@ -37,7 +40,7 @@ def get_mtime(filename): def get_hashed_mtime(filename, length=12): filename = os.path.realpath(filename) mtime = str(int(get_mtime(filename))) - return get_hexdigest(mtime)[:length] + return get_hexdigest(mtime, length) cache = get_cache(settings.COMPRESS_CACHE_BACKEND)