[enh] implement caching for thumbnails

This commit is contained in:
Laurent Peuch 2015-12-11 09:22:36 +01:00
parent 719b2c5128
commit 674958551a

View File

@ -2,6 +2,7 @@
import os import os
import sys import sys
import json
import yaml import yaml
import shutil import shutil
@ -12,6 +13,36 @@ index_template = templates.get_template("index.html")
gallery_index_template = templates.get_template("gallery-index.html") gallery_index_template = templates.get_template("gallery-index.html")
class Cache(object):
cache_file_path = os.path.join(os.getcwd(), ".prosopopee_cache")
def __init__(self):
if os.path.exists(os.path.join(os.getcwd(), ".prosopopee_cache")):
self.cache = json.load(open(self.cache_file_path, "r"))
else:
self.cache = {}
def thumbnail_needs_to_be_generated(self, source, target):
if not os.path.exists(target):
return True
if target not in self.cache:
return True
if self.cache[target] != os.path.getsize(source):
return True
return False
def cache_thumbnail(self, source, target):
self.cache[target] = os.path.getsize(source)
def __del__(self):
json.dump(self.cache, open(self.cache_file_path, "w"))
CACHE = Cache()
class TemplateFunctions(): class TemplateFunctions():
def __init__(self, base_dir, target_dir, has_gm): def __init__(self, base_dir, target_dir, has_gm):
self.base_dir = base_dir self.base_dir = base_dir
@ -34,9 +65,15 @@ class TemplateFunctions():
thumbnail_name[-2] += "-small" thumbnail_name[-2] += "-small"
thumbnail_name = ".".join(thumbnail_name) thumbnail_name = ".".join(thumbnail_name)
command = "gm convert %s -resize %s %s" % (os.path.join(self.base_dir, image), gm_geometry, os.path.join(self.target_dir, thumbnail_name)) source, target = os.path.join(self.base_dir, image), os.path.join(self.target_dir, thumbnail_name)
print command
if CACHE.thumbnail_needs_to_be_generated(source, target):
command = "gm convert %s -resize %s %s" % (source, gm_geometry, target)
# print command
os.system(command) os.system(command)
CACHE.cache_thumbnail(source, target)
return thumbnail_name return thumbnail_name