2016-10-31 07:18:57 +01:00
|
|
|
import os
|
|
|
|
import json
|
2016-11-05 07:54:36 +01:00
|
|
|
import glob
|
2016-10-31 07:18:57 +01:00
|
|
|
|
2016-10-31 16:47:34 +01:00
|
|
|
def getpath():
|
|
|
|
return os.path.dirname("{}/themes/".format(os.path.dirname(os.path.realpath(__file__))))
|
|
|
|
|
2016-11-05 07:54:36 +01:00
|
|
|
def themes():
|
|
|
|
d = getpath()
|
|
|
|
return [ os.path.basename(f).replace(".json", "") for f in glob.iglob("{}/*.json".format(d)) ]
|
|
|
|
|
2016-10-31 07:18:57 +01:00
|
|
|
class Theme:
|
2016-10-31 10:45:15 +01:00
|
|
|
_cycle_index = 0
|
|
|
|
_cycle = None
|
2016-11-05 08:11:08 +01:00
|
|
|
def __init__(self, config):
|
2016-10-31 07:18:57 +01:00
|
|
|
self._data = None
|
|
|
|
path = os.path.dirname(os.path.realpath(__file__))
|
2016-11-05 08:11:08 +01:00
|
|
|
with open("{}/{}.json".format(getpath(), config.theme())) as f:
|
2016-10-31 07:18:57 +01:00
|
|
|
self._data = json.load(f)
|
|
|
|
self._defaults = self._data.get("defaults", {})
|
2016-10-31 10:45:15 +01:00
|
|
|
self._cycle = self._defaults.get("cycle", [])
|
2016-10-31 11:46:07 +01:00
|
|
|
self._modules = {}
|
2016-10-31 10:45:15 +01:00
|
|
|
|
|
|
|
self.reset()
|
2016-10-31 07:18:57 +01:00
|
|
|
|
|
|
|
def _gettheme(self, obj, key):
|
|
|
|
module = obj.__module__.split(".")[-1]
|
|
|
|
module_theme = self._data.get(module, {})
|
|
|
|
|
2016-10-31 07:54:18 +01:00
|
|
|
value = getattr(obj, key)() if hasattr(obj, key) else None
|
2016-10-31 07:18:57 +01:00
|
|
|
value = self._defaults.get(key, value)
|
2016-10-31 10:45:15 +01:00
|
|
|
if len(self._cycle) > 0:
|
|
|
|
value = self._defaults["cycle"][self._cycle_index].get(key, value)
|
2016-10-31 11:05:54 +01:00
|
|
|
value = module_theme.get(key, value)
|
2016-10-31 10:45:15 +01:00
|
|
|
|
2016-10-31 07:34:43 +01:00
|
|
|
if hasattr(obj, "state"):
|
|
|
|
state = getattr(obj, "state")()
|
|
|
|
state_theme = module_theme.get("states", {}).get(state, {})
|
|
|
|
|
|
|
|
value = state_theme.get(key, value)
|
|
|
|
|
2016-10-31 11:46:07 +01:00
|
|
|
if type(value) is list:
|
|
|
|
# cycle through the values
|
|
|
|
if not obj in self._modules:
|
|
|
|
self._modules[obj] = { "idx": 0 }
|
|
|
|
else:
|
|
|
|
self._modules[obj]["idx"] += 1
|
|
|
|
if self._modules[obj]["idx"] >= len(value):
|
|
|
|
self._modules[obj]["idx"] = 0
|
|
|
|
value = value[self._modules[obj]["idx"]]
|
|
|
|
|
2016-10-31 07:18:57 +01:00
|
|
|
return value
|
|
|
|
|
2016-10-31 10:45:15 +01:00
|
|
|
def reset(self):
|
|
|
|
self._cycle_index = 0
|
|
|
|
self._previous_background = None
|
|
|
|
self._background = None
|
|
|
|
|
2016-10-31 11:35:12 +01:00
|
|
|
def urgent(self, obj):
|
|
|
|
self._gettheme(obj, "urgent")
|
|
|
|
|
2016-10-31 10:45:15 +01:00
|
|
|
def next(self):
|
|
|
|
self._cycle_index += 1
|
|
|
|
self._previous_background = self._background
|
|
|
|
if self._cycle_index >= len(self._cycle):
|
|
|
|
self._cycle_index = 0
|
|
|
|
|
2016-10-31 07:54:18 +01:00
|
|
|
def color(self, obj):
|
2016-10-31 11:35:12 +01:00
|
|
|
fg = None
|
|
|
|
if obj.warning():
|
|
|
|
fg = self._gettheme(obj, "fg-warning")
|
|
|
|
if obj.critical():
|
|
|
|
fg = self._gettheme(obj, "fg-critical")
|
|
|
|
if fg == None:
|
|
|
|
fg = self._gettheme(obj, "fg")
|
|
|
|
return fg
|
2016-10-31 07:54:18 +01:00
|
|
|
|
|
|
|
def background(self, obj):
|
2016-10-31 11:35:12 +01:00
|
|
|
self._background = None
|
|
|
|
if obj.warning():
|
|
|
|
self._background = self._gettheme(obj, "bg-warning")
|
|
|
|
if obj.critical():
|
|
|
|
self._background = self._gettheme(obj, "bg-critical")
|
|
|
|
|
|
|
|
if self._background == None:
|
|
|
|
self._background = self._gettheme(obj, "bg")
|
|
|
|
|
2016-10-31 10:45:15 +01:00
|
|
|
return self._background
|
|
|
|
|
|
|
|
def previous_background(self):
|
|
|
|
return self._previous_background
|
2016-10-31 07:54:18 +01:00
|
|
|
|
2016-10-31 08:14:12 +01:00
|
|
|
def separator(self, obj):
|
|
|
|
return self._gettheme(obj, "separator")
|
|
|
|
|
2016-10-31 07:39:26 +01:00
|
|
|
def default_separators(self, obj):
|
|
|
|
return self._gettheme(obj, "default_separators")
|
|
|
|
|
2016-10-31 07:18:57 +01:00
|
|
|
def prefix(self, obj):
|
|
|
|
return self._gettheme(obj, "prefix")
|
|
|
|
|
|
|
|
def suffix(self, obj):
|
|
|
|
return self._gettheme(obj, "suffix")
|
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|