diff --git a/prosopopee/cache.py b/prosopopee/cache.py new file mode 100644 index 0000000..20b3ea0 --- /dev/null +++ b/prosopopee/cache.py @@ -0,0 +1,46 @@ +import os +import json + +CACHE_VERSION = 1 + + +class Cache(object): + cache_file_path = os.path.join(os.getcwd(), ".prosopopee_cache") + + def __init__(self, json): + # fix: I need to keep a reference to json because for whatever reason + # modules are set to None during python shutdown thus totally breaking + # the __del__ call to save the cache + # This wonderfully stupid behavior has been fixed in 3.4 (which nobody uses) + self.json = json + if os.path.exists(os.path.join(os.getcwd(), ".prosopopee_cache")): + self.cache = json.load(open(self.cache_file_path, "r")) + else: + self.cache = {"version": CACHE_VERSION} + + if "version" not in self.cache or self.cache["version"] != CACHE_VERSION: + print "info: cache format as changed, prune cache" + self.cache = {"version": CACHE_VERSION} + + def thumbnail_needs_to_be_generated(self, source, target, image): + if not os.path.exists(target): + return True + + if target not in self.cache: + return True + + cached_thumbnail = self.cache[target] + + if cached_thumbnail["size"] != os.path.getsize(source) or cached_thumbnail["options"] != image.options: + return True + + return False + + def cache_thumbnail(self, source, target, image): + self.cache[target] = {"size": os.path.getsize(source), "options": image.options} + + def __del__(self): + self.json.dump(self.cache, open(self.cache_file_path, "w")) + + +CACHE = Cache(json=json) diff --git a/prosopopee/prosopopee.py b/prosopopee/prosopopee.py index 499799c..383a185 100644 --- a/prosopopee/prosopopee.py +++ b/prosopopee/prosopopee.py @@ -2,12 +2,13 @@ import os import sys -import json import yaml import shutil from jinja2 import Environment, FileSystemLoader +from .cache import CACHE + templates = Environment(loader=FileSystemLoader([os.path.realpath(os.path.join(os.getcwd(), "templates")), os.path.join(os.path.split(os.path.realpath(__file__))[0], "templates")])) index_template = templates.get_template("index.html") gallery_index_template = templates.get_template("gallery-index.html") @@ -18,51 +19,6 @@ gm_settings = { "auto-orient": True } -CACHE_VERSION = 1 - - -class Cache(object): - cache_file_path = os.path.join(os.getcwd(), ".prosopopee_cache") - - def __init__(self, json): - # fix: I need to keep a reference to json because for whatever reason - # modules are set to None during python shutdown thus totally breaking - # the __del__ call to save the cache - # This wonderfully stupid behavior has been fixed in 3.4 (which nobody uses) - self.json = json - if os.path.exists(os.path.join(os.getcwd(), ".prosopopee_cache")): - self.cache = json.load(open(self.cache_file_path, "r")) - else: - self.cache = {"version": CACHE_VERSION} - - if "version" not in self.cache or self.cache["version"] != CACHE_VERSION: - print "info: cache format as changed, prune cache" - self.cache = {"version": CACHE_VERSION} - - def thumbnail_needs_to_be_generated(self, source, target, image): - if not os.path.exists(target): - return True - - if target not in self.cache: - return True - - cached_thumbnail = self.cache[target] - - if cached_thumbnail["size"] != os.path.getsize(source) or cached_thumbnail["options"] != image.options: - return True - - return False - - def cache_thumbnail(self, source, target, image): - self.cache[target] = {"size": os.path.getsize(source), "options": image.options} - - def __del__(self): - self.json.dump(self.cache, open(self.cache_file_path, "w")) - - -CACHE = Cache(json=json) - - class Image(object): base_dir = "" target_dir = ""