summaryrefslogtreecommitdiff
path: root/alot/settings/theme.py
diff options
context:
space:
mode:
authorPatrick Totzke <patricktotzke@gmail.com>2012-03-11 14:47:46 +0000
committerPatrick Totzke <patricktotzke@gmail.com>2012-03-11 14:47:46 +0000
commit42a0466cafa979513f4e0efe6d81bcec953bc711 (patch)
tree1b55928642330591d2edda0d24fc1915826a0aec /alot/settings/theme.py
parent6cc3e22eaa6a16e597c051484844d7f13102e4f7 (diff)
cleanup: moved Theme to separate file
Diffstat (limited to 'alot/settings/theme.py')
-rw-r--r--alot/settings/theme.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/alot/settings/theme.py b/alot/settings/theme.py
new file mode 100644
index 00000000..301cc547
--- /dev/null
+++ b/alot/settings/theme.py
@@ -0,0 +1,71 @@
+import os
+from urwid import AttrSpec, AttrSpecError
+
+from utils import read_config
+from errors import ConfigError
+
+DEFAULTSPATH = os.path.join(os.path.dirname(__file__), '..', 'defaults')
+
+
+class Theme(object):
+ """Colour theme"""
+ def __init__(self, path):
+ """
+ :param path: path to theme file
+ :type path: str
+ """
+ self._spec = os.path.join(DEFAULTSPATH, 'theme.spec')
+ self._config = read_config(path, self._spec)
+ self.attributes = self._parse_attributes(self._config)
+
+ def _parse_attributes(self, c):
+ """
+ parse a (previously validated) valid theme file
+ into urwid AttrSpec attributes for internal use.
+
+ :param c: config object for theme file
+ :type c: `configobj.ConfigObj`
+ :raises: `ConfigError`
+ """
+
+ attributes = {}
+ for sec in c.sections:
+ try:
+ colours = int(sec)
+ except ValueError:
+ err_msg = 'section name %s is not a valid colour mode'
+ raise ConfigError(err_msg % sec)
+ attributes[colours] = {}
+ for mode in c[sec].sections:
+ attributes[colours][mode] = {}
+ for themable in c[sec][mode].sections:
+ block = c[sec][mode][themable]
+ fg = block['fg']
+ if colours == 1:
+ bg = 'default'
+ else:
+ bg = block['bg']
+ if colours == 256:
+ fg = fg or c['16'][mode][themable][fg]
+ bg = bg or c['16'][mode][themable][bg]
+ try:
+ att = AttrSpec(fg, bg, colours)
+ except AttrSpecError, e:
+ raise ConfigError(e)
+ attributes[colours][mode][themable] = att
+ return attributes
+
+ def get_attribute(self, mode, name, colourmode):
+ """
+ returns requested attribute
+
+ :param mode: ui-mode (e.g. `search`,`thread`...)
+ :type mode: str
+ :param name: identifier of the atttribute
+ :type name: str
+ :param colourmode: colour mode; in [1, 16, 256]
+ :type colourmode: int
+ """
+ return self.attributes[colourmode][mode][name]
+
+