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

View File

@ -1,7 +1,7 @@
import json import json
from dir_to_json import get_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): 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() 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=""): def flatten_dirs_old(content, path="", action=""):
output = [] output = []
for item in content: for item in content: