From d4ea14b0b34097b79bd203c7d57c459ecaed34d2 Mon Sep 17 00:00:00 2001 From: Ultrabug Date: Wed, 18 Mar 2015 11:31:39 +0100 Subject: new online_status module by @obb --- py3status/modules/online_status.py | 70 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 py3status/modules/online_status.py (limited to 'py3status/modules/online_status.py') diff --git a/py3status/modules/online_status.py b/py3status/modules/online_status.py new file mode 100644 index 0000000..432ffb0 --- /dev/null +++ b/py3status/modules/online_status.py @@ -0,0 +1,70 @@ +# -*- coding: utf-8 -*- +""" +Module displaying if a connection to the internet is established. + +@author obb +""" + +from time import time +try: + # python3 + from urllib.request import urlopen +except: + from urllib2 import urlopen + + +class Py3status: + """ + Configuration parameters: + - cache_timeout : how often to run the check + - format_offline : what to display when offline + - format_online : what to display when online + - timeout : how long before deciding we're offline + - url : connect to this url to check the connection status + """ + # available configuration parameters + cache_timeout = 10 + format_offline = '■' + format_online = '●' + timeout = 2 + url = 'http://www.google.com' + + def _connection_present(self): + try: + urlopen(self.url, timeout=self.timeout) + except: + return False + else: + return True + + def online_status(self, i3s_output_list, i3s_config): + self.offline_color = i3s_config['color_bad'] + self.online_color = i3s_config['color_good'] + + response = { + 'cached_until': time() + self.cache_timeout + } + + connected = self._connection_present() + if connected: + response['full_text'] = self.format_online + response['color'] = self.online_color + else: + response['full_text'] = self.format_offline + response['color'] = self.offline_color + + return response + +if __name__ == "__main__": + """ + Test this module by calling it directly. + """ + from time import sleep + x = Py3status() + config = { + 'color_good': '#00FF00', + 'color_bad': '#FF0000', + } + while True: + print(x.online_status([], config)) + sleep(1) -- cgit v1.3 From 35e1037960e4136af93845743949a78aa4abe4cf Mon Sep 17 00:00:00 2001 From: Ultrabug Date: Wed, 18 Mar 2015 15:02:25 +0100 Subject: remove useless code on online_status module --- py3status/modules/online_status.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'py3status/modules/online_status.py') diff --git a/py3status/modules/online_status.py b/py3status/modules/online_status.py index 432ffb0..8cc6eeb 100644 --- a/py3status/modules/online_status.py +++ b/py3status/modules/online_status.py @@ -38,9 +38,6 @@ class Py3status: return True def online_status(self, i3s_output_list, i3s_config): - self.offline_color = i3s_config['color_bad'] - self.online_color = i3s_config['color_good'] - response = { 'cached_until': time() + self.cache_timeout } @@ -48,10 +45,10 @@ class Py3status: connected = self._connection_present() if connected: response['full_text'] = self.format_online - response['color'] = self.online_color + response['color'] = i3s_config['color_good'] else: response['full_text'] = self.format_offline - response['color'] = self.offline_color + response['color'] = i3s_config['color_bad'] return response -- cgit v1.3