feat: support local-fs via dynamic config menu

This commit is contained in:
Yannik Schmidt
2024-04-14 21:07:54 +02:00
parent eef9c35c10
commit 3bf2ea9d5a
4 changed files with 43 additions and 13 deletions

3
.gitignore vendored
View File

@@ -3,4 +3,5 @@
__pycache__/ __pycache__/
cache/ cache/
install/ install/
*.json *.json
install-dir/

View File

@@ -30,8 +30,9 @@ def close_input_window(input_window):
'''Close the config window and save the settings''' '''Close the config window and save the settings'''
# retrieve values # # retrieve values #
entries = list(filter(lambda x: isinstance(x, customtkinter.CTkEntry), input_window.winfo_children())) entries = list(filter(lambda x: isinstance(x, (customtkinter.CTkEntry, customtkinter.CTkOptionMenu)), input_window.winfo_children()))
labels = [ input_window.grid_slaves(row=e.grid_info()["row"], column=e.grid_info()["column"]-1)[0] for e in entries ] labels = [ input_window.grid_slaves(row=e.grid_info()["row"], column=e.grid_info()["column"]-1)[0] for e in entries ]
print(labels)
ret_dict = dict() ret_dict = dict()
for e, l in zip(entries, labels): for e, l in zip(entries, labels):
ret_dict.update({ l.cget("text") : e.get() }) ret_dict.update({ l.cget("text") : e.get() })
@@ -276,17 +277,30 @@ if __name__ == "__main__":
user = config_loaded.get("User:") user = config_loaded.get("User:")
password = config_loaded.get("Password:") password = config_loaded.get("Password:")
install_dir = config_loaded["Install dir:"] install_dir = config_loaded["Install dir:"]
backend_type = config_loaded["Select option:"]
# get the secod part of the server string, then split once at first / to get path and prepend / again# # get the secod part of the server string, then split once at first / to get path and prepend / again#
remote_root_dir = "/" + config_loaded["Server/Path:"].split("://")[1].split("/", 1)[1] if backend_type == "FTP/FTPS":
server = config_loaded["Server/Path:"][:-len(remote_root_dir)] remote_root_dir = "/" + config_loaded["Server/Path:"].split("://")[1].split("/", 1)[1]
server = config_loaded["Server/Path:"][:-len(remote_root_dir)]
elif backend_type == "Local Filesystem":
remote_root_dir = config_loaded["Server/Path:"]
server = None
else:
raise NotImplementedError("Unsupported Backend")
# debug output # # debug output #
print(user, password, install_dir, remote_root_dir, server, config_loaded["Server/Path:"]) print(user, password, install_dir, remote_root_dir, server, config_loaded["Server/Path:"])
# add db backend # # add db backend #
db = data_backend.FTP(user, password, install_dir, server=server, if backend_type == "FTP/FTPS":
db = data_backend.FTP(user, password, install_dir, server=server,
remote_root_dir=remote_root_dir, progress_bar_wrapper=pgw, tkinter_root=app) remote_root_dir=remote_root_dir, progress_bar_wrapper=pgw, tkinter_root=app)
elif backend_type == "Local Filesystem":
db = data_backend.LocalFS(user, password, install_dir, server=server,
remote_root_dir=remote_root_dir, progress_bar_wrapper=pgw, tkinter_root=app)
else:
raise NotImplementedError("Unsupported Backend")
# geometry is set at the very beginning # # geometry is set at the very beginning #
app.update() app.update()

View File

@@ -20,6 +20,7 @@ class DataBackend:
self.install_dir = install_dir self.install_dir = install_dir
self.progress_bar_wrapper = progress_bar_wrapper self.progress_bar_wrapper = progress_bar_wrapper
self.root = tkinter_root self.root = tkinter_root
self.cache_dir = "./cache/"
def get(self, path, return_content=False): def get(self, path, return_content=False):
'''Return the contents of this path''' '''Return the contents of this path'''
@@ -50,8 +51,8 @@ class LocalFS(DataBackend):
# load the file on remote # # load the file on remote #
with open(fullpath, "rb") as f: with open(fullpath, "rb") as f:
print(cache_dir, path)
target = os.path.join(cache_dir, os.path.basename(path)) target = os.path.join(cache_dir, os.path.basename(path))
print("Cache-Dir-Base", cache_dir, "Cache-Dir-Target", target, "Path:", path)
with open(target, "wb") as ft: with open(target, "wb") as ft:
if return_content: if return_content:
return f.read() return f.read()
@@ -82,9 +83,9 @@ class LocalFS(DataBackend):
if not os.path.isfile(meta_file): if not os.path.isfile(meta_file):
continue continue
else: else:
meta_info_list.append(software.Software(meta_file, self)) meta_info_list.append(software.Software(meta_file, self, self.progress_bar_wrapper))
return meta_info_list return list(filter(lambda x: not x.invalid, meta_info_list))
class FTP(DataBackend): class FTP(DataBackend):
@@ -122,6 +123,9 @@ class FTP(DataBackend):
else: else:
ftp.login() ftp.login()
# cache dir is automatically set #
self.cache_dir = None
return ftp return ftp
@@ -227,5 +231,5 @@ class FTP(DataBackend):
#print(meta_file_content) #print(meta_file_content)
local_meta_file_list.append(f) local_meta_file_list.append(f)
return [ software.Software(meta_file, self, self.progress_bar_wrapper) return list(filter(lambda x: not x.invalid, [ software.Software(meta_file, self, self.progress_bar_wrapper)
for meta_file in local_meta_file_list ] for meta_file in local_meta_file_list ]))

View File

@@ -14,9 +14,17 @@ class Software:
self.meta_file = meta_file self.meta_file = meta_file
self.directory = os.path.dirname(meta_file) self.directory = os.path.dirname(meta_file)
self.backend = backend self.backend = backend
# print(self.directory) print("Software Directory:", self.directory)
self.cache_dir = os.path.join("cache", self.directory)
self._load_from_yaml() self.cache_dir = backend.cache_dir or os.path.join("cache", self.directory)
# return None instead of the object if yaml failed #
try:
self.invalid = False
self._load_from_yaml()
except ValueError as e:
self.invalid = True
self.progress_bar_wrapper = progress_bar_wrapper self.progress_bar_wrapper = progress_bar_wrapper
def _load_from_yaml(self): def _load_from_yaml(self):
@@ -24,6 +32,9 @@ class Software:
content = self.backend.get(self.meta_file, self.cache_dir, return_content=True) content = self.backend.get(self.meta_file, self.cache_dir, return_content=True)
meta = yaml.safe_load(content) meta = yaml.safe_load(content)
if not meta:
raise ValueError("Empty Meta File")
self.title = meta.get("title") self.title = meta.get("title")
self.genre = meta.get("genre") self.genre = meta.get("genre")
self.description = meta.get("description") self.description = meta.get("description")