aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/whconsole.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/whconsole.py')
-rwxr-xr-xscripts/whconsole.py30
1 files changed, 19 insertions, 11 deletions
diff --git a/scripts/whconsole.py b/scripts/whconsole.py
index 5e76b3bc..59e726f1 100755
--- a/scripts/whconsole.py
+++ b/scripts/whconsole.py
@@ -6,10 +6,11 @@
# This file may be distributed under the terms of the GNU GPLv3 license.
import sys, os, optparse, socket, fcntl, select, json, errno, time
+
# Set a file-descriptor as non-blocking
def set_nonblock(fd):
- fcntl.fcntl(fd, fcntl.F_SETFL
- , fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK)
+ fcntl.fcntl(fd, fcntl.F_SETFL, fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK)
+
def webhook_socket_create(uds_filename):
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
@@ -22,14 +23,16 @@ def webhook_socket_create(uds_filename):
if e.errno == errno.ECONNREFUSED:
time.sleep(0.1)
continue
- sys.stderr.write("Unable to connect socket %s [%d,%s]\n"
- % (uds_filename, e.errno,
- errno.errorcode[e.errno]))
+ sys.stderr.write(
+ "Unable to connect socket %s [%d,%s]\n"
+ % (uds_filename, e.errno, errno.errorcode[e.errno])
+ )
sys.exit(-1)
break
sys.stderr.write("Connection.\n")
return sock
+
class KeyboardReader:
def __init__(self, uds_filename):
self.kbd_fd = sys.stdin.fileno()
@@ -39,42 +42,46 @@ class KeyboardReader:
self.poll.register(sys.stdin, select.POLLIN | select.POLLHUP)
self.poll.register(self.webhook_socket, select.POLLIN | select.POLLHUP)
self.kbd_data = self.socket_data = b""
+
def process_socket(self):
data = self.webhook_socket.recv(4096)
if not data:
sys.stderr.write("Socket closed\n")
sys.exit(0)
- parts = data.split(b'\x03')
+ parts = data.split(b"\x03")
parts[0] = self.socket_data + parts[0]
self.socket_data = parts.pop()
for line in parts:
sys.stdout.write("GOT: %s\n" % (line,))
+
def process_kbd(self):
data = os.read(self.kbd_fd, 4096)
- parts = data.split(b'\n')
+ parts = data.split(b"\n")
parts[0] = self.kbd_data + parts[0]
self.kbd_data = parts.pop()
for line in parts:
line = line.strip()
- if not line or line.startswith(b'#'):
+ if not line or line.startswith(b"#"):
continue
try:
m = json.loads(line)
except:
sys.stderr.write("ERROR: Unable to parse line\n")
continue
- cm = json.dumps(m, separators=(',', ':'))
+ cm = json.dumps(m, separators=(",", ":"))
sys.stdout.write("SEND: %s\n" % (cm,))
self.webhook_socket.send(cm.encode() + b"\x03")
+
def run(self):
while 1:
- res = self.poll.poll(1000.)
+ res = self.poll.poll(1000.0)
for fd, event in res:
if fd == self.kbd_fd:
self.process_kbd()
else:
self.process_socket()
+
def main():
usage = "%prog [options] <socket filename>"
opts = optparse.OptionParser(usage)
@@ -85,5 +92,6 @@ def main():
ml = KeyboardReader(args[0])
ml.run()
-if __name__ == '__main__':
+
+if __name__ == "__main__":
main()