2020-04-25 16:28:04 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Displays the state of a Space API endpoint
|
|
|
|
Space API is an API for hackspaces based on JSON. See spaceapi.io for
|
|
|
|
an example.
|
|
|
|
|
|
|
|
Requires the following libraries:
|
|
|
|
* requests
|
|
|
|
* regex
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* spaceapi.url: String representation of the api endpoint
|
|
|
|
* spaceapi.format: Format string for the output
|
|
|
|
|
|
|
|
Format Strings:
|
|
|
|
* Format strings are indicated by double %%
|
2020-04-25 16:28:19 +02:00
|
|
|
* They represent a leaf in the JSON tree, layers seperated by '.'
|
|
|
|
* Boolean values can be overwritten by appending '%true%false'
|
2020-04-25 16:28:04 +02:00
|
|
|
in the format string
|
2020-04-25 16:28:19 +02:00
|
|
|
* Example: to reference 'open' in '{'state':{'open': true}}'
|
2020-05-06 12:57:38 +02:00
|
|
|
you would write '%%state.open%%', if you also want
|
|
|
|
to say 'Open/Closed' depending on the boolean you
|
|
|
|
would write '%%state.open%Open%Closed%%'
|
2020-04-25 16:28:04 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import threading
|
|
|
|
import re
|
|
|
|
import json
|
|
|
|
|
2020-04-25 16:35:00 +02:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.input
|
|
|
|
import core.decorators
|
2020-04-25 16:28:04 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-25 16:28:04 +02:00
|
|
|
def formatStringBuilder(s, json):
|
|
|
|
"""
|
|
|
|
Parses Format Strings
|
|
|
|
Parameter:
|
|
|
|
s -> format string
|
|
|
|
json -> the spaceapi response object
|
|
|
|
"""
|
2020-05-03 11:15:52 +02:00
|
|
|
identifiers = re.findall("%%.*?%%", s)
|
2020-04-25 16:28:04 +02:00
|
|
|
for i in identifiers:
|
|
|
|
ic = i[2:-2] # Discard %%
|
2020-05-03 11:15:52 +02:00
|
|
|
j = ic.split("%")
|
2020-04-25 16:28:04 +02:00
|
|
|
|
|
|
|
# Only neither of, or both true AND false may be overwritten
|
|
|
|
if len(j) != 3 and len(j) != 1:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "INVALID FORMAT STRING"
|
2020-04-25 16:28:04 +02:00
|
|
|
|
|
|
|
if len(j) == 1: # no overwrite
|
|
|
|
s = s.replace(i, json[j[0]])
|
|
|
|
elif json[j[0]]: # overwrite for True
|
|
|
|
s = s.replace(i, j[1])
|
|
|
|
else: # overwrite for False
|
|
|
|
s = s.replace(i, j[2])
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
2020-04-25 16:35:00 +02:00
|
|
|
class Module(core.module.Module):
|
|
|
|
@core.decorators.every(minutes=15)
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.getState))
|
2020-04-25 16:28:04 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.__forceReload)
|
2020-04-25 16:28:04 +02:00
|
|
|
|
2020-04-25 16:35:00 +02:00
|
|
|
self.__data = {}
|
|
|
|
self.__error = None
|
|
|
|
self.__thread = None
|
2020-04-25 16:28:04 +02:00
|
|
|
|
|
|
|
# The URL representing the api endpoint
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__url = self.parameter("url", default="http://club.entropia.de/spaceapi")
|
2020-04-25 16:28:04 +02:00
|
|
|
self._format = self.parameter(
|
2020-05-03 11:15:52 +02:00
|
|
|
"format", default=" %%space%%: %%state.open%Open%Closed%%"
|
2020-04-25 16:28:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
try:
|
2020-04-25 16:35:00 +02:00
|
|
|
if self.__error is not None:
|
2020-05-03 11:15:52 +02:00
|
|
|
return ["critical"]
|
|
|
|
elif self.__data["state.open"]:
|
|
|
|
return ["warning"]
|
2020-04-25 16:28:04 +02:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
except KeyError:
|
2020-05-03 11:15:52 +02:00
|
|
|
return ["critical"]
|
2020-04-25 16:28:04 +02:00
|
|
|
|
2020-04-25 16:35:00 +02:00
|
|
|
def update(self):
|
|
|
|
if not self.__thread or self.__thread.is_alive() == False:
|
|
|
|
self.__thread = threading.Thread(target=self.get_api_async, args=())
|
|
|
|
self.__thread.start()
|
2020-04-25 16:28:04 +02:00
|
|
|
|
|
|
|
def getState(self, widget):
|
|
|
|
text = self._format
|
2020-04-25 16:35:00 +02:00
|
|
|
if self.__error is not None:
|
|
|
|
text = self.__error
|
2020-04-25 16:28:04 +02:00
|
|
|
else:
|
|
|
|
try:
|
2020-04-25 16:35:00 +02:00
|
|
|
text = formatStringBuilder(self._format, self.__data)
|
2020-04-25 16:28:04 +02:00
|
|
|
except KeyError:
|
2020-05-03 11:15:52 +02:00
|
|
|
text = "KeyError"
|
2020-04-25 16:28:04 +02:00
|
|
|
return text
|
|
|
|
|
|
|
|
def get_api_async(self):
|
|
|
|
try:
|
2020-04-25 16:35:00 +02:00
|
|
|
with requests.get(self.__url, timeout=10) as request:
|
2020-04-25 16:28:04 +02:00
|
|
|
# Can't implement error handling for python2.7 if I use
|
|
|
|
# request.json() as it uses simplejson in newer versions
|
2020-04-25 16:35:00 +02:00
|
|
|
self.__data = self.__flatten(json.loads(request.text))
|
|
|
|
self.__error = None
|
2020-04-25 16:28:04 +02:00
|
|
|
except requests.exceptions.Timeout:
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__error = "Timeout"
|
2020-04-25 16:28:04 +02:00
|
|
|
except requests.exceptions.HTTPError:
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__error = "HTTP Error"
|
2020-04-25 16:28:04 +02:00
|
|
|
except ValueError:
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__error = "Not a JSON response"
|
|
|
|
core.event.trigger("update", [self.id], redraw_only=True)
|
2020-04-25 16:28:04 +02:00
|
|
|
|
|
|
|
# left_mouse_button handler
|
|
|
|
def __forceReload(self, event):
|
2020-04-25 16:35:00 +02:00
|
|
|
if self.__thread:
|
|
|
|
self.__thread.raise_exception()
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__error = "RELOADING"
|
|
|
|
core.event.trigger("update", [self.id], redraw_only=True)
|
2020-04-25 16:28:04 +02:00
|
|
|
|
2020-04-25 16:28:19 +02:00
|
|
|
# Flattens the JSON structure recursively, e.g. ['space']['open']
|
|
|
|
# becomes ['space.open']
|
2020-04-25 16:28:04 +02:00
|
|
|
def __flatten(self, json):
|
|
|
|
out = {}
|
|
|
|
for key in json:
|
|
|
|
value = json[key]
|
|
|
|
if type(value) is dict:
|
|
|
|
flattened_key = self.__flatten(value)
|
|
|
|
for fk in flattened_key:
|
2020-05-03 11:15:52 +02:00
|
|
|
out[key + "." + fk] = flattened_key[fk]
|
2020-04-25 16:28:04 +02:00
|
|
|
else:
|
|
|
|
out[key] = value
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
# Author: Tobias Manske <tobias@chaoswg.xyz>
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|