Skip to content

Commit

Permalink
Added optional length argument to get_hexdigest utility.
Browse files Browse the repository at this point in the history
  • Loading branch information
jezdez committed Apr 7, 2011
1 parent 68a3576 commit e1d230e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
12 changes: 5 additions & 7 deletions compressor/base.py
Expand Up @@ -63,16 +63,14 @@ def cached_filters(self):

@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):
Expand Down
9 changes: 6 additions & 3 deletions compressor/cache.py
Expand Up @@ -8,8 +8,11 @@
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):
Expand Down Expand Up @@ -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)

0 comments on commit e1d230e

Please sign in to comment.