-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatushandler.py
72 lines (53 loc) · 1.79 KB
/
statushandler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
import sys
import json
import urllib2
class I3statusHandler:
modules = []
def __init__(self):
pass
def register_module(self, module):
""" Register a new module. """
# check if module implemented the
# correct functions
if not hasattr(module, 'output'):
raise Exception("Module %s does not implement \
all the needed functions!".format(module))
self.modules.append(module)
def print_line(self, message):
""" Non-buffered printing to stdout. """
sys.stdout.write(message + '\n')
sys.stdout.flush()
def read_line(self):
""" Interrupted respecting reader for stdin. """
# try reading a line, removing any extra whitespace
try:
line = sys.stdin.readline().strip()
# i3status sends EOF, or an empty line
if not line:
sys.exit(3)
return line
# exit on ctrl-c
except KeyboardInterrupt:
sys.exit()
def run(self):
self.print_line(self.read_line())
self.print_line(self.read_line())
while True:
line, prefix = self.read_line(), ''
# ignore comma at start of lines
if line.startswith(','):
line, prefix = line[1:], ','
j = json.loads(line)
for module in self.modules:
output = module.output()
if output:
j.insert(0, module.output())
# and echo back new encoded json
self.print_line(prefix+json.dumps(j))
def has_internet_connection():
try:
response=urllib2.urlopen('http://74.125.113.99',timeout=1)
return True
except urllib2.URLError as err: pass
return False