added a way to config Munyal without gui/tkinter

master
kirbylife 2020-12-17 12:57:38 -06:00
parent 6b74f27e6c
commit 7afb2b07f8
3 changed files with 90 additions and 30 deletions

View File

@ -21,6 +21,8 @@ from requests import post
import websocket import websocket
from compare_json import compare_json from compare_json import compare_json
from config import get_config from config import get_config
from config import get_last_history
from config import set_last_history
from dir_to_json import get_json from dir_to_json import get_json
from misc import check_network from misc import check_network
from misc import path_join from misc import path_join
@ -101,8 +103,8 @@ class MunyalClient(Icon):
while True: while True:
print("Conectando al websocket") print("Conectando al websocket")
ws = websocket.WebSocketApp( ws = websocket.WebSocketApp(
# f"ws://{self.config['login']['user']}.loca.lt", f"ws://{self.config['login']['user']}.loca.lt",
"ws://127.0.0.1:12345", # "ws://127.0.0.1:12345",
on_message=self.__download, on_message=self.__download,
on_error=print, on_error=print,
on_close=lambda soc: self.__ws_close(ws)) on_close=lambda soc: self.__ws_close(ws))
@ -122,8 +124,8 @@ class MunyalClient(Icon):
folder = self.config["folder"] folder = self.config["folder"]
uuid = self.config["uuid"] uuid = self.config["uuid"]
while True: while True:
data = self.stack[0]
if self.stack: if self.stack:
data = self.stack[0]
try: try:
data["uuid"] = uuid data["uuid"] = uuid
if data["name"] in self.ignored: if data["name"] in self.ignored:
@ -156,7 +158,9 @@ class MunyalClient(Icon):
def uploader(self): def uploader(self):
print("Uploader") print("Uploader")
folder = self.config["folder"] folder = self.config["folder"]
last = get_json(folder) last = get_last_history()
if last is None:
last = get_json(folder)
while True: while True:
sleep(1) sleep(1)
actual = get_json(folder) actual = get_json(folder)
@ -176,6 +180,8 @@ class MunyalClient(Icon):
f["action"] = "add" f["action"] = "add"
self.stack.extend(add) self.stack.extend(add)
last = _actual last = _actual
if delete or add:
set_last_history(last)
def __upload(self, path): def __upload(self, path):
pass pass

View File

