fix: and clean

This commit is contained in:
starnakin 2023-05-17 11:08:40 +02:00
parent d39e9cfb62
commit 61f535fce4
3 changed files with 53 additions and 28 deletions

37
main.py
View File

@ -24,8 +24,8 @@ def files_navigator(site: dict):
else:
path = path + file
def add_site():
site = menu.add_site()
def add_site(database: Database):
site = menu.add_site(database)
database.add_site(site)
def config_preview(site_name:str):
@ -38,12 +38,11 @@ def config_preview(site_name:str):
"""
return (str)
def sites_deleter():
def sites_deleter(database: Database):
lst = []
for i in sites_table.all():
for i in database.get_sites():
lst.append(f"{str(i.get('id'))}: {i.get('name')}")
terminal_menu = TerminalMenu(lst, show_search_hint=True, preview_title="delete", preview_command=config_preview)
choose = terminal_menu.show()
choose = menu.site_deleter(lst, database)
if (choose == None):
return (1)
site_name = lst[choose]
@ -53,20 +52,18 @@ def sites_deleter():
database.set_last_path("")
database.set_last_site("")
def sites_editor(site_id: int):
def sites_editor(database: Database):
lst = []
for i in database.get_sites():
lst.append(f"{str(i.get('id'))}: {i.get('name')}")
terminal_menu = TerminalMenu(lst, show_search_hint=True, preview_title="delete", preview_command=config_preview)
choose = terminal_menu.show()
choose = menu.site_editor(lst, database)
if (choose == None):
return (1)
site_name = lst[choose]
id = int(site_name.split(": ")[0])
site = database.get_site_by_id(id)
lst = [f"name: {site.get('name')}", f"url: {site.get('url')}", f"user: {site.get('user')}", f"password: {site.get('password')}"]
terminal_menu = TerminalMenu(lst)
choose = terminal_menu.show()
choose = menu.param_editor(lst)
if (choose == None):
return (1);
for i in lst:
@ -80,7 +77,7 @@ def sites_editor(site_id: int):
elif (choose == 3):
sites_table.update(set("password", input("password: ")), query.id == id)
def sites_navigator():
def sites_navigator(database: Database):
nb_site = database.get_sites_table_len()
if (nb_site == 0):
add_site()
@ -90,24 +87,22 @@ def sites_navigator():
files_navigator(last_site)
while True:
nb_site = database.get_sites_table_len()
terminal_menu = TerminalMenu(get(database.get_sites(), "name") + ["", "add", "edit", "delete"], skip_empty_entries=True, show_search_hint=True)
choose = terminal_menu.show()
choose = menu.sites(database.get_sites())
if (choose == None):
return (1)
if (choose == nb_site + 1):
add_site();
add_site(database);
elif (choose == nb_site + 2):
sites_editor()
sites_editor(database)
elif (choose == nb_site + 3):
sites_deleter()
sites_navigator()
sites_deleter(database)
sites_navigator(database)
return
else:
set_url(database.get_sites()[choose])
files_navigator()
files_navigator(database.get_sites()[choose])
database = Database()
if (database.get_cookies_table_len() == 0):
database.add_cookies({"last_path": "", "last_site": "", "id": "0"})
sites_navigator();
sites_navigator(database);

38
menu.py
View File

@ -1,10 +1,11 @@
from simple_term_menu import TerminalMenu
from database import Database
from scrapper import get_files, get_uri
from utils import get_url
from utils import get_url, get
uri = ""
preview_site = {}
preview_database = ""
def add_site(database: Database) -> dict:
print("add a site:")
@ -12,10 +13,10 @@ def add_site(database: Database) -> dict:
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": str(database.get_sites_table_len())})
name = input(f"name[{str(len(sites_table.all()))}] :")
site.update({"id": database.get_sites_table_len()})
name = input(f"name[{str(database.get_sites_table_len())}]")
if (name == ""):
name = str(database.get_sites_table_len())
name = database.get_sites_table_len()
site.update({"name": name})
return (site)
@ -38,11 +39,15 @@ def files(site, path):
show_search_hint=True,
title=f"Index of {path}")
choose = terminal_menu.show()
if (choose == None):
return (None)
return (files[choose])
def config_preview(site_name:str):
id = int(site_name.split(": ")[0])
site = database.get_site_by_id(id)
site = preview_database.get_site_by_id(id)
if (site == None):
return
str = f"""
url: {site.get('url')}
user: {site.get('user')}
@ -50,7 +55,10 @@ def config_preview(site_name:str):
"""
return (str)
def site_deleter(sites: list):
def site_deleter(sites, database: Database):
global preview_database
preview_database = database
terminal_menu = TerminalMenu(sites,
show_search_hint=True,
preview_title="delete",
@ -58,7 +66,15 @@ def site_deleter(sites: list):
choose = terminal_menu.show()
return (choose)
def site_editor(lst: list):
def param_editor(lst: list):
terminal_menu = TerminalMenu(lst)
choose = terminal_menu.show()
return (choose)
def site_editor(lst: list, database: Database):
global preview_database
preview_database = database
terminal_menu = TerminalMenu(lst + ["", "quit"],
skip_empty_entries=True,
show_search_hint=True,
@ -68,3 +84,11 @@ def site_editor(lst: list):
if (choose == len(lst) + 1):
return (None)
return (choose)
def sites(sites: list):
nb_site = len(sites)
terminal_menu = TerminalMenu(get(sites, "name") + ["", "add", "edit", "delete"],
skip_empty_entries=True,
show_search_hint=True)
choose = terminal_menu.show()
return (choose)

View File

@ -4,3 +4,9 @@ def get_url(site: dict):
return (f"http://{site.get('url')}")
else:
return (f"http://{site.get('user')}:{site.get('password')}@{site.get('url')}")
def get(dictionnarys: list, key: str):
lst = []
for dictionnary in dictionnarys:
lst.append(dictionnary.get(key))
return (lst)