Support for standalone operation

Specify standalone=True to the I3statushandler constructor
to run i3pystatus without i3status in the background.
i3pystatus won't read input from stdin or any other
file object specified with input_stream.

The keyword argument interval specifies how often output should
be generated. The default is 1 (second).


Sorry guys for changing the way i3pystatus "way of operation"
is set so often. If you're want the "self-contained" mode
(you execute i3pystatus, i3pystatus automatically starts
i3status), don't set the file attribute, but pass the file
descriptor of the pipe as input_stream like this:

process = subprocess.Popen(["i3status", "-c", "~/.i3/status"], stdout=subprocess.PIPE, universal_newlines=True)
status = i3pystatus(input_stream=process.stdout)

On a side note:
The main class name has been changed to i3pystatus, but I3statusHandler
is still available as an alias. Use whichever you prefer :-)
(Linux is about choice after all)
This commit is contained in:
enkore 2013-02-22 21:23:58 +01:00
parent 4c5dfbe429
commit 5474980b11

View File

@ -68,6 +68,34 @@ class IOHandler:
raise EOFError()
return line
class StandaloneIO(IOHandler):
"""
I/O handler for standalone usage of i3pystatus (w/o i3status)
writing as usual, reading will always return a empty JSON array,
and the i3bar protocol header
"""
n = -1
def __init__(self, interval=1):
super().__init__()
self.interval = interval
def read(self):
while True:
yield self.read_line()
time.sleep(self.interval)
def read_line(self):
self.n += 1
if self.n == 0:
return '{"version": 1}'
elif self.n == 1:
return "["
else:
return ",[]"
class JSONIO:
def __init__(self, io):
self.io = io
@ -97,9 +125,14 @@ class JSONIO:
yield j
self.io.write_line(prefix + json.dumps(j))
class I3statusHandler:
class i3pystatus:
modules = []
file = sys.stdin
def __init__(self, standalone=False, interval=1, input_stream=sys.stdin):
if standalone:
self.io = StandaloneIO(interval)
else:
self.io = IOHandler(input_stream)
def register(self, module, position=0):
"""Register a new module."""
@ -109,6 +142,8 @@ class I3statusHandler:
module.registered(self)
def run(self):
for j in JSONIO(IOHandler(self.file)).read():
for j in JSONIO(self.io).read():
for module in self.modules:
j.insert(module.position, module.output)
I3statusHandler = i3pystatus