2016-12-09 11:57:13 +01:00
|
|
|
# pylint: disable=C0103
|
|
|
|
|
2016-12-04 18:10:04 +01:00
|
|
|
"""Theme support"""
|
|
|
|
|
2016-12-08 09:44:05 +01:00
|
|
|
import os
|
2016-12-17 07:05:23 +01:00
|
|
|
import glob
|
2016-12-08 12:09:21 +01:00
|
|
|
import copy
|
2016-12-08 09:44:05 +01:00
|
|
|
import json
|
2017-05-13 16:41:43 +02:00
|
|
|
import io
|
2016-12-08 09:44:05 +01:00
|
|
|
|
|
|
|
import bumblebee.error
|
|
|
|
|
|
|
|
def theme_path():
|
|
|
|
"""Return the path of the theme directory"""
|
|
|
|
return os.path.dirname("{}/../themes/".format(os.path.dirname(os.path.realpath(__file__))))
|
|
|
|
|
2016-12-17 07:05:23 +01:00
|
|
|
def themes():
|
|
|
|
result = []
|
|
|
|
|
|
|
|
for filename in glob.iglob("{}/*.json".format(theme_path())):
|
|
|
|
if "test" not in filename:
|
|
|
|
result.append(os.path.basename(filename).replace(".json", ""))
|
|
|
|
return result
|
|
|
|
|
2016-12-04 18:10:04 +01:00
|
|
|
class Theme(object):
|
2016-12-08 08:44:54 +01:00
|
|
|
"""Represents a collection of icons and colors"""
|
2016-12-08 09:44:05 +01:00
|
|
|
def __init__(self, name):
|
2016-12-09 12:28:39 +01:00
|
|
|
self._widget = None
|
2016-12-09 16:33:29 +01:00
|
|
|
self._cycle_idx = 0
|
|
|
|
self._cycle = {}
|
|
|
|
self._prevbg = None
|
2017-10-08 08:13:10 +02:00
|
|
|
self._colorset = {}
|
|
|
|
self._init(self.load(name))
|
2016-12-08 09:44:05 +01:00
|
|
|
|
2016-12-08 11:52:47 +01:00
|
|
|
def _init(self, data):
|
|
|
|
"""Initialize theme from data structure"""
|
2017-09-20 08:59:23 +02:00
|
|
|
self._theme = data
|
2016-12-08 12:09:21 +01:00
|
|
|
for iconset in data.get("icons", []):
|
|
|
|
self._merge(data, self._load_icons(iconset))
|
2017-10-08 08:13:10 +02:00
|
|
|
for colorset in data.get("colors", []):
|
|
|
|
self._merge(self._colorset, self._load_colors(colorset))
|
2016-12-08 11:52:47 +01:00
|
|
|
self._defaults = data.get("defaults", {})
|
2016-12-09 12:28:39 +01:00
|
|
|
self._cycles = self._theme.get("cycle", [])
|
|
|
|
self.reset()
|
|
|
|
|
|
|
|
def data(self):
|
|
|
|
"""Return the raw theme data"""
|
|
|
|
return self._theme
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
"""Reset theme to initial state"""
|
|
|
|
self._cycle = self._cycles[0] if len(self._cycles) > 0 else {}
|
|
|
|
self._cycle_idx = 0
|
|
|
|
self._widget = None
|
2016-12-09 12:55:16 +01:00
|
|
|
self._prevbg = None
|
2016-12-08 11:52:47 +01:00
|
|
|
|
2016-12-09 13:06:08 +01:00
|
|
|
def padding(self, widget):
|
|
|
|
"""Return padding for widget"""
|
|
|
|
return self._get(widget, "padding", "")
|
|
|
|
|
|
|
|
def prefix(self, widget, default=None):
|
2016-12-08 11:31:20 +01:00
|
|
|
"""Return the theme prefix for a widget's full text"""
|
2016-12-09 13:06:08 +01:00
|
|
|
padding = self.padding(widget)
|
2016-12-09 12:28:39 +01:00
|
|
|
pre = self._get(widget, "prefix", None)
|
2016-12-09 13:06:08 +01:00
|
|
|
return u"{}{}{}".format(padding, pre, padding) if pre else default
|
2016-12-08 11:31:20 +01:00
|
|
|
|
2016-12-09 13:06:08 +01:00
|
|
|
def suffix(self, widget, default=None):
|
2016-12-08 11:31:20 +01:00
|
|
|
"""Return the theme suffix for a widget's full text"""
|
2016-12-09 12:28:39 +01:00
|
|
|
padding = self._get(widget, "padding", "")
|
|
|
|
suf = self._get(widget, "suffix", None)
|
2016-12-09 13:06:08 +01:00
|
|
|
return u"{}{}{}".format(padding, suf, padding) if suf else default
|
2016-12-08 11:52:47 +01:00
|
|
|
|
2016-12-09 08:58:45 +01:00
|
|
|
def fg(self, widget):
|
|
|
|
"""Return the foreground color for this widget"""
|
|
|
|
return self._get(widget, "fg", None)
|
|
|
|
|
|
|
|
def bg(self, widget):
|
|
|
|
"""Return the background color for this widget"""
|
|
|
|
return self._get(widget, "bg", None)
|
|
|
|
|
2017-04-22 08:24:52 +02:00
|
|
|
def align(self, widget):
|
|
|
|
"""Return the widget alignment"""
|
|
|
|
return self._get(widget, "align", None)
|
|
|
|
|
|
|
|
def minwidth(self, widget):
|
|
|
|
"""Return the minimum width string for this widget"""
|
|
|
|
return self._get(widget, "minwidth", "")
|
|
|
|
|
2016-12-09 12:55:16 +01:00
|
|
|
def separator(self, widget):
|
|
|
|
"""Return the separator between widgets"""
|
|
|
|
return self._get(widget, "separator", None)
|
|
|
|
|
|
|
|
def separator_fg(self, widget):
|
2016-12-09 16:33:29 +01:00
|
|
|
"""Return the separator's foreground/text color"""
|
2016-12-09 12:55:16 +01:00
|
|
|
return self.bg(widget)
|
|
|
|
|
|
|
|
def separator_bg(self, widget):
|
2016-12-09 16:33:29 +01:00
|
|
|
"""Return the separator's background color"""
|
2016-12-09 12:55:16 +01:00
|
|
|
return self._prevbg
|
|
|
|
|
|
|
|
def separator_block_width(self, widget):
|
|
|
|
"""Return the SBW"""
|
|
|
|
return self._get(widget, "separator-block-width", None)
|
|
|
|
|
2017-10-08 08:13:10 +02:00
|
|
|
def _load_wal_colors(self):
|
|
|
|
walfile = os.path.expanduser("~/.cache/wal/colors.json")
|
|
|
|
result = {}
|
|
|
|
with io.open(walfile) as data:
|
|
|
|
colors = json.load(data)
|
|
|
|
for field in ["special", "colors"]:
|
|
|
|
for key in colors[field]:
|
|
|
|
result[key] = colors[field][key]
|
|
|
|
return result
|
|
|
|
|
|
|
|
def _load_colors(self, name):
|
|
|
|
"""Load colors for a theme"""
|
|
|
|
if name == "wal":
|
|
|
|
return self._load_wal_colors()
|
|
|
|
|
2016-12-08 12:09:21 +01:00
|
|
|
def _load_icons(self, name):
|
2016-12-09 07:11:23 +01:00
|
|
|
"""Load icons for a theme"""
|
2016-12-08 12:09:21 +01:00
|
|
|
path = "{}/icons/".format(theme_path())
|
|
|
|
return self.load(name, path=path)
|
|
|
|
|
|
|
|
def load(self, name, path=theme_path()):
|
2016-12-08 09:44:05 +01:00
|
|
|
"""Load and parse a theme file"""
|
|
|
|
themefile = "{}/{}.json".format(path, name)
|
2016-12-08 12:09:21 +01:00
|
|
|
|
2016-12-08 09:44:05 +01:00
|
|
|
if os.path.isfile(themefile):
|
|
|
|
try:
|
2017-10-08 08:13:10 +02:00
|
|
|
with io.open(themefile, encoding="utf-8") as data:
|
2017-05-13 16:41:43 +02:00
|
|
|
return json.load(data)
|
2016-12-08 09:44:05 +01:00
|
|
|
except ValueError as exception:
|
|
|
|
raise bumblebee.error.ThemeLoadError("JSON error: {}".format(exception))
|
|
|
|
else:
|
|
|
|
raise bumblebee.error.ThemeLoadError("no such theme: {}".format(name))
|
2016-12-04 18:10:04 +01:00
|
|
|
|
2016-12-08 12:09:21 +01:00
|
|
|
def _get(self, widget, name, default=None):
|
2016-12-09 07:11:23 +01:00
|
|
|
"""Return the config value 'name' for 'widget'"""
|
2016-12-08 12:09:21 +01:00
|
|
|
|
2016-12-09 12:28:39 +01:00
|
|
|
if not self._widget:
|
|
|
|
self._widget = widget
|
|
|
|
|
|
|
|
if self._widget != widget:
|
2016-12-09 12:55:16 +01:00
|
|
|
self._prevbg = self.bg(self._widget)
|
2016-12-09 12:28:39 +01:00
|
|
|
self._widget = widget
|
2016-12-09 12:55:16 +01:00
|
|
|
if len(self._cycles) > 0:
|
|
|
|
self._cycle_idx = (self._cycle_idx + 1) % len(self._cycles)
|
|
|
|
self._cycle = self._cycles[self._cycle_idx]
|
2016-12-09 12:28:39 +01:00
|
|
|
|
|
|
|
module_theme = self._theme.get(widget.module, {})
|
2017-05-10 20:01:29 +02:00
|
|
|
class_theme = self._theme.get(widget.cls(), {})
|
2016-12-10 11:25:02 +01:00
|
|
|
|
|
|
|
state_themes = []
|
|
|
|
# avoid infinite recursion
|
2016-12-11 07:28:15 +01:00
|
|
|
states = widget.state()
|
|
|
|
if name not in states:
|
|
|
|
for state in states:
|
2016-12-10 11:25:02 +01:00
|
|
|
state_themes.append(self._get(widget, state, {}))
|
2016-12-08 12:44:52 +01:00
|
|
|
|
2016-12-08 12:09:21 +01:00
|
|
|
value = self._defaults.get(name, default)
|
2017-04-22 13:07:50 +02:00
|
|
|
value = widget.get("theme.{}".format(name), value)
|
2016-12-09 12:28:39 +01:00
|
|
|
value = self._cycle.get(name, value)
|
2017-05-10 20:01:29 +02:00
|
|
|
value = class_theme.get(name, value)
|
2016-12-08 12:09:21 +01:00
|
|
|
value = module_theme.get(name, value)
|
2016-12-10 11:25:02 +01:00
|
|
|
|
|
|
|
for theme in state_themes:
|
|
|
|
value = theme.get(name, value)
|
2016-12-08 11:52:47 +01:00
|
|
|
|
2016-12-10 12:00:08 +01:00
|
|
|
if isinstance(value, list):
|
|
|
|
key = "{}-idx".format(name)
|
|
|
|
idx = widget.get(key, 0)
|
|
|
|
widget.set(key, (idx + 1) % len(value))
|
|
|
|
value = value[idx]
|
|
|
|
|
2017-10-08 08:13:10 +02:00
|
|
|
if isinstance(value, list) or isinstance(value, dict):
|
|
|
|
return value
|
|
|
|
return self._colorset.get(value, value)
|
2016-12-08 11:52:47 +01:00
|
|
|
|
2016-12-08 12:09:21 +01:00
|
|
|
# algorithm copied from
|
|
|
|
# http://blog.impressiver.com/post/31434674390/deep-merge-multiple-python-dicts
|
|
|
|
# nicely done :)
|
|
|
|
def _merge(self, target, *args):
|
2016-12-09 07:11:23 +01:00
|
|
|
"""Merge two arbitrarily nested data structures"""
|
2016-12-08 12:09:21 +01:00
|
|
|
if len(args) > 1:
|
|
|
|
for item in args:
|
|
|
|
self._merge(item)
|
|
|
|
return target
|
|
|
|
|
|
|
|
item = args[0]
|
|
|
|
if not isinstance(item, dict):
|
|
|
|
return item
|
|
|
|
for key, value in item.items():
|
|
|
|
if key in target and isinstance(target[key], dict):
|
|
|
|
self._merge(target[key], value)
|
|
|
|
else:
|
2017-09-20 08:59:23 +02:00
|
|
|
if not key in target:
|
|
|
|
target[key] = copy.deepcopy(value)
|
2016-12-08 12:09:21 +01:00
|
|
|
return target
|
|
|
|
|
2016-12-04 18:10:04 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|