From 60b5def7d91f3f051c5a7a94fb628dc12a14419e Mon Sep 17 00:00:00 2001 From: enkore Date: Sun, 17 Feb 2013 01:25:41 +0100 Subject: [PATCH] Splitted IO handling even a bit more :-) --- i3pystatus/__init__.py | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/i3pystatus/__init__.py b/i3pystatus/__init__.py index bfb38ac..602545c 100644 --- a/i3pystatus/__init__.py +++ b/i3pystatus/__init__.py @@ -43,13 +43,13 @@ class IOHandler: self.inp = inp self.out = out - def print_line(self, message): + def write(self, message): """Unbuffered printing to stdout.""" self.out.write(message + "\n") self.out.flush() - def read_line(self): + def read(self): """Interrupted respecting reader for stdin.""" # try reading a line, removing any extra whitespace @@ -63,9 +63,26 @@ class IOHandler: except KeyboardInterrupt: 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: modules = [] - fd = sys.stdin def __init__(self, fd=None): if fd is None: @@ -80,25 +97,16 @@ class I3statusHandler: module.registered(self) def run(self): - self.io.print_line(self.io.read_line()) - self.io.print_line(self.io.read_line()) + jio = JSONIO(self.io) while True: - line, prefix = self.io.read_line(), "" - - # ignore comma at start of lines - if line.startswith(","): - line, prefix = line[1:], "," - - j = json.loads(line) + prefix, j = jio.read() for module in self.modules: module.tick() output = module.output - if output: j.insert(0, output) - # and echo back new encoded json - self.io.print_line(prefix+json.dumps(j)) + jio.write(prefix, j)