diff --git a/client.py b/client.py index 274395c..d911618 100755 --- a/client.py +++ b/client.py @@ -21,6 +21,8 @@ from requests import post import websocket from compare_json import compare_json 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 misc import check_network from misc import path_join @@ -101,8 +103,8 @@ class MunyalClient(Icon): while True: print("Conectando al websocket") ws = websocket.WebSocketApp( - # f"ws://{self.config['login']['user']}.loca.lt", - "ws://127.0.0.1:12345", + f"ws://{self.config['login']['user']}.loca.lt", + # "ws://127.0.0.1:12345", on_message=self.__download, on_error=print, on_close=lambda soc: self.__ws_close(ws)) @@ -122,8 +124,8 @@ class MunyalClient(Icon): folder = self.config["folder"] uuid = self.config["uuid"] while True: - data = self.stack[0] if self.stack: + data = self.stack[0] try: data["uuid"] = uuid if data["name"] in self.ignored: @@ -156,7 +158,9 @@ class MunyalClient(Icon): def uploader(self): print("Uploader") folder = self.config["folder"] - last = get_json(folder) + last = get_last_history() + if last is None: + last = get_json(folder) while True: sleep(1) actual = get_json(folder) @@ -176,6 +180,8 @@ class MunyalClient(Icon): f["action"] = "add" self.stack.extend(add) last = _actual + if delete or add: + set_last_history(last) def __upload(self, path): pass diff --git a/config.py b/config.py index 78c5efd..d72d2d8 100644 --- a/config.py +++ b/config.py @@ -5,12 +5,9 @@ import pathlib import pickle from hashlib import sha256 -from tkinter import Button, Entry, Label, StringVar, Tk, filedialog from uuid import uuid4 -from PIL import Image, ImageTk - from misc import alert from misc import get_os from misc import path_join @@ -28,11 +25,26 @@ def __get_files_folder(): folder = path_join(os.path.expanduser("~"), "Munyal") 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(): for _ in range(3): config = __get_config() if not config: - gui_config() + try: + gui_config() + except ModuleNotFoundError as e: + cli_config() else: return config @@ -51,7 +63,7 @@ def __validate_values(user, password): 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() if not user: 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") else: password.set("") - folder_config = __get_config_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()) - })) + __set_config(user, passwd_hash, folder) 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(): + from tkinter import Button, Entry, Label, StringVar, Tk, filedialog + from PIL import Image, ImageTk + root = Tk() root.geometry("250x420") @@ -105,7 +143,7 @@ def gui_config(): connect = Button( root, 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, image=img_tk).pack() @@ -125,6 +163,19 @@ def gui_config(): 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): path = filedialog.askdirectory() field.set(path) diff --git a/misc.py b/misc.py index 651f14d..3ce0ded 100644 --- a/misc.py +++ b/misc.py @@ -2,7 +2,6 @@ import base64 import os import pickle import sys -from tkinter import Button, Label, Toplevel from requests import get from requests.exceptions import ConnectionError @@ -28,12 +27,16 @@ def get_os(): def alert(window, message, title="Munyal"): - child = Toplevel(window) - child.title(title) - label = Label(child, text=message) - button = Button(child, text="Aceptar", command=child.destroy) - label.pack() - button.pack() + try: + from tkinter import Button, Label, Toplevel + child = Toplevel(window) + child.title(title) + label = Label(child, text=message) + 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=""):