-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
80 lines (57 loc) · 1.83 KB
/
client.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python
import os, os.path, sys
from twisted.internet import reactor
from twisted.internet import inotify
from twisted.python import filepath
from shet.client import ShetClient
from parser import parse
from mode import Mode
from buttons import Buttons
from button_press import ButtonPress
class Client(ShetClient):
def __init__(self, bindings_dir, *args, **kwargs):
ShetClient.__init__(self, *args, **kwargs)
self.bindings_dir = bindings_dir
self.bindings = []
self.load_bindings()
# Setup inotify
self.notifier = inotify.INotify()
self.notifier.startReading()
self.notifier.watch(filepath.FilePath(self.bindings_dir),
callbacks = [self.notify])
self.watch_event("/lounge/panel/pressed", self.on_btn_pressed)
print "Button monitor started..."
def notify(self, notifier, filepath, mask):
if filepath.path.endswith(".pom"):
print "\033[2J" # Clear the screen
self.load_bindings()
def load_bindings(self):
bindings = []
for file in filter((lambda n: n.endswith(".pom")),
os.listdir(self.bindings_dir)):
file = os.path.join(self.bindings_dir, file)
try:
print "Reading %s"%file,
bindings += parse(open(file, "r").read())
print "OK."
except Exception, e:
print "Error parsing bindings:", e
self.bindings = bindings
print "Done loading bindings."
def on_btn_pressed(self, data):
mode_data, button_data = data
mode = Mode(*mode_data)
buttons = Buttons(*button_data)
press = ButtonPress(mode, buttons)
for binding in self.bindings:
if binding.is_triggered_by(press):
args = binding.get_call(press)
try:
if binding.prop:
self.set(*args)
else:
self.call(*args)
except Exception, e:
print e
if __name__ == "__main__":
Client(sys.argv[1]).run()