From f53cd062ae8c25929837a205679dc5e2c4e19809 Mon Sep 17 00:00:00 2001 From: Tobias Manske Date: Thu, 17 Jan 2019 01:03:11 +0100 Subject: [PATCH] [modules/spaceapi] Performance optimization - JSON The module now flattens the JSON structure when it is received from the API endpoint instead of every time the statusbar is updated. This should make the module much more performant. Signed-off-by: Tobias Manske --- bumblebee/modules/spaceapi.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/bumblebee/modules/spaceapi.py b/bumblebee/modules/spaceapi.py index 10b0bdc..42ff495 100644 --- a/bumblebee/modules/spaceapi.py +++ b/bumblebee/modules/spaceapi.py @@ -52,14 +52,9 @@ def formatStringBuilder(s, json): if len(j) != 3 and len(j) != 1: return "INVALID FORMAT STRING" - arr = j[0].split(".") - repl = json - for a in arr: # Walk the JSON tree to find replacement string - repl = repl[a] - if len(j) == 1: # no overwrite - s = s.replace(i, repl) - elif repl: # overwrite for Trfor True + 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]) @@ -91,7 +86,7 @@ class Module(bumblebee.engine.Module): try: if self._error is not None: return ["critical"] - elif self._data["state"]["open"]: + elif self._data["state.open"]: return ["warning"] else: return [] @@ -122,7 +117,7 @@ class Module(bumblebee.engine.Module): with requests.get(self._url, timeout=10) as request: # Can't implement error handling for python2.7 if I use # request.json() as it uses simplejson in newer versions - self._data = json.loads(request.text) + self._data = self.__flatten(json.loads(request.text)) self._error = None except requests.exceptions.Timeout: self._error = "Timeout" @@ -136,6 +131,20 @@ class Module(bumblebee.engine.Module): self._threadingCount += 300 self._error = "RELOADING" + # 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 + # Author: Tobias Manske # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4