2018-10-05 17:25:52 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Displays the state of a spaceapi endpoint
|
|
|
|
|
|
|
|
Requires the following libraries:
|
2018-10-05 17:54:43 +02:00
|
|
|
* requests
|
2018-10-05 17:25:52 +02:00
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* spaceapi.url: String representation of the api endpoint
|
|
|
|
* spaceapi.name: String overwriting the space name
|
|
|
|
* spaceapi.prefix: Prefix for the space string
|
2018-10-05 21:37:17 +02:00
|
|
|
* spaceapi.interval: time between updates in minutes
|
2018-10-26 00:17:13 +02:00
|
|
|
* spaceapi.timeout: Maximum time in seconds to wait for a response from API
|
|
|
|
endpoint
|
2018-10-05 17:25:52 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import bumblebee.input
|
|
|
|
import bumblebee.output
|
|
|
|
import bumblebee.engine
|
|
|
|
|
2018-10-05 17:54:43 +02:00
|
|
|
import requests
|
2018-10-05 17:25:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Module(bumblebee.engine.Module):
|
|
|
|
def __init__(self, engine, config):
|
|
|
|
super(Module, self).__init__(
|
|
|
|
engine, config, bumblebee.output.Widget(full_text=self.getState)
|
|
|
|
)
|
2018-10-05 21:37:17 +02:00
|
|
|
|
|
|
|
# Represents the state of the hackerspace
|
|
|
|
self._open = False
|
|
|
|
# Set to true if there was an error calling the spaceapi
|
2018-10-05 17:25:52 +02:00
|
|
|
self._error = False
|
2018-10-05 21:37:17 +02:00
|
|
|
# The URL representing the api endpoint
|
2018-10-05 17:25:52 +02:00
|
|
|
self._url = self.parameter("url",
|
|
|
|
default="http://club.entropia.de/spaceapi")
|
2018-10-05 21:37:17 +02:00
|
|
|
# Space Name, can be set manually in case of multiple widgets,
|
|
|
|
# so you're able to distinguish
|
2018-10-05 17:25:52 +02:00
|
|
|
self._name = self.parameter("name", default="")
|
2018-10-05 21:37:17 +02:00
|
|
|
|
2018-10-26 00:17:13 +02:00
|
|
|
# The timeout prevents the statusbar from blocking when the destination
|
|
|
|
# can't be reached.
|
|
|
|
self._timeout = self.parameter("timeout", default=2)
|
|
|
|
|
2018-10-05 21:37:17 +02:00
|
|
|
# Only execute every 5 minutes by default
|
|
|
|
self.interval(self.parameter("interval", default=5))
|
2018-10-05 17:25:52 +02:00
|
|
|
|
|
|
|
def getState(self, widget):
|
2018-10-05 21:37:17 +02:00
|
|
|
text = self.parameter("prefix", default="")
|
|
|
|
text += self._name + ": "
|
|
|
|
|
2018-10-05 17:25:52 +02:00
|
|
|
if self._error:
|
2018-10-05 21:37:17 +02:00
|
|
|
text += "ERROR"
|
|
|
|
elif self._open:
|
|
|
|
text += "Open"
|
2018-10-05 17:25:52 +02:00
|
|
|
else:
|
2018-10-05 21:37:17 +02:00
|
|
|
text += "Closed"
|
|
|
|
return text
|
2018-10-05 17:25:52 +02:00
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
if self._error:
|
|
|
|
return ["critical"]
|
2018-10-05 21:37:17 +02:00
|
|
|
elif self._open:
|
2018-10-05 17:25:52 +02:00
|
|
|
return ["warning"]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
def update(self, widgets):
|
2018-10-05 21:37:17 +02:00
|
|
|
try:
|
2018-12-24 12:46:08 +01:00
|
|
|
with requests.get(self._url, timeout=self._timeout) as u:
|
2018-10-05 21:37:17 +02:00
|
|
|
json = u.json()
|
|
|
|
self._open = json["state"]["open"]
|
|
|
|
self._name = self.parameter("name", default=json["space"])
|
|
|
|
self._error = False
|
|
|
|
except Exception:
|
|
|
|
# Displays ERROR status
|
|
|
|
self._error = True
|
2018-10-05 17:25:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Author: Tobias Manske <tobias.manske@mailbox.org>
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|