Splitted IO handling even a bit more :-)
This commit is contained in:
parent
ab5afd0227
commit
60b5def7d9
@ -43,13 +43,13 @@ class IOHandler:
|
|||||||
self.inp = inp
|
self.inp = inp
|
||||||
self.out = out
|
self.out = out
|
||||||
|
|
||||||
def print_line(self, message):
|
def write(self, message):
|
||||||
"""Unbuffered printing to stdout."""
|
"""Unbuffered printing to stdout."""
|
||||||
|
|
||||||
self.out.write(message + "\n")
|
self.out.write(message + "\n")
|
||||||
self.out.flush()
|
self.out.flush()
|
||||||
|
|
||||||
def read_line(self):
|
def read(self):
|
||||||
"""Interrupted respecting reader for stdin."""
|
"""Interrupted respecting reader for stdin."""
|
||||||
|
|
||||||
# try reading a line, removing any extra whitespace
|
# try reading a line, removing any extra whitespace
|
||||||
@ -63,9 +63,26 @@ class IOHandler:
|
|||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
|
class JSONIO:
|
||||||
|
def __init__(self, io):
|
||||||
|
self.io = io
|
||||||
|
self.io.write(self.io.read())
|
||||||
|
self.io.write(self.io.read())
|
||||||
|
|
||||||
|
def write(self, prefix, j):
|
||||||
|
self.io.write(prefix + json.dumps(j))
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
line, prefix = self.io.read(), ""
|
||||||
|
|
||||||
|
# ignore comma at start of lines
|
||||||
|
if line.startswith(","):
|
||||||
|
line, prefix = line[1:], ","
|
||||||
|
|
||||||
|
return (prefix, json.loads(line))
|
||||||
|
|
||||||
class I3statusHandler:
|
class I3statusHandler:
|
||||||
modules = []
|
modules = []
|
||||||
fd = sys.stdin
|
|
||||||
|
|
||||||
def __init__(self, fd=None):
|
def __init__(self, fd=None):
|
||||||
if fd is None:
|
if fd is None:
|
||||||
@ -80,25 +97,16 @@ class I3statusHandler:
|
|||||||
module.registered(self)
|
module.registered(self)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.io.print_line(self.io.read_line())
|
jio = JSONIO(self.io)
|
||||||
self.io.print_line(self.io.read_line())
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
line, prefix = self.io.read_line(), ""
|
prefix, j = jio.read()
|
||||||
|
|
||||||
# ignore comma at start of lines
|
|
||||||
if line.startswith(","):
|
|
||||||
line, prefix = line[1:], ","
|
|
||||||
|
|
||||||
j = json.loads(line)
|
|
||||||
|
|
||||||
for module in self.modules:
|
for module in self.modules:
|
||||||
module.tick()
|
module.tick()
|
||||||
|
|
||||||
output = module.output
|
output = module.output
|
||||||
|
|
||||||
if output:
|
if output:
|
||||||
j.insert(0, output)
|
j.insert(0, output)
|
||||||
|
|
||||||
# and echo back new encoded json
|
jio.write(prefix, j)
|
||||||
self.io.print_line(prefix+json.dumps(j))
|
|
||||||
|
Loading…
Reference in New Issue
Block a user