@ -5,12 +5,9 @@ import pathlib
import pickle import pickle
from hashlib import sha256 from hashlib import sha256
from tkinter import Button, Entry, Label, StringVar, Tk, filedialog
from uuid import uuid4 from uuid import uuid4
from PIL import Image, ImageTk
from misc import alert from misc import alert
from misc import get_os from misc import get_os
from misc import path_join from misc import path_join
@ -28,11 +25,26 @@ def __get_files_folder():
folder = path_join(os.path.expanduser("~"), "Munyal") folder = path_join(os.path.expanduser("~"), "Munyal")
return folder return folder
def get_last_history():
last_history_path = path_join(__get_config_folder(), "history.pkl")
if not os.path.exists(last_history_path):
return None
with open(last_history_path, "rb") as history:
return pickle.load(history)
def set_last_history(last_history):
last_history_path = path_join(__get_config_folder(), "history.pkl")
with open(last_history_path, "wb") as history:
pickle.dump(last_history, history)
def get_config(): def get_config():
for _ in range(3): for _ in range(3):
config = __get_config() config = __get_config()
if not config: if not config:
gui_config() try:
gui_config()
except ModuleNotFoundError as e:
cli_config()
else: else:
return config return config
@ -51,7 +63,7 @@ def __validate_values(user, password):
return True return True
def _set_config(root, user, password, folder): def _gui_validate_config(root, user, password, folder):
passwd_hash = sha256(password.get().encode("utf-8")).hexdigest() passwd_hash = sha256(password.get().encode("utf-8")).hexdigest()
if not user: if not user:
alert(root, "Introduce tu usuario de la red Munyal") alert(root, "Introduce tu usuario de la red Munyal")
@ -65,23 +77,49 @@ def _set_config(root, user, password, folder):
"Tu usuario o contraseña son incorrectos, favor de reintentarlo") "Tu usuario o contraseña son incorrectos, favor de reintentarlo")
else: else:
password.set("") password.set("")
folder_config = __get_config_folder() __set_config(user, passwd_hash, folder)
path = pathlib.Path(folder_config)
path.mkdir(parents=True, exist_ok=True)
with open(path_join(folder_config, "config"), "wb") as config_file:
config_file.write(
pickle.dumps({
"login": {
"user": user,
"password": passwd_hash
},
"folder": folder,
"uuid": str(uuid4())
}))
root.destroy() root.destroy()
def _cli_validate_config(user, password, folder):
if not user:
print("! Introduce tu usuario de la red munyal !")
elif not password:
print("! Introduce tu contraseña !")
elif not folder:
print("! Elige la carpeta que quieres sincronizar !")
elif not __validate_values(user, passwd_hash):
print("! Tu usuario o contraseña son incorrectos !")
else:
return True
return False
def __set_config(user, password, folder):
folder_config = __get_config_folder()
# Create config folder
path = pathlib.Path(folder_config)
path.mkdir(parents=True, exist_ok=True)
# Create Munyal folder
path = pathlib.Path(folder)
path.mkdir(parents=True, exist_ok=True)
with open(path_join(folder_config, "config"), "wb") as config_file:
config_file.write(
pickle.dumps({
"login": {
"user": user,
"password": passwd_hash
},
"folder": folder,
"uuid": str(uuid4())
}))
def gui_config(): def gui_config():
from tkinter import Button, Entry, Label, StringVar, Tk, filedialog
from PIL import Image, ImageTk
root = Tk() root = Tk()
root.geometry("250x420") root.geometry("250x420")
@ -105,7 +143,7 @@ def gui_config():
connect = Button( connect = Button(
root, root,
text="Conectar", text="Conectar",
command=lambda: _set_config(root, host.get(), passwd, folder.get())) command=lambda: _gui_validate_config(root, host.get(), passwd, folder.get()))
Label(root, text="MUNYAL").pack() Label(root, text="MUNYAL").pack()
Label(root, image=img_tk).pack() Label(root, image=img_tk).pack()
@ -125,6 +163,19 @@ def gui_config():
root.mainloop() root.mainloop()
def cli_config():
while True:
host = input("Introduce tu usuario de Munyal\n>>> ")
password = input("Introduce tu contraseña\n>>> ")
folder = input("Introduce el directorio que quieres sincronizar\n>> ")
if _cli_validate_config(host, password, folder):
break
else:
print("! Los datos introducidos no son correctos, intentalo de nuevo !")
passwd_hash = sha256(password.encode("utf-8")).hexdigest()
__set_config(user, password, folder)
def search_folder(field): def search_folder(field):
path = filedialog.askdirectory() path = filedialog.askdirectory()
field.set(path) field.set(path)

17
misc.py
View File

@ -2,7 +2,6 @@ import base64
import os import os
import pickle import pickle
import sys import sys
from tkinter import Button, Label, Toplevel
from requests import get from requests import get
from requests.exceptions import ConnectionError from requests.exceptions import ConnectionError
@ -28,12 +27,16 @@ def get_os():
def alert(window, message, title="Munyal"): def alert(window, message, title="Munyal"):
child = Toplevel(window) try:
child.title(title) from tkinter import Button, Label, Toplevel
label = Label(child, text=message) child = Toplevel(window)
button = Button(child, text="Aceptar", command=child.destroy) child.title(title)
label.pack() label = Label(child, text=message)
button.pack() button = Button(child, text="Aceptar", command=child.destroy)
label.pack()
button.pack()
except ModuleNotFoundError as e:
print("!", Message, "!")
def flatten_dirs_old(content, path="", action=""): def flatten_dirs_old(content, path="", action=""):