added paralel client interaction

master
kirbylife 2020-12-08 20:47:09 -06:00
parent eab16c032d
commit 16b733fc03
2 changed files with 39 additions and 10 deletions

View File

@ -47,6 +47,7 @@ class MunyalClient(Icon):
self.icon = _standby_icon self.icon = _standby_icon
self.config = get_config() self.config = get_config()
self.stack = [] self.stack = []
self.ignored = []
def is_online(self): def is_online(self):
# if not self.ws_online: # if not self.ws_online:
@ -96,7 +97,7 @@ class MunyalClient(Icon):
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=print, 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))
ws.on_open = lambda soc: self.listener(soc) ws.on_open = lambda soc: self.listener(soc)
@ -111,12 +112,17 @@ class MunyalClient(Icon):
self.ws = ws self.ws = ws
def wrapper(*args): def wrapper(*args):
folder = self.config["folder"]
uuid = self.config["uuid"]
while True: while True:
print(self.stack) print(self.stack)
if self.stack: if self.stack:
try: try:
data = self.stack[0] data = self.stack[0]
folder = self.config["folder"] data["uuid"] = uuid
if data["name"] in self.ignored:
self.ignored.remove(data["name"])
self.stack.pop(0)
if data["is_file"] and data["action"] == "add": if data["is_file"] and data["action"] == "add":
full_path = os.path.join(folder, data["name"]) full_path = os.path.join(folder, data["name"])
data["file"] = base64.b85encode( data["file"] = base64.b85encode(
@ -124,7 +130,7 @@ class MunyalClient(Icon):
self.ws.send(json.dumps(data)) self.ws.send(json.dumps(data))
self.stack.pop(0) self.stack.pop(0)
except Exception as e: except Exception as e:
print(e.with_traceback()) print(e)
print("Error uploading file, trying again") print("Error uploading file, trying again")
if not self.is_online() or self.__ws_break: if not self.is_online() or self.__ws_break:
self.__ws_break = False self.__ws_break = False
@ -163,13 +169,32 @@ class MunyalClient(Icon):
def __upload(self, path): def __upload(self, path):
pass pass
def downloader(self): def downloader(self, data):
print("Downloader") self.ignored.append(data["name"])
while True: full_path = os.path.join(self.config["folder"], name)
sleep(7) if data["is_file"]:
if data["action"] == "add":
directory = os.path.split(full_path)[0]
if not os.path.exists(directory):
os.makedirs(directory, exist_ok=True)
with open(full_path, "wb") as f:
f.write(base64.b85decode(data["file"].encode("ascii")))
else:
os.remove(full_path)
else:
if data["action"] == "add":
os.mkdir(full_path)
else:
shutil.rmtree(full_path)
def __download(self, path):
pass def __download(self, message):
try:
data = json.loads(message)
except Exception:
return
if data["uuid"] != self.config["uuid"]:
downloader(data)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -3,9 +3,12 @@ import shutil
import sys import sys
import pathlib import pathlib
import pickle import pickle
from hashlib import sha256 from hashlib import sha256
from tkinter import Button, Entry, Label, StringVar, Tk, filedialog from tkinter import Button, Entry, Label, StringVar, Tk, filedialog
from uuid import uuid4
from PIL import Image, ImageTk from PIL import Image, ImageTk
from misc import alert, get_os from misc import alert, get_os
@ -70,7 +73,8 @@ def _set_config(root, user, password, folder):
"user": user, "user": user,
"password": passwd_hash "password": passwd_hash
}, },
"folder": folder "folder": folder,
"uuid": str(uuid4())
})) }))
root.destroy() root.destroy()