2019-01-07 01:10:06 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-10-05 17:25:52 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
2019-01-07 01:37:10 +01:00
|
|
|
"""Displays the state of a Space API endpoint
|
|
|
|
Space API is an API for hackspaces based on JSON. See spaceapi.io for
|
|
|
|
an example.
|
2018-10-05 17:25:52 +02:00
|
|
|
|
|
|
|
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-07 01:37:10 +01:00
|
|
|
* spaceapi.format: Format string for the output
|
|
|
|
|
|
|
|
Format Strings:
|
|
|
|
* Format strings are indicated by double %%
|
|
|
|
* They represent a leaf in the JSON tree, layers seperated by "."
|
|
|
|
* Boolean values can be overwritten by appending "%true%false"
|
|
|
|
in the format string
|
|
|
|
* Example: to reference "open" in "{"state":{"open": true}}"
|
|
|
|
you would write "%%state.open%%", if you also want
|
|
|
|
to say "Open/Closed" depending on the boolean you
|
|
|
|
would write "%%state.open%Open%Closed%%"
|
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
|
2019-01-07 01:10:06 +01:00
|
|
|
import json
|
2019-01-05 19:34:21 +01:00
|
|
|
|
|
|
|
|
2019-01-07 01:10:06 +01:00
|
|
|
def formatStringBuilder(s, json):
|
2019-01-05 19:34:21 +01:00
|
|
|
"""
|
2019-01-07 01:37:10 +01:00
|
|
|
Parses Format Strings
|
|
|
|
Parameter:
|
|
|
|
s -> format string
|
|
|
|
json -> the spaceapi response object
|
2019-01-05 19:34:21 +01:00
|
|
|
"""
|
|
|
|
identifiers = re.findall("%%.*?%%", s)
|
|
|
|
for i in identifiers:
|
|
|
|
ic = i[2:-2] # Discard %%
|
|
|
|
j = ic.split("%")
|
|
|
|
|
2019-01-07 01:37:10 +01:00
|
|
|
# Only neither of, or both true AND false may be overwritten
|
2019-01-05 19:34:21 +01:00
|
|
|
if len(j) != 3 and len(j) != 1:
|
2019-01-07 01:37:10 +01:00
|
|
|
return "INVALID FORMAT STRING"
|
2019-01-05 19:34:21 +01:00
|
|
|
|
2019-01-07 01:37:10 +01:00
|
|
|
if len(j) == 1: # no overwrite
|
2019-01-17 01:03:11 +01:00
|
|
|
s = s.replace(i, json[j[0]])
|
|
|
|
elif json[j[0]]: # overwrite for True
|
2019-01-05 19:34:21 +01:00
|
|
|
s = s.replace(i, j[1])
|
2019-01-07 01:37:10 +01:00
|
|
|
else: # overwrite for False
|
2019-01-05 19:34:21 +01:00
|
|
|
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-07 01:37:10 +01:00
|
|
|
engine.input.register_callback(
|
|
|
|
self, button=bumblebee.input.LEFT_MOUSE, cmd=self.__forceReload
|
|
|
|
)
|
2019-01-06 22:57:25 +01:00
|
|
|
|
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(
|
2019-01-07 01:10:06 +01:00
|
|
|
"format", default=u" %%space%%: %%state.open%Open%Closed%%"
|
2019-01-05 19:34:21 +01:00
|
|
|
)
|
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-17 01:03:11 +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:
|
2019-01-07 01:10:06 +01:00
|
|
|
# Can't implement error handling for python2.7 if I use
|
|
|
|
# request.json() as it uses simplejson in newer versions
|
2019-01-17 01:03:11 +01:00
|
|
|
self._data = self.__flatten(json.loads(request.text))
|
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-07 01:10:06 +01:00
|
|
|
except ValueError:
|
2019-01-05 19:34:21 +01:00
|
|
|
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-17 01:03:11 +01:00
|
|
|
# Flattens the JSON structure recursively, e.g. ["space"]["open"]
|
|
|
|
# becomes ["space.open"]
|
|
|
|
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:
|
|
|
|
out[key + "." + fk] = flattened_key[fk]
|
|
|
|
else:
|
|
|
|
out[key] = value
|
|
|
|
return out
|
|
|
|
|
2019-01-07 01:37:10 +01: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
|