summaryrefslogtreecommitdiff
path: root/alot/settings
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2017-07-17 10:33:35 -0700
committerDylan Baker <dylan@pnwbakers.com>2017-07-19 11:57:13 -0700
commit06b543978c6e625da541e878f62225ea16f91f0f (patch)
tree2dc91ae2e586e715e90ff4efae7f78cdf3cef411 /alot/settings
parent9406ce0379c7aec54a6ac9ba9ae7dc4c785ff082 (diff)
settings: Allow loading themes from system locations
This adds additional system locations to search for theme files, which are either defined as the environment variable XDG_DATA_DIRS, or the generic fallback (per the xdg base directories spec) as /usr/local/share and /usr/share. We then add alot/themees to those paths to search for themes in. This allows a package manager to install the theme files system wide along with alot, and for alot to load them from the system wide directories. The Archlinux alot-git package is already installing the packages like this.
Diffstat (limited to 'alot/settings')
-rw-r--r--alot/settings/manager.py38
1 files changed, 27 insertions, 11 deletions
diff --git a/alot/settings/manager.py b/alot/settings/manager.py
index 5e86ecec..ab68659e 100644
--- a/alot/settings/manager.py
+++ b/alot/settings/manager.py
@@ -4,10 +4,11 @@
from __future__ import absolute_import
import imp
+import itertools
+import logging
+import mailcap
import os
import re
-import mailcap
-import logging
from configobj import ConfigObj, Section
from ..account import SendmailAccount
@@ -23,6 +24,7 @@ from .theme import Theme
DEFAULTSPATH = os.path.join(os.path.dirname(__file__), '..', 'defaults')
+DATA_DIRS = os.environ.get('XDG_DATA_DIRS', '/usr/local/share:/usr/share').split(':')
class SettingsManager(object):
@@ -106,17 +108,31 @@ class SettingsManager(object):
logging.debug(themes_dir)
# if config contains theme string use that
+ data_dirs = [os.path.join(d, 'alot/themes') for d in DATA_DIRS]
if themestring:
- if not os.path.isdir(themes_dir):
- err_msg = 'cannot find theme %s: themes_dir %s is missing'
- raise ConfigError(err_msg % (themestring, themes_dir))
+ # This is a python for/else loop
+ # https://docs.python.org/3/reference/compound_stmts.html#for
+ #
+ # tl/dr; If the loop loads a theme it breaks. If it doesn't break,
+ # then it raises a ConfigError.
+ for dir_ in itertools.chain([themes_dir], data_dirs):
+ if not os.path.isdir(dir_):
+ logging.warning(
+ 'cannot find theme %s: themes_dir %s is missing',
+ themestring, dir_)
+ else:
+ theme_path = os.path.join(dir_, themestring)
+ try:
+ self._theme = Theme(theme_path)
+ except ConfigError as e:
+ logging.warning(
+ 'Theme file %s failed validation: %s',
+ themestring, str(e.message))
+ else:
+ break
else:
- theme_path = os.path.join(themes_dir, themestring)
- try:
- self._theme = Theme(theme_path)
- except ConfigError as e:
- err_msg = 'Theme file %s failed validation:\n'
- raise ConfigError((err_msg % themestring) + str(e.message))
+ raise ConfigError('Cannot load theme {}, see log for more '
+ 'information'.format(themestring))
# if still no theme is set, resort to default
if self._theme is None: