commit
43b72066f4
@ -7,19 +7,14 @@ from threading import Thread
|
|||||||
import time
|
import time
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
|
||||||
class BaseModule:
|
class Module:
|
||||||
output = None
|
output = None
|
||||||
|
position = 0
|
||||||
|
|
||||||
def registered(self, status_handler):
|
def registered(self, status_handler):
|
||||||
"""Called when this module is registered with a status handler"""
|
"""Called when this module is registered with a status handler"""
|
||||||
|
|
||||||
def tick(self):
|
class AsyncModule(Module):
|
||||||
"""Called once per tick"""
|
|
||||||
|
|
||||||
class Module(BaseModule):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class AsyncModule(BaseModule):
|
|
||||||
def registered(self, status_handler):
|
def registered(self, status_handler):
|
||||||
self.thread = Thread(target=self.mainloop)
|
self.thread = Thread(target=self.mainloop)
|
||||||
self.thread.daemon = True
|
self.thread.daemon = True
|
||||||
@ -44,35 +39,56 @@ class IOHandler:
|
|||||||
self.inp = inp
|
self.inp = inp
|
||||||
self.out = out
|
self.out = out
|
||||||
|
|
||||||
def write(self, message):
|
def write_line(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(self):
|
def read(self):
|
||||||
"""Interrupted respecting reader for stdin."""
|
"""Iterate over all input lines (Generator)"""
|
||||||
|
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
yield self.read_line()
|
||||||
|
except EOFError:
|
||||||
|
return
|
||||||
|
|
||||||
|
def read_line(self):
|
||||||
|
"""Interrupted respecting reader for stdin.
|
||||||
|
|
||||||
|
Raises EOFError if the end of stream has been reached"""
|
||||||
|
|
||||||
# try reading a line, removing any extra whitespace
|
|
||||||
try:
|
try:
|
||||||
line = self.inp.readline().decode("utf-8").strip()
|
line = self.inp.readline().decode("utf-8").strip()
|
||||||
# i3status sends EOF, or an empty line
|
|
||||||
if not line:
|
|
||||||
sys.exit(3)
|
|
||||||
return line
|
|
||||||
# exit on ctrl-c
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
sys.exit()
|
raise EOFError()
|
||||||
|
|
||||||
|
# i3status sends EOF, or an empty line
|
||||||
|
if not line:
|
||||||
|
raise EOFError()
|
||||||
|
return line
|
||||||
|
|
||||||
class JSONIO:
|
class JSONIO:
|
||||||
def __init__(self, io):
|
def __init__(self, io):
|
||||||
self.io = io
|
self.io = io
|
||||||
self.io.write(self.io.read())
|
self.io.write_line(self.io.read_line())
|
||||||
self.io.write(self.io.read())
|
self.io.write_line(self.io.read_line())
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
"""Iterate over all JSON input (Generator)"""
|
||||||
|
|
||||||
|
for line in self.io.read():
|
||||||
|
with self.parse_line(line) as j:
|
||||||
|
yield j
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def read(self):
|
def parse_line(self, line):
|
||||||
line, prefix = self.io.read(), ""
|
"""Parse a single line of JSON and write modified JSON back.
|
||||||
|
|
||||||
|
Usage is quite simple using the usual with-Syntax."""
|
||||||
|
|
||||||
|
prefix = ""
|
||||||
|
|
||||||
# ignore comma at start of lines
|
# ignore comma at start of lines
|
||||||
if line.startswith(","):
|
if line.startswith(","):
|
||||||
@ -80,8 +96,7 @@ class JSONIO:
|
|||||||
|
|
||||||
j = json.loads(line)
|
j = json.loads(line)
|
||||||
yield j
|
yield j
|
||||||
|
self.io.write_line(prefix + json.dumps(j))
|
||||||
self.io.write(prefix + json.dumps(j))
|
|
||||||
|
|
||||||
class I3statusHandler:
|
class I3statusHandler:
|
||||||
modules = []
|
modules = []
|
||||||
@ -90,22 +105,16 @@ class I3statusHandler:
|
|||||||
if fd is None:
|
if fd is None:
|
||||||
fd = sys.stdin
|
fd = sys.stdin
|
||||||
|
|
||||||
self.io = IOHandler(fd)
|
self.io = JSONIO(IOHandler(fd))
|
||||||
|
|
||||||
def register(self, module):
|
def register(self, module, position=0):
|
||||||
"""Register a new module."""
|
"""Register a new module."""
|
||||||
|
|
||||||
self.modules.append(module)
|
self.modules.append(module)
|
||||||
|
module.position = position
|
||||||
module.registered(self)
|
module.registered(self)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
jio = JSONIO(self.io)
|
for j in self.io.read():
|
||||||
|
for module in self.modules:
|
||||||
while True:
|
j.insert(module.position, module.output)
|
||||||
with jio.read() as j:
|
|
||||||
for module in self.modules:
|
|
||||||
module.tick()
|
|
||||||
|
|
||||||
output = module.output
|
|
||||||
if output:
|
|
||||||
j.insert(0, output)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user