modify all the logic to compare the json's

master
kirbylife 2020-12-15 20:54:28 -06:00
parent 3f0ee4af7e
commit 6b74f27e6c
4 changed files with 39 additions and 30 deletions

View File

@ -23,7 +23,6 @@ from compare_json import compare_json
from config import get_config
from dir_to_json import get_json
from misc import check_network
from misc import flatten_dirs
from misc import path_join
from pystray import Icon, Menu
from pystray import MenuItem as Item
@ -36,7 +35,7 @@ except ImportError:
_online_icon = Image.open("img/icons/online.png")
_offline_icon = Image.open("img/icons/offline.png")
_standby_icon = Image.open("img/icons/offline.png")
_standby_icon = Image.open("img/icons/standby.png")
if not int(os.getenv("VERBOSE", 1)):
def X(*args, **kwargs):
@ -57,6 +56,9 @@ class MunyalClient(Icon):
# if not self.ws_online:
# self.icon = _standby_icon
# return False
if self.__ws_break:
self.icon = _standby_icon
return False
ping = check_network("http://google.com", 443)
if ping:
self.icon = _online_icon
@ -91,16 +93,16 @@ class MunyalClient(Icon):
# thread_downloader.start()
# thread_listener.start()
self.run(self.__run)
self.menu = Menu(Item("Exit", lambda *args: sys.exit(0)))
self.run(self.__run)
def __run(self, icon):
icon.visible = True
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))
@ -111,6 +113,7 @@ class MunyalClient(Icon):
def __ws_close(self, ws):
print("WebSocket cerrado")
self.__ws_break = True
self.icon = _standby_icon
def listener(self, ws):
self.ws = ws
@ -119,10 +122,9 @@ class MunyalClient(Icon):
folder = self.config["folder"]
uuid = self.config["uuid"]
while True:
print(self.stack)
data = self.stack[0]
if self.stack:
try:
data = self.stack[0]
data["uuid"] = uuid
if data["name"] in self.ignored:
self.ignored.remove(data["name"])
@ -135,8 +137,14 @@ class MunyalClient(Icon):
self.ws.send(json.dumps(data))
self.stack.pop(0)
except Exception as e:
print(e)
print("Error uploading file, trying again")
if data["is_file"] and data["action"] == "add":
path = path_join(folder, data["name"])
if not os.path.exists(path):
self.stack.pop(0)
print("the file does not exists anymore, skiped")
else:
print(e)
print("Error uploading file, trying again")
if not self.is_online() or self.__ws_break:
self.__ws_break = False
self.ws = None
@ -157,13 +165,13 @@ class MunyalClient(Icon):
if delete:
print("Cosas eliminadas:")
print(delete)
print(len(delete))
for f in delete:
f["action"] = "delete"
self.stack.extend(delete)
if add:
print("Cosas agregadas:")
print(add)
print(len(add))
for f in add:
f["action"] = "add"
self.stack.extend(add)

View File

@ -1,7 +1,7 @@
import json
from dir_to_json import get_json
from misc import flatten_dirs
from flatten_dirs import flatten_dirs
def compare_json(json_1, json_2):

19
flatten_dirs.py 100644
View File

@ -0,0 +1,19 @@
from misc import path_join
def _flatten_dirs(content, path=""):
for item in content:
item["name"] = path_join(path, item["name"])
if item["is_file"]:
yield item
else:
yield from _flatten_dirs(item["content"], item["name"])
del item["content"]
yield item
def flatten_dirs(content, path=""):
dirs = _flatten_dirs(content, path=path)
output = {}
for item in dirs:
output[item["name"]] = item
return output

18
misc.py
View File

@ -36,24 +36,6 @@ def alert(window, message, title="Munyal"):
button.pack()
def _flatten_dirs(content, path=""):
for item in content:
item["name"] = path_join(path, item["name"])
if item["is_file"]:
yield item
else:
yield from _flatten_dirs(item["content"], item["name"])
del item["content"]
yield item
def flatten_dirs(content, path=""):
dirs = _flatten_dirs(content, path=path)
output = {}
for item in dirs:
output[item["name"]] = item
return output
def flatten_dirs_old(content, path="", action=""):
output = []
for item in content: