-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolutils.py
67 lines (55 loc) · 1.87 KB
/
toolutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os,sys,platform,hashlib,socket,threading,requests,json,subprocess
from getmac import get_mac_address
def get_ip():
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.connect(('8.8.8.8',80))
ip=s.getsockname()[0]
s.close()
return ip
def normalize_path(path):
return path.replace("\\","/")
def getpath():
return os.path.dirname(os.path.realpath(sys.argv[0]))
def get_sys_info():
return {"platform":platform.platform(aliased=True,terse=True),
"hostname":platform.node(),
"architecture":platform.machine(),
"processor":platform.processor(),
"system":platform.system(),
"mac":get_mac_address()
}
def md5(path):
f = open(getpath()+path,'rb')
md5_obj = hashlib.md5()
md5_obj.update(f.read())
hash_code = md5_obj.hexdigest()
f.close()
return str(hash_code).lower()
def is_win():
return platform.system()=='Windows'
def is_mac():
return platform.system()=='Darwin'
def safe_serialize(obj):
default = lambda o: f"<<non-serializable: {type(o).__qualname__}>>"
return json.dumps(obj, default=default)
def show_folder(relpath):
from toolutils import getpath
path = os.path.join(getpath(),relpath)
if sys.platform == "win32":
os.startfile(path)
else:
opener ="open" if sys.platform == "darwin" else "xdg-open"
subprocess.call([opener, path])
def debounce(wait_time):
def decorator(function):
def debounced(*args, **kwargs):
def call_function():
debounced._timer = None
return function(*args, **kwargs)
if debounced._timer is not None:
debounced._timer.cancel()
debounced._timer = threading.Timer(wait_time, call_function)
debounced._timer.start()
debounced._timer = None
return debounced
return decorator