bumblebee-status/bumblebee/theme.py
Tobias Witek 97273b5a41 [themes] Add state-dependent themeing
Module themes (only!) can now contain state-specific theme information -
for example, the "battery" module has different states for charging and
discharging, and those can have different prefix and postfix
configurations to indicate what is going on.
2016-10-31 07:34:43 +01:00

34 lines
1 KiB
Python

import os
import json
class Theme:
def __init__(self, name="default"):
self._data = None
path = os.path.dirname(os.path.realpath(__file__))
with open("%s/themes/%s.json" % (path, name)) as f:
self._data = json.load(f)
self._defaults = self._data.get("defaults", {})
def _gettheme(self, obj, key):
module = obj.__module__.split(".")[-1]
module_theme = self._data.get(module, {})
value = getattr(obj, key)() if hasattr(obj, key) else ""
value = self._defaults.get(key, value)
value = module_theme.get(key, value)
if hasattr(obj, "state"):
state = getattr(obj, "state")()
state_theme = module_theme.get("states", {}).get(state, {})
value = state_theme.get(key, value)
return value
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