From 42a0466cafa979513f4e0efe6d81bcec953bc711 Mon Sep 17 00:00:00 2001 From: Patrick Totzke Date: Sun, 11 Mar 2012 14:47:46 +0000 Subject: cleanup: moved Theme to separate file --- alot/settings/theme.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 alot/settings/theme.py (limited to 'alot/settings/theme.py') 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] + + -- cgit v1.2.3