from database import Database from scrapper import get_files, get_uri from utils import get_url, get import platform from terminal_menu.Menu import Menu uri = "" preview_site = {} preview_database = "" def get_user_choice_by_menu(options: list, title = None, preview_command=None, preview_size: float = 0.3, show_search_hint: bool = False, skip_empty_entries = False): menu = Menu(options, title, preview_body_function=preview_command, preview_ratio=preview_size, skip_empty_option=skip_empty_entries ) return (menu.show()); def add_site(database: Database) -> dict: print("add a site:") site = {} site.update({"url": input("url without protocol (ip:port):")}) site.update({"user": input("user(leave blank):")}) site.update({"password": input("password(leave blank):")}) site.update({"id": database.get_sites_table_len()}) name = input(f"name[{str(database.get_sites_table_len())}]") if (name == ""): name = database.get_sites_table_len() site.update({"name": name}) return (site) def files_preview(filename: str) -> str: if (not filename.endswith("/")): return (None) files = get_files(preview_site, uri + filename) return ("\n".join(files)) def files(site, path): global uri global preview_site uri = path preview_site = site files = get_files(site, path) choose = get_user_choice_by_menu(files, preview_command=files_preview, preview_size=0.3, show_search_hint=True, title=f"Index of {path}") if (choose == None): return (None) return (files[choose]) def config_preview(site_name:str): id = int(site_name.split(": ")[0]) site = preview_database.get_site_by_id(id) if (site == None): return str = f""" url: {site.get('url')} user: {site.get('user')} password: {site.get('password')} """ return (str) def site_deleter(sites, database: Database): global preview_database preview_database = database choose = get_user_choice_by_menu(sites, show_search_hint=True, preview_title="delete", preview_command=config_preview) return (choose) def param_editor(lst: list): choose = get_user_choice_by_menu(lst) return (choose) def site_editor(lst: list, database: Database): global preview_database preview_database = database choose = get_user_choice_by_menu(lst + ["", "quit"], skip_empty_entries=True, show_search_hint=True, title="Edit") if (choose == len(lst) + 1): return (None) return (choose) def sites(sites: list): nb_site = len(sites) choose = get_user_choice_by_menu(get(sites, "name") + ["", "add", "edit", "delete"], skip_empty_entries=True, show_search_hint=True) return (choose)