Use a wrapper for file-backed cached functions

This commit is contained in:
Salamandar 2024-06-09 23:22:03 +02:00
parent 91b14e1dc7
commit 1f035c4456

View file

@ -310,26 +310,27 @@ def file_exists(file_path):
return os.path.isfile(file_path) and os.stat(file_path).st_size > 0 return os.path.isfile(file_path) and os.stat(file_path).st_size > 0
def cache_file(cachefile: str, ttl_s: int):
def cache_is_fresh():
return os.path.exists(cachefile) and time.time() - os.path.getmtime(cachefile) < ttl_s
def decorator(function):
def wrapper(*args, **kwargs):
if not cache_is_fresh():
with open(cachefile, "w+") as outfile:
outfile.write(function(*args, **kwargs))
return open(cachefile).read()
return wrapper
return decorator
@cache_file(".spdx_licenses", 3600)
def spdx_licenses(): def spdx_licenses():
cachefile = ".spdx_licenses" return urlopen("https://spdx.org/licenses/")["content"]
if os.path.exists(cachefile) and time.time() - os.path.getmtime(cachefile) < 3600:
return open(cachefile).read()
url = "https://spdx.org/licenses/"
content = urlopen(url)["content"]
open(cachefile, "w").write(content)
return content
@cache_file(".manifest.v2.schema.json", 3600)
def manifest_v2_schema(): def manifest_v2_schema():
cachefile = ".manifest.v2.schema.json" return urlopen("https://raw.githubusercontent.com/YunoHost/apps/master/schemas/manifest.v2.schema.json")["content"]
if os.path.exists(cachefile) and time.time() - os.path.getmtime(cachefile) < 3600:
return json.loads(open(cachefile).read())
url = "https://raw.githubusercontent.com/YunoHost/apps/master/schemas/manifest.v2.schema.json"
content = urlopen(url)["content"]
open(cachefile, "w").write(content)
return json.loads(content)
tests = {} tests = {}