-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
143 lines (118 loc) · 4.38 KB
/
handler.py
File metadata and controls
143 lines (118 loc) · 4.38 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
def handler():
import os, sys, json, subprocess
from pynput import keyboard
from ui import mk_config_dir, get_config_dir, get_text_from_key
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
mk_config_dir()
CONFIG_DIR = get_config_dir()
def log(*args):
with open(os.path.join(CONFIG_DIR,"latest.log"),"a") as f:
message = "\n"
for x in list(args):
message+=" "+str(x)
print(message)
f.write(message)
CDIR = os.path.abspath("./") if not hasattr(sys,"_MEIPASS") else sys._MEIPASS
log(f"CDIR={CDIR}")
LOCK_PATH = os.path.join(CDIR,"__LOCK__")
if os.path.exists(LOCK_PATH):
log("__LOCK__ present; quitting...")
raise Exception
else:
with open(LOCK_PATH,"w"):pass
class FileChangedHandler(FileSystemEventHandler):
def __init__(self, path:str):
super().__init__()
self.path = path
def on_modified(self, event):
if event.src_path.endswith(self.path):
update_shortcuts()
observer1 = None
class FileCreatedHandler(FileSystemEventHandler):
def on_created(self, event):
log("watchdog: file created")
if event.src_path.endswith("__BREAK__"):
log("watchdog: break...")
observer1.stop()
pressed = {}
MAP = { # Qt => pynput
"Meta": ["cmd",None],
"Control": [["ctrl","blacklist",["win32"]],["ctrl_l","whitelist",["win32"]]],
"Alt": ["alt_l","whitelist",["win32"]],
"CapsLock": ["caps_lock",None]
}
def map_from_qt_key(key:str): # `key` is `str` from `Qt.Key`
mapping = MAP.get(key)
if not mapping: return key.lower()
maps = mapping[0]
if type(maps) == str:
map_type = mapping[1]
if map_type == "blacklist":
if sys.platform in mapping[2]: return
elif map_type == "whitelist":
if not sys.platform in mapping[2]: return
return maps
else:
for set in mapping:
set_type = set[1]
if set_type == "blacklist":
if sys.platform in set[2]: continue
elif set_type == "whitelist":
if not sys.platform in set[2]: continue
return set[0]
return key
SHORTCUTS = []
def update_shortcuts():
nonlocal SHORTCUTS
with open(os.path.join(CONFIG_DIR,"config.json")) as f:
SHORTCUTS = json.load(f)
log("SHORTCUTS => ",SHORTCUTS)
update_shortcuts()
def on_press(key):
# log(f"Pressed: {get_text_from_key(key)}")
key_name = get_text_from_key(key)
if pressed.get(key_name): return
pressed[key_name] = True
for s in SHORTCUTS:
cut:str = s["Shortcut"]
keys = cut.split(" + ")
kt = key_name
if map_from_qt_key(keys[-1]) != kt: continue
broken = False
for k in keys:
mapped_key = map_from_qt_key(k)
if not mapped_key in pressed.keys() or not pressed[mapped_key]:
broken = True
break
if broken: continue
try:
log("Executing:",s["Command"])
subprocess.Popen(s["Command"], shell=True)
except Exception as e:
log(f"Error executing shortcut: {e}")
def on_release(key):
# log(f"Released: {get_text_from_key(key)}")
pressed[get_text_from_key(key)] = False
log("Verified listener thread")
keyboard_listener = keyboard.Listener(
on_press=on_press,
on_release=on_release)
log("Commencing listening...")
keyboard_listener.start()
observer = Observer()
observer1 = Observer()
observer.schedule(FileChangedHandler("config.json"),CONFIG_DIR,recursive=False)
observer1.schedule(FileCreatedHandler(),CDIR,recursive=False)
observer.start()
observer1.start()
log("Watchdogs started...")
BREAK_PATH = os.path.join(CDIR,"__BREAK__")
if os.path.exists(BREAK_PATH):
os.remove(BREAK_PATH)
try:
observer1.join()
finally:
log("Exitting handler...")
os.remove(BREAK_PATH)
keyboard_listener.stop()