2016-10-31 07:18:57 +01:00
|
|
|
import os
|
2016-11-25 21:06:24 +01:00
|
|
|
import copy
|
2016-10-31 07:18:57 +01:00
|
|
|
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():
|
2016-11-25 17:57:41 +01:00
|
|
|
return os.path.dirname("{}/../themes/".format(os.path.dirname(os.path.realpath(__file__))))
|
2016-10-31 16:47:34 +01:00
|
|
|
|
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
|
|
|
|
2016-11-25 21:06:24 +01:00
|
|
|
self._data = self.get_theme(config.theme())
|
|
|
|
|
|
|
|
for iconset in self._data.get("icons", []):
|
|
|
|
self.merge(self._data, self.get_theme(iconset))
|
|
|
|
|
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-25 21:06:24 +01:00
|
|
|
def get_theme(self, name):
|
|
|
|
for path in [ getpath(), "{}/icons/".format(getpath()) ]:
|
|
|
|
if os.path.isfile("{}/{}.yaml".format(path, name)):
|
|
|
|
with open("{}/{}.yaml".format(path, name)) as f:
|
|
|
|
return yaml.load(f)
|
|
|
|
if os.path.isfile("{}/{}.json".format(path, name)):
|
|
|
|
with open("{}/{}.json".format(path, name)) as f:
|
|
|
|
return json.load(f)
|
|
|
|
return None
|
|
|
|
|
|
|
|
# algorithm copied from
|
|
|
|
# http://blog.impressiver.com/post/31434674390/deep-merge-multiple-python-dicts
|
|
|
|
# nicely done :)
|
|
|
|
def merge(self, target, *args):
|
|
|
|
if len(args) > 1:
|
|
|
|
for item in args:
|
|
|
|
self.merge(item)
|
|
|
|
return target
|
|
|
|
|
|
|
|
item = args[0]
|
|
|
|
if not isinstance(item, dict):
|
|
|
|
return item
|
2016-11-30 18:30:01 +01:00
|
|
|
for key, value in item.items():
|
2016-11-25 21:06:24 +01:00
|
|
|
if key in target and isinstance(target[key], dict):
|
|
|
|
self.merge(target[key], value)
|
|
|
|
else:
|
|
|
|
target[key] = copy.deepcopy(value)
|
|
|
|
return target
|
|
|
|
|
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-30 18:30:01 +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-30 18:30:01 +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")
|
|
|
|
|
2016-12-02 17:52:05 +01:00
|
|
|
def default_separators(self, widget, default):
|
|
|
|
return self._get(widget, "default-separators", default)
|
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-12-02 17:52:05 +01:00
|
|
|
def separator_block_width(self, widget, default):
|
|
|
|
return self._get(widget, "separator-block-width", default)
|
2016-10-31 11:35:12 +01:00
|
|
|
|
2016-11-30 18:30:01 +01:00
|
|
|
def _get(self, widget, name, default = None):
|
2016-11-05 08:59:43 +01:00
|
|
|
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-30 18:30:01 +01:00
|
|
|
return value if value else default
|
2016-10-31 07:39:26 +01:00
|
|
|
|
2016-10-31 07:18:57 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|