summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Totzke <patricktotzke@gmail.com>2012-02-18 11:50:59 +0000
committerPatrick Totzke <patricktotzke@gmail.com>2012-02-18 11:50:59 +0000
commite7c257e0c396f8e920751c6e11205de6b8c90afa (patch)
tree8119b6affe87b7a41ffa5b394c178ddb49b7b374
parent1a5ceebc3ea958aea71878865adde43a2e1d24d8 (diff)
add SettingsManager
that handles alot.rc, notmuch.rc and themes: It is intended to be a uniform object that offers *all* user settings via getters: * theming_attributes * bindings * notmuch/alot config entries later on: construction of accounts
-rw-r--r--alot/settings.py65
1 files changed, 61 insertions, 4 deletions
diff --git a/alot/settings.py b/alot/settings.py
index 5e67f5ea..b559f172 100644
--- a/alot/settings.py
+++ b/alot/settings.py
@@ -57,6 +57,62 @@ class Theme(object):
return urwid.AttrSpec(fg, bg, colourmode)
+class SettingsManager(object):
+ def __init__(self, alot_rc=None, notmuch_rc=None, theme=None):
+ self.hooks = None
+
+ self._config = ConfigObj()
+ self.read_config(alot_rc)
+ self.read_notmuch_config(notmuch_rc)
+
+ theme_path = theme or os.path.join(DEFAULTSPATH, 'theme.rc')
+ self.theme = Theme(theme_path)
+
+
+ def read_notmuch_config(self, path):
+ spec = os.path.join(DEFAULTSPATH, 'notmuch.rc.spec')
+ self._notmuchconfig = read_config(path, spec)
+
+ def read_config(self, path):
+ spec = os.path.join(DEFAULTSPATH, 'alot.rc.spec')
+ newconfig = read_config(path, spec)
+ self._config.merge(newconfig)
+
+ hooks_path = os.path.expanduser(self._config.get('hooksfile'))
+ try:
+ self.hooks = imp.load_source('hooks', hooks_path)
+ except:
+ logging.debug('unable to load hooks file:%s' % hooks_path)
+
+
+ def get_theming_attribute(self, mode, name):
+ colours = int(self._config.get('colourmode'))
+ return self.theme.get_attribute(mode, name, colours)
+
+ def get_hook(self, key):
+ """return hook (`callable`) identified by `key`"""
+ if self.hooks:
+ if key in self.hooks.__dict__:
+ return self.hooks.__dict__[key]
+ return None
+
+ def get_mapping(self, mode, key):
+ """look up keybinding from `MODE-maps` sections
+
+ :param mode: mode identifier
+ :type mode: str
+ :param key: urwid-style key identifier
+ :type key: str
+ :returns: a command line to be applied upon keypress
+ :rtype: str
+ """
+ cmdline = None
+ bindings = self._config['bindings']
+ if key in bindings[mode]:
+ cmdline = bindings[mode][key]
+ elif key in bindings['global']:
+ cmdline = bindings['global'][key]
+ return cmdline
class FallbackConfigParser(SafeConfigParser):
""":class:`~ConfigParser.SafeConfigParser` that allows fallback values"""
@@ -308,12 +364,13 @@ class AlotConfigParser(FallbackConfigParser):
return cmdline
+defaultconfig = os.path.join(DEFAULTSPATH, 'alot.rc')
+defaultnotmuchconfig = os.path.join(DEFAULTSPATH, 'notmuch.rc')
config = AlotConfigParser()
-config.read(os.path.join(os.path.dirname(__file__), 'defaults', 'alot.rc'))
+config.read(defaultconfig)
notmuchconfig = FallbackConfigParser()
-notmuchconfig.read(os.path.join(os.path.dirname(__file__),
- 'defaults',
- 'notmuch.rc'))
+notmuchconfig.read(defaultnotmuchconfig)
+settings = SettingsManager(os.path.join(DEFAULTSPATH, 'alot.rc.new'), defaultnotmuchconfig)
mailcaps = mailcap.getcaps()