munyal-client/misc.py

90 lines
2.3 KiB
Python
Raw Normal View History

2019-08-10 01:44:09 +00:00
import base64
import os
import pickle
import sys
from tkinter import Button, Label, Toplevel
2020-12-08 20:45:42 +00:00
from requests import get
from requests.exceptions import ConnectionError
2019-08-10 01:44:09 +00:00
from tcping import Ping
def check_network(ip, port):
2020-12-08 20:45:42 +00:00
try:
req = get(f"{ip}").status_code
return req in range(200, 300)
except ConnectionError:
return False
# ping = Ping(ip, port, 20)
# ping.ping(1)
# return ping.result.rows[0].successed == 1
2019-08-10 01:44:09 +00:00
def get_os():
2020-12-08 21:05:25 +00:00
if sys.platform in ["linux", "windows", "win32"]:
2019-08-10 01:44:09 +00:00
return sys.platform
raise Exception("OS not supported")
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()
2020-12-08 20:45:42 +00:00
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=""):
2020-12-08 20:45:42 +00:00
output = []
for item in content:
if item["is_file"]:
2020-12-09 03:37:51 +00:00
item["name"] = path_join(path, item["name"])
2020-12-08 20:45:42 +00:00
output.append(item)
else:
if item["content"] and action == "add":
output.extend(
flatten_dirs(item["content"],
2020-12-09 03:37:51 +00:00
path_join(path, item["name"]),
2020-12-08 20:45:42 +00:00
action=action))
else:
2020-12-09 03:37:51 +00:00
item["name"] = path_join(path, item["name"])
2020-12-08 20:45:42 +00:00
output.append(item)
return output
2020-12-09 03:37:51 +00:00
def path_join(*items):
return os.path.join(*items).replace("\\", "/")
if __name__ == "__main__":
from dir_to_json import get_json
import json
folder = sys.argv[1]
action = sys.argv[2]
try:
json_1 = get_json(folder)
except Exception as e:
print(e)
print("error outside")
sys.exit(1)
print(json.dumps(list(flatten_dirs(json_1)), indent=2))