mirror of
https://github.com/YunoHost/package_linter.git
synced 2024-09-03 20:06:12 +02:00
Use a wrapper for file-backed cached functions
This commit is contained in:
parent
91b14e1dc7
commit
1f035c4456
1 changed files with 17 additions and 16 deletions
|
@ -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 spdx_licenses():
|
def cache_file(cachefile: str, ttl_s: int):
|
||||||
cachefile = ".spdx_licenses"
|
def cache_is_fresh():
|
||||||
if os.path.exists(cachefile) and time.time() - os.path.getmtime(cachefile) < 3600:
|
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 open(cachefile).read()
|
||||||
|
return wrapper
|
||||||
url = "https://spdx.org/licenses/"
|
return decorator
|
||||||
content = urlopen(url)["content"]
|
|
||||||
open(cachefile, "w").write(content)
|
|
||||||
return content
|
|
||||||
|
|
||||||
|
|
||||||
|
@cache_file(".spdx_licenses", 3600)
|
||||||
|
def spdx_licenses():
|
||||||
|
return urlopen("https://spdx.org/licenses/")["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 = {}
|
||||||
|
|
Loading…
Add table
Reference in a new issue