Skip to content

Commit

Permalink
Support for standalone operation
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
enkore committed Feb 22, 2013
1 parent 4c5dfbe commit 5474980
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions i3pystatus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,34 @@ def read_line(self):
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
Expand Down Expand Up @@ -97,9 +125,14 @@ def parse_line(self, line):
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."""
Expand All @@ -109,6 +142,8 @@ def register(self, module, position=0):
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

0 comments on commit 5474980

Please sign in to comment.