]> git.sur5r.net Git - i3/i3status/blob - contrib/wrapper.py
contrib: python version of the wrapper
[i3/i3status] / contrib / wrapper.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # This script is a simple wrapper which prefixes each i3status line with custom
5 # information. It is a python reimplementation of:
6 # http://code.stapelberg.de/git/i3status/tree/contrib/wrapper.pl
7 #
8 # To use it, ensure your ~/.i3status.conf contains this line:
9 #     output_format = "i3bar"
10 # in the 'general' section.
11 # Then, in your ~/.i3/config, use:
12 #     status_command i3status | ~/i3status/contrib/wrapper.py
13 # In the 'bar' section.
14 #
15 # In its current version it will display the cpu frequency governor, but you
16 # are free to change it to display whatever you like, see the comment in the
17 # source code below.
18 #
19 # © 2012 Valentin Haenel <valentin.haenel@gmx.de>
20 #
21 # This program is free software. It comes without any warranty, to the extent
22 # permitted by applicable law. You can redistribute it and/or modify it under
23 # the terms of the Do What The Fuck You Want To Public License (WTFPL), Version
24 # 2, as published by Sam Hocevar. See http://sam.zoy.org/wtfpl/COPYING for more
25 # details.
26
27 import sys
28 import json
29
30 def get_governor():
31     """ Get the current governor for cpu0, assuming all CPUs use the same. """
32     with open('/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor') as fp:
33         return fp.readlines()[0].strip()
34
35 def print_line(message):
36     """ Non-buffered printing to stdout. """
37     sys.stdout.write(message + '\n')
38     sys.stdout.flush()
39
40 def read_line():
41     """ Interrupted respecting reader for stdin. """
42     # try reading a line, removing any extra whitespace
43     try:
44         line = sys.stdin.readline().strip()
45         # i3status sends EOF, or an empty line
46         if not line:
47             sys.exit(3)
48         return line
49     # exit on ctrl-c
50     except KeyboardInterrupt:
51         sys.exit()
52
53 if __name__ == '__main__':
54     # Skip the first line which contains the version header.
55     print_line(read_line())
56
57     # The second line contains the start of the infinite array.
58     print_line(read_line())
59
60     while True:
61         line, prefix = read_line(), ''
62         # ignore comma at start of lines
63         if line.startswith(','):
64             line, prefix = line[1:], ','
65
66         j = json.loads(line)
67         # insert information into the start of the json, but could be anywhere
68         # CHANGE THIS LINE TO INSERT SOMETHING ELSE
69         j.insert(0, {'full_text' : '%s' % get_governor(), 'name' : 'gov'})
70         # and echo back new encoded json
71         print_line(prefix+json.dumps(j))