2016-10-31 07:18:57 +01:00
|
|
|
import os
|
|
|
|
import json
|
2016-11-05 16:53:18 +01:00
|
|
|
import yaml
|
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-11-05 08:11:08 +01:00
|
|
|
def __init__(self, config):
|
2016-11-05 11:57:09 +01:00
|
|
|
self._config = config
|
2016-11-05 16:53:18 +01:00
|
|
|
|
|
|
|
if os.path.isfile("{}/{}.yaml".format(getpath(), config.theme())):
|
|
|
|
with open("{}/{}.yaml".format(getpath(), config.theme())) as f:
|
|
|
|
self._data = yaml.load(f)
|
|
|
|
else:
|
|
|
|
with open("{}/{}.json".format(getpath(), config.theme())) as f:
|
|
|
|
self._data = json.load(f)
|
2016-10-31 07:18:57 +01:00
|
|
|
self._defaults = self._data.get("defaults", {})
|
2016-11-05 11:40:27 +01:00
|
|
|
self._cycles = self._defaults.get("cycle", [])
|
|
|
|
self.begin()
|
2016-10-31 10:45:15 +01:00
|
|
|
|
2016-11-05 08:59:43 +01:00
|
|
|
def begin(self):
|
2016-11-05 12:28:05 +01:00
|
|
|
self._config.set("theme.cycleidx", 0)
|
2016-11-05 11:40:27 +01:00
|
|
|
self._cycle = self._cycles[0] if len(self._cycles) > 0 else {}
|
|
|
|
self._background = [ None, None ]
|
2016-10-31 07:18:57 +01:00
|
|
|
|
2016-11-05 08:59:43 +01:00
|
|
|
def next_widget(self):
|
2016-11-05 11:40:27 +01:00
|
|
|
self._background[1] = self._background[0]
|
2016-11-05 11:57:09 +01:00
|
|
|
idx = self._config.increase("theme.cycleidx", len(self._cycles), 0)
|
|
|
|
self._cycle = self._cycles[idx] if len(self._cycles) > idx else {}
|
2016-10-31 10:45:15 +01:00
|
|
|
|
2016-11-05 08:59:43 +01:00
|
|
|
def prefix(self, widget):
|
2016-11-05 11:40:27 +01:00
|
|
|
return self._get(widget, "prefix")
|
2016-10-31 07:34:43 +01:00
|
|
|
|
2016-11-05 08:59:43 +01:00
|
|
|
def suffix(self, widget):
|
2016-11-05 11:40:27 +01:00
|
|
|
return self._get(widget, "suffix")
|
2016-10-31 11:46:07 +01:00
|
|
|
|
2016-11-05 08:59:43 +01:00
|
|
|
def color(self, widget):
|
|
|
|
result = self._get(widget, "fg")
|
|
|
|
if widget.warning():
|
|
|
|
result = self._get(widget, "fg-warning")
|
|
|
|
if widget.critical():
|
|
|
|
result = self._get(widget, "fg-critical")
|
|
|
|
return result
|
2016-10-31 07:18:57 +01:00
|
|
|
|
2016-11-05 08:59:43 +01:00
|
|
|
def background(self, widget):
|
2016-11-05 11:40:27 +01:00
|
|
|
result = self._get(widget, "bg")
|
|
|
|
if widget.warning():
|
|
|
|
result = self._get(widget, "bg-warning")
|
|
|
|
if widget.critical():
|
|
|
|
result = self._get(widget, "bg-critical")
|
|
|
|
self._background[0] = result
|
|
|
|
return result
|
2016-10-31 10:45:15 +01:00
|
|
|
|
2016-11-05 08:59:43 +01:00
|
|
|
def separator(self, widget):
|
2016-11-05 11:40:27 +01:00
|
|
|
return self._get(widget, "separator")
|
|
|
|
|
|
|
|
def default_separators(self, widget):
|
|
|
|
return self._get(widget, "default-separators")
|
2016-10-31 11:35:12 +01:00
|
|
|
|
2016-11-05 08:59:43 +01:00
|
|
|
def separator_color(self, widget):
|
2016-11-05 11:40:27 +01:00
|
|
|
return self.background(widget)
|
2016-10-31 10:45:15 +01:00
|
|
|
|
2016-11-05 08:59:43 +01:00
|
|
|
def separator_background(self, widget):
|
2016-11-05 11:40:27 +01:00
|
|
|
return self._background[1]
|
2016-10-31 11:35:12 +01:00
|
|
|
|
2016-11-05 08:59:43 +01:00
|
|
|
def separator_block_width(self, widget):
|
2016-11-05 12:28:05 +01:00
|
|
|
return self._get(widget, "separator-block-width")
|
2016-10-31 11:35:12 +01:00
|
|
|
|
2016-11-05 08:59:43 +01:00
|
|
|
def _get(self, widget, name):
|
|
|
|
module = widget.module()
|
|
|
|
state = widget.state()
|
2016-11-12 12:11:42 +01:00
|
|
|
inst = widget.instance()
|
|
|
|
inst = inst.replace("{}.".format(module), "")
|
2016-11-05 08:59:43 +01:00
|
|
|
module_theme = self._data.get(module, {})
|
|
|
|
state_theme = module_theme.get("states", {}).get(state, {})
|
2016-11-12 12:11:42 +01:00
|
|
|
instance_theme = module_theme.get(inst, {})
|
|
|
|
instance_state_theme = instance_theme.get("states", {}).get(state, {})
|
2016-10-31 10:45:15 +01:00
|
|
|
|
2016-11-05 08:59:43 +01:00
|
|
|
value = None
|
|
|
|
value = self._defaults.get(name, value)
|
|
|
|
value = self._cycle.get(name, value)
|
|
|
|
value = module_theme.get(name, value)
|
|
|
|
value = state_theme.get(name, value)
|
2016-11-12 12:11:42 +01:00
|
|
|
value = instance_theme.get(name, value)
|
|
|
|
value = instance_state_theme.get(name, value)
|
2016-10-31 07:54:18 +01:00
|
|
|
|
2016-11-05 08:59:43 +01:00
|
|
|
if type(value) is list:
|
2016-11-05 11:57:09 +01:00
|
|
|
key = "{}{}".format(repr(widget), value)
|
2016-11-05 12:18:30 +01:00
|
|
|
idx = self._config.parameter(key, 0)
|
2016-11-05 11:57:09 +01:00
|
|
|
self._config.increase(key, len(value), 0)
|
|
|
|
value = value[idx]
|
2016-10-31 08:14:12 +01:00
|
|
|
|
2016-11-05 08:59:43 +01:00
|
|
|
return value
|
2016-10-31 07:39:26 +01:00
|
|
|
|
2016-10-31 07:18:57 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|