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
|
2019-01-05 19:34:21 +01:00
|
|
|
* regex
|
2018-10-05 17:25:52 +02:00
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* spaceapi.url: String representation of the api endpoint
|
2019-01-05 19:34:21 +01:00
|
|
|
* spaceapi.format: Format string for the output (refer to code)
|
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
|
2019-01-04 15:39:55 +01:00
|
|
|
import threading
|
2019-01-05 19:34:21 +01:00
|
|
|
import re
|
|
|
|
from json.decoder import JSONDecodeError
|
|
|
|
|
|
|
|
|
|
|
|
def formatStringBuilder(s: str, json: dict) -> str:
|
|
|
|
"""
|
|
|
|
This function seems to be in dire need of some explanation so here it is:
|
|
|
|
It basically searches the format string for strings of the pattern
|
|
|
|
%%ITEM.IN.TREE[%IFTRUE%IFFALSE]%%. For example to query the state of
|
2019-01-06 22:57:25 +01:00
|
|
|
the hackspace you'd write %%state.open%% as it's located in json[state][open]
|
2019-01-05 19:34:21 +01:00
|
|
|
according to the API specificaion. As the output of true or false doesn't
|
|
|
|
look to great you can specify the text you want to have shown so you'd
|
|
|
|
write %%state.open%Open%Closed%% to overwrite true/false with Open/Closed.
|
|
|
|
"""
|
|
|
|
identifiers = re.findall("%%.*?%%", s)
|
|
|
|
for i in identifiers:
|
|
|
|
ic = i[2:-2] # Discard %%
|
|
|
|
j = ic.split("%")
|
|
|
|
|
|
|
|
if len(j) != 3 and len(j) != 1:
|
|
|
|
return "INVALID SYNTAX"
|
|
|
|
|
|
|
|
arr = j[0].split(".")
|
|
|
|
repl = json
|
|
|
|
for a in arr: # Walk the JSON tree to find replacement
|
|
|
|
repl = repl[a]
|
|
|
|
|
|
|
|
if len(j) == 1:
|
|
|
|
s = s.replace(i, repl)
|
|
|
|
elif repl:
|
|
|
|
s = s.replace(i, j[1])
|
|
|
|
else:
|
|
|
|
s = s.replace(i, j[2])
|
|
|
|
return s
|
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
|
|
|
|
2019-01-06 22:57:25 +01:00
|
|
|
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE,
|
|
|
|
cmd=self.__forceReload)
|
|
|
|
|
2019-01-04 15:39:55 +01:00
|
|
|
self._data = {}
|
|
|
|
self._error = None
|
|
|
|
|
|
|
|
self._threadingCount = 0
|
|
|
|
|
2018-10-05 21:37:17 +02:00
|
|
|
# The URL representing the api endpoint
|
2019-01-04 15:39:55 +01:00
|
|
|
self._url = self.parameter("url", default="http://club.entropia.de/spaceapi")
|
2019-01-05 19:34:21 +01:00
|
|
|
self._format = self.parameter(
|
|
|
|
"format", default=" %%space%%: %%state.open%Open%Closed%%"
|
|
|
|
)
|
2018-10-05 21:37:17 +02:00
|
|
|
|
2019-01-04 15:39:55 +01:00
|
|
|
def state(self, widget):
|
|
|
|
try:
|
|
|
|
if self._error is not None:
|
|
|
|
return ["critical"]
|
2019-01-05 19:34:21 +01:00
|
|
|
elif self._data["state"]["open"]:
|
2019-01-04 15:39:55 +01:00
|
|
|
return ["warning"]
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
except KeyError:
|
|
|
|
return ["critical"]
|
2018-10-26 00:17:13 +02:00
|
|
|
|
2019-01-04 15:39:55 +01:00
|
|
|
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
|
|
|
|
)
|
2018-10-05 17:25:52 +02:00
|
|
|
|
|
|
|
def getState(self, widget):
|
2019-01-04 15:39:55 +01:00
|
|
|
text = self._format
|
|
|
|
if self._error is not None:
|
|
|
|
text = self._error
|
2018-10-05 17:25:52 +02:00
|
|
|
else:
|
2019-01-04 15:39:55 +01:00
|
|
|
try:
|
2019-01-05 19:34:21 +01:00
|
|
|
text = formatStringBuilder(self._format, self._data)
|
2019-01-04 15:39:55 +01:00
|
|
|
except KeyError:
|
|
|
|
text = "KeyError"
|
2018-10-05 21:37:17 +02:00
|
|
|
return text
|
2018-10-05 17:25:52 +02:00
|
|
|
|
2019-01-04 15:39:55 +01:00
|
|
|
def get_api_async(self):
|
2018-10-05 21:37:17 +02:00
|
|
|
try:
|
2019-01-05 19:34:21 +01:00
|
|
|
with requests.get(self._url, timeout=10) as request:
|
|
|
|
self._data = request.json()
|
2019-01-04 15:39:55 +01:00
|
|
|
self._error = None
|
|
|
|
except requests.exceptions.Timeout:
|
|
|
|
self._error = "Timeout"
|
|
|
|
except requests.exceptions.HTTPError:
|
|
|
|
self._error = "HTTP Error"
|
2019-01-05 19:34:21 +01:00
|
|
|
except JSONDecodeError:
|
|
|
|
self._error = "Not a JSON response"
|
2018-10-05 17:25:52 +02:00
|
|
|
|
2019-01-06 22:57:25 +01:00
|
|
|
# left_mouse_button handler
|
|
|
|
def __forceReload(self, event):
|
|
|
|
self._threadingCount += 300
|
|
|
|
self._error = "RELOADING"
|
2018-10-05 17:25:52 +02:00
|
|
|
|
2019-01-05 19:34:21 +01:00
|
|
|
# Author: Tobias Manske <tobias@chaoswg.xyz>
|
2018-10-05 17:25:52 +02:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|