Cache to automagically download .hrb files

This commit is contained in:
Emilia Allison 2024-01-11 09:24:48 -05:00
parent 910b678ffe
commit dfb285d239
Signed by: emilia
GPG Key ID: 05D5D1107E5100A1
1 changed files with 39 additions and 0 deletions

39
cache.py Normal file
View File

@ -0,0 +1,39 @@
from os import listdir, mkdir, path
import re
import fetch
NAME_REGEX = r'(.*\..*)(_)'
class Cache:
def __init__(self, dir: str = "AutoDatabaseGenerator/Resources"):
self.dir = dir
self._cache = dict()
self._scan_cache()
def get(self, name: str):
if name in self._cache:
return path.abspath(self.dir + self._cache[name])
try:
driver = fetch.get_driver_by_exact_name(name)
file = fetch.download_driver(driver, self.dir)
except Exception as e:
raise Exception("Could not fetch driver", e)
self._cache[name] = file
return path.abspath(self.dir + self._cache[name])
def _scan_cache(self):
try:
files = listdir(self.dir)
print("files found: ", files)
except FileNotFoundError:
mkdir(self.dir)
return
filtered = [file for file in files if file.endswith('.hrb')]
for file in filtered:
name = re.match(NAME_REGEX, file).group(1)
self._cache[name] = file