the method now return the config dict if exists, oterwise open gui
parent
f60b410124
commit
d593a12d8f
110
config.py
110
config.py
|
@ -1,30 +1,105 @@
|
||||||
import os
|
import os
|
||||||
from tkinter import Button, Entry, Label, StringVar, PhotoImage, Tk, Canvas
|
import pathlib
|
||||||
|
import pickle
|
||||||
|
from hashlib import sha256
|
||||||
|
from tkinter import Button, Entry, Label, StringVar, Tk, filedialog
|
||||||
|
|
||||||
|
from PIL import Image, ImageTk
|
||||||
|
|
||||||
|
from misc import alert, get_os
|
||||||
|
|
||||||
|
|
||||||
def main(args):
|
def __get_config_folder():
|
||||||
|
_os = get_os()
|
||||||
|
if _os == "linux":
|
||||||
|
folder = os.path.join(os.getenv("HOME"), ".munyal")
|
||||||
|
else:
|
||||||
|
folder = os.path.join(os.path.expandvars("%APPDATA%"), "Munyal")
|
||||||
|
return folder
|
||||||
|
|
||||||
|
|
||||||
|
def get_config():
|
||||||
|
while True:
|
||||||
|
config = __get_config()
|
||||||
|
if not config:
|
||||||
|
gui_config()
|
||||||
|
else:
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
def __get_config():
|
||||||
|
folder = __get_config_folder()
|
||||||
|
config_file = os.path.join(folder, "config")
|
||||||
|
if os.path.exists(config_file):
|
||||||
|
config_bytes = open(config_file, "rb")
|
||||||
|
config = pickle.load(config_bytes)
|
||||||
|
config_bytes.close()
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
def __validate_values(user, password):
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def _set_config(root, user, password, folder):
|
||||||
|
passwd_hash = sha256(password.get().encode("utf-8")).hexdigest()
|
||||||
|
if not user:
|
||||||
|
alert(root, "Introduce tu usuario de la red Munyal")
|
||||||
|
elif not password:
|
||||||
|
alert(root, "Introduce tu contraseña")
|
||||||
|
elif not folder:
|
||||||
|
alert(root, "Elige la carpeta que quieres sincronizar")
|
||||||
|
elif not __validate_values(user, passwd_hash):
|
||||||
|
alert(
|
||||||
|
root,
|
||||||
|
"Tu usuario o contraseña son incorrectos, favor de reintentarlo")
|
||||||
|
else:
|
||||||
|
password.set("")
|
||||||
|
folder_config = __get_config_folder()
|
||||||
|
path = pathlib.Path(folder_config)
|
||||||
|
path.mkdir(parents=True, exist_ok=True)
|
||||||
|
with open(os.path.join(folder_config, "config"), "wb") as config_file:
|
||||||
|
config_file.write(
|
||||||
|
pickle.dumps({
|
||||||
|
"login": {
|
||||||
|
"user": user,
|
||||||
|
"password": passwd_hash
|
||||||
|
},
|
||||||
|
"folder": folder
|
||||||
|
}))
|
||||||
|
root.destroy()
|
||||||
|
|
||||||
|
|
||||||
|
def gui_config():
|
||||||
root = Tk()
|
root = Tk()
|
||||||
root.title("Munyal")
|
root.geometry("250x420")
|
||||||
root.geometry("320x500")
|
|
||||||
|
|
||||||
logo = PhotoImage(file="img/logo.png")
|
img_logo = Image.open("img/logo.png")
|
||||||
|
img_tk = ImageTk.PhotoImage(img_logo.resize((200, 150), Image.ANTIALIAS))
|
||||||
|
|
||||||
host = StringVar()
|
host = StringVar()
|
||||||
host_field = Entry(root, textvariable=host, width=30)
|
host.set("localhost")
|
||||||
|
host_field = Entry(root, textvariable=host)
|
||||||
|
|
||||||
passwd = StringVar()
|
passwd = StringVar()
|
||||||
passwd_field = Entry(root, textvariable=passwd, show="*", width=30)
|
passwd_field = Entry(root, textvariable=passwd, show="*")
|
||||||
|
|
||||||
folder = StringVar()
|
folder = StringVar()
|
||||||
folder.set(os.path.join(os.getenv("HOME"), "Munyal"))
|
folder.set(os.path.join(os.getenv("HOME"), "Munyal"))
|
||||||
folder_field = Entry(root, textvariable=folder, width=30)
|
folder_field = Entry(root, textvariable=folder)
|
||||||
|
btn_folder = Button(root,
|
||||||
|
text="Examinar",
|
||||||
|
command=lambda: search_folder(folder))
|
||||||
|
|
||||||
connect = Button(root, text="Conectar", command=lambda: None, width=10)
|
connect = Button(
|
||||||
|
root,
|
||||||
|
text="Conectar",
|
||||||
|
command=lambda: _set_config(root, host.get(), passwd, folder.get()))
|
||||||
|
|
||||||
Label(root, image=logo).pack()
|
Label(root, text="MUNYAL").pack()
|
||||||
Label(root, text="MUNYAL", font=("", 30)).pack()
|
Label(root, image=img_tk).pack()
|
||||||
Label(root, text="").pack()
|
Label(root, text="").pack()
|
||||||
Label(root, text="Nombre del servidor").pack()
|
Label(root, text="Ruta del servidor").pack()
|
||||||
host_field.pack()
|
host_field.pack()
|
||||||
Label(root, text="").pack()
|
Label(root, text="").pack()
|
||||||
Label(root, text="Contraseña").pack()
|
Label(root, text="Contraseña").pack()
|
||||||
|
@ -32,12 +107,17 @@ def main(args):
|
||||||
Label(root, text="").pack()
|
Label(root, text="").pack()
|
||||||
Label(root, text="Carpeta a sincronizar").pack()
|
Label(root, text="Carpeta a sincronizar").pack()
|
||||||
folder_field.pack()
|
folder_field.pack()
|
||||||
|
btn_folder.pack()
|
||||||
Label(root, text="").pack()
|
Label(root, text="").pack()
|
||||||
connect.pack()
|
connect.pack()
|
||||||
|
|
||||||
root.mainloop()
|
root.mainloop()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
def search_folder(field):
|
||||||
import sys
|
path = filedialog.askdirectory()
|
||||||
sys.exit(main(sys.argv))
|
field.set(path)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(get_config())
|
||||||
|
|
Loading…
Reference in New Issue