[modules/spaceapi] rewrote module to use threading

Also I'm not catching every exception anymore.

Signed-off-by: Tobias Manske <tobias.manske@mailbox.org>
This commit is contained in:
Tobias Manske 2019-01-04 15:39:55 +01:00
parent 5203cd88a3
commit 0be81ec1f7
No known key found for this signature in database
GPG key ID: 978D99F12D4E041F

View file

@ -7,11 +7,7 @@ Requires the following libraries:
Parameters: Parameters:
* spaceapi.url: String representation of the api endpoint * spaceapi.url: String representation of the api endpoint
* spaceapi.name: String overwriting the space name * spaceapi.format: Format string for the output
* spaceapi.prefix: Prefix for the space string
* spaceapi.interval: time between updates in minutes
* spaceapi.timeout: Maximum time in seconds to wait for a response from API
endpoint
""" """
import bumblebee.input import bumblebee.input
@ -19,6 +15,8 @@ import bumblebee.output
import bumblebee.engine import bumblebee.engine
import requests import requests
import threading
import sys
class Module(bumblebee.engine.Module): class Module(bumblebee.engine.Module):
@ -27,54 +25,60 @@ class Module(bumblebee.engine.Module):
engine, config, bumblebee.output.Widget(full_text=self.getState) engine, config, bumblebee.output.Widget(full_text=self.getState)
) )
# Represents the state of the hackerspace self._data = {}
self._open = False self._error = None
# Set to true if there was an error calling the spaceapi
self._error = False self._threadingCount = 0
# The URL representing the api endpoint # The URL representing the api endpoint
self._url = self.parameter("url", self._url = self.parameter("url", default="http://club.entropia.de/spaceapi")
default="http://club.entropia.de/spaceapi") self._format = self.parameter("format", default="%%space%%: %%state%%")
# Space Name, can be set manually in case of multiple widgets,
# so you're able to distinguish
self._name = self.parameter("name", default="")
# The timeout prevents the statusbar from blocking when the destination
# can't be reached.
self._timeout = self.parameter("timeout", default=2)
# Only execute every 5 minutes by default
self.interval(self.parameter("interval", default=5))
def getState(self, widget):
text = self.parameter("prefix", default="")
text += self._name + ": "
if self._error:
text += "ERROR"
elif self._open:
text += "Open"
else:
text += "Closed"
return text
def state(self, widget): def state(self, widget):
if self._error: try:
if self._error is not None:
return ["critical"] return ["critical"]
elif self._open: elif self._data['state']['open']:
return ["warning"] return ["warning"]
else: else:
return [] return []
except KeyError:
return ["critical"]
def update(self, widgets): def update(self, widgets):
if self._threadingCount == 0:
thread = threading.Thread(target=self.get_api_async, args=())
thread.start()
self._threadingCount = (
0 if self._threadingCount > 300 else self._threadingCount + 1
)
def getState(self, widget):
text = self._format
if self._error is not None:
text = self._error
else:
try: try:
with requests.get(self._url, timeout=self._timeout) as u: text = text.replace("%%space%%", self._data['space'])
json = u.json() if self._data['state']['open']:
self._open = json["state"]["open"] text = text.replace("%%state%%", "Open")
self._name = self.parameter("name", default=json["space"]) else:
self._error = False text = text.replace("%%state%%", "Closed")
except Exception: except KeyError:
# Displays ERROR status text = "KeyError"
self._error = True return text
def get_api_async(self):
try:
with requests.get(self._url, timeout=10) as u:
self._data = u.json()
self._error = None
except requests.exceptions.Timeout:
self._error = "Timeout"
except requests.exceptions.HTTPError:
self._error = "HTTP Error"
# except Exception:
# self._error = "CRITICAL ERROR"
# Author: Tobias Manske <tobias.manske@mailbox.org> # Author: Tobias Manske <tobias.manske@mailbox.org>