mirror of
https://github.com/FAUSheppy/homelab_gamevault
synced 2025-12-06 06:51:36 +01:00
wip: initial
This commit is contained in:
53
data_backend.py
Normal file
53
data_backend.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import os
|
||||
import glob
|
||||
import yaml
|
||||
|
||||
class DataBackend:
|
||||
|
||||
def __init__(self, user, password, cache_dir, remote_root_dir=None):
|
||||
|
||||
self.user = user
|
||||
self.password = password
|
||||
self.cache_dir = cache_dir
|
||||
self.remote_root_dir = remote_root_dir
|
||||
|
||||
if not os.path.isdir(self.cache_dir):
|
||||
os.mkdir(self.cache_dir)
|
||||
|
||||
def get(self, path):
|
||||
'''Return the contents of this path'''
|
||||
raise NotImplementedError()
|
||||
|
||||
def list(self, path):
|
||||
'''List the contents of this path'''
|
||||
raise NotImplementedError()
|
||||
|
||||
def find_all_metadata(self):
|
||||
'''Return key-value map of { software : metadata-dict }'''
|
||||
raise NotImplementedError
|
||||
|
||||
class LocalFS(DataBackend):
|
||||
|
||||
def get(self, path):
|
||||
fullpath = os.path.join(self.remote_root_dir, path)
|
||||
with open(fullpath, "rb") as f:
|
||||
target = os.path.join(self.cache_dir, os.path.basename(path))
|
||||
with open(target, "wb") as ft:
|
||||
ft.write(f.read())
|
||||
|
||||
def list(self, path):
|
||||
fullpath = os.path.join(self.remote_root_dir, path)
|
||||
return os.listdir(fullpath)
|
||||
|
||||
def find_all_metadata(self):
|
||||
|
||||
meta_info_list = []
|
||||
for software_dir in glob.iglob(self.remote_root_dir + "/*"):
|
||||
meta_file = os.path.join(software_dir, "meta.yaml")
|
||||
if not os.path.isfile(meta_file):
|
||||
continue
|
||||
else:
|
||||
with open(meta_file) as f:
|
||||
meta_info_list.append(yaml.safe_load(f))
|
||||
|
||||
return meta_info_list
|
||||
Reference in New Issue
Block a user