2016-02-18 06:43:28 +01:00
|
|
|
import os
|
|
|
|
import json
|
|
|
|
|
2016-02-23 05:57:25 +01:00
|
|
|
CACHE_VERSION = 2
|
2016-02-18 06:43:28 +01:00
|
|
|
|
2016-02-28 09:47:33 +01:00
|
|
|
|
2016-02-28 09:49:02 +01:00
|
|
|
def remove_superficial_options(options):
|
2016-02-28 09:49:22 +01:00
|
|
|
cleaned_options = options.copy()
|
|
|
|
del cleaned_options["name"]
|
|
|
|
if "text" in cleaned_options:
|
|
|
|
del cleaned_options["text"]
|
2016-05-11 01:07:24 +02:00
|
|
|
if "type" in cleaned_options:
|
|
|
|
del cleaned_options["type"]
|
2017-05-23 21:44:13 +02:00
|
|
|
if "size" in cleaned_options:
|
|
|
|
del cleaned_options["size"]
|
|
|
|
if "float" in cleaned_options:
|
|
|
|
del cleaned_options["float"]
|
2016-02-28 09:49:22 +01:00
|
|
|
return cleaned_options
|
2016-02-18 06:43:28 +01:00
|
|
|
|
2016-02-28 09:47:33 +01:00
|
|
|
|
2016-02-18 06:43:28 +01:00
|
|
|
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:
|
2017-07-03 13:37:38 +02:00
|
|
|
print("info: cache format as changed, prune cache")
|
2016-02-18 06:43:28 +01:00
|
|
|
self.cache = {"version": CACHE_VERSION}
|
|
|
|
|
2016-02-18 14:27:13 +01:00
|
|
|
def needs_to_be_generated(self, source, target, options):
|
2016-02-18 08:29:08 +01:00
|
|
|
if not os.path.exists(target):
|
|
|
|
return True
|
|
|
|
|
|
|
|
if target not in self.cache:
|
|
|
|
return True
|
|
|
|
|
2016-02-18 14:27:13 +01:00
|
|
|
cached_picture = self.cache[target]
|
2016-02-18 08:29:08 +01:00
|
|
|
|
2016-02-28 09:49:02 +01:00
|
|
|
if cached_picture["size"] != os.path.getsize(source) or cached_picture["options"] != remove_superficial_options(options):
|
2016-02-18 08:29:08 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2016-02-18 14:27:13 +01:00
|
|
|
def cache_picture(self, source, target, options):
|
2016-02-28 09:49:02 +01:00
|
|
|
self.cache[target] = {"size": os.path.getsize(source), "options": remove_superficial_options(options)}
|
2016-02-18 08:29:08 +01:00
|
|
|
|
2017-07-12 13:39:45 +02:00
|
|
|
def cache_dump(self):
|
|
|
|
self.json.dump(self.cache, open(self.cache_file_path, "w"))
|
2016-02-18 06:43:28 +01:00
|
|
|
|
|
|
|
CACHE = Cache(json=json)
|