summaryrefslogtreecommitdiffstats
path: root/py3status/modules/online_status.py
diff options
context:
space:
mode:
authorrixx <rixx-git@cutebit.de>2015-03-19 15:40:36 +0100
committerrixx <rixx-git@cutebit.de>2015-03-19 15:43:53 +0100
commit102313553232fae442a17d2c6193ee43b5852d22 (patch)
tree9992c9e424d8105c378be4e4452aba8f4fd295a7 /py3status/modules/online_status.py
parent74c855d59f8aacc0b67261d2e5b7ac7402877360 (diff)
parentdbbe4cdc6bf9d428f6fa5e2fb3c7a8dd6198abcd (diff)
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'py3status/modules/online_status.py')
-rw-r--r--py3status/modules/online_status.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/py3status/modules/online_status.py b/py3status/modules/online_status.py
new file mode 100644
index 0000000..8cc6eeb
--- /dev/null
+++ b/py3status/modules/online_status.py
@@ -0,0 +1,67 @@
+# -*- 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):
+ response = {
+ 'cached_until': time() + self.cache_timeout
+ }
+
+ connected = self._connection_present()
+ if connected:
+ response['full_text'] = self.format_online
+ response['color'] = i3s_config['color_good']
+ else:
+ response['full_text'] = self.format_offline
+ response['color'] = i3s_config['color_bad']
+
+ 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)