From 4501d073407be97fdf4206bec853ea0ec1e5351b Mon Sep 17 00:00:00 2001 From: Julian Mehne Date: Sat, 2 Dec 2017 22:37:03 +0100 Subject: Inform user why :compose got cancelled. --- alot/commands/globals.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'alot') diff --git a/alot/commands/globals.py b/alot/commands/globals.py index 57ddfcb6..92ef3e7e 100644 --- a/alot/commands/globals.py +++ b/alot/commands/globals.py @@ -35,6 +35,7 @@ from ..widgets.utils import DialogBox from ..db.errors import DatabaseLockedError from ..db.envelope import Envelope from ..settings.const import settings +from ..settings.errors import NoMatchingAccount from ..utils import argparse as cargparse MODE = 'global' @@ -816,7 +817,14 @@ class ComposeCommand(Command): # find out the right account sender = self.envelope.get('From') name, addr = email.utils.parseaddr(sender) - account = settings.get_account_by_address(addr) + try: + account = settings.get_account_by_address(addr) + except NoMatchingAccount: + msg = 'Cannot compose mail - no account found for `%s`' % addr + logging.error(msg) + ui.notify(msg, priority='error') + raise CommandCanceled() + if account is None: accounts = settings.get_accounts() if not accounts: -- cgit v1.2.3 From b9ae81a590eee4ab3f601a5edc1ea3f451c39c66 Mon Sep 17 00:00:00 2001 From: Julian Mehne Date: Sun, 3 Dec 2017 10:29:04 +0100 Subject: Give more context if reading of config file fails. --- alot/__main__.py | 2 ++ alot/settings/utils.py | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'alot') diff --git a/alot/__main__.py b/alot/__main__.py index fc34164f..ee5e442a 100644 --- a/alot/__main__.py +++ b/alot/__main__.py @@ -106,6 +106,8 @@ def main(): settings.read_config() settings.read_notmuch_config() except (ConfigError, OSError, IOError) as e: + print('Error when parsing a config file. ' + 'See log for potential details.') sys.exit(e) # store options given by config swiches to the settingsManager: diff --git a/alot/settings/utils.py b/alot/settings/utils.py index d87157c3..242fa63c 100644 --- a/alot/settings/utils.py +++ b/alot/settings/utils.py @@ -3,6 +3,8 @@ # For further details see the COPYING file from __future__ import absolute_import +import logging + from configobj import ConfigObj, ConfigObjError, flatten_errors from validate import Validator from urwid import AttrSpec @@ -30,7 +32,9 @@ def read_config(configpath=None, specpath=None, checks=None): config = ConfigObj(infile=configpath, configspec=specpath, file_error=True, encoding='UTF8') except ConfigObjError as e: - raise ConfigError(e) + msg = 'Error when parsing `%s`:\n%s' % (configpath, e) + logging.error(msg) + raise ConfigError(msg) except IOError: raise ConfigError('Could not read %s and/or %s' % (configpath, specpath)) -- cgit v1.2.3 From 2ef957b4d9626eb1b1ff6d011823ac92f0eb917d Mon Sep 17 00:00:00 2001 From: Julian Mehne Date: Tue, 5 Dec 2017 03:09:48 +0100 Subject: Print stderr of failed sendmail cmd in prompt. --- alot/account.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'alot') diff --git a/alot/account.py b/alot/account.py index a4ce439a..c672686d 100644 --- a/alot/account.py +++ b/alot/account.py @@ -359,11 +359,11 @@ class SendmailAccount(Account): def errb(failure): """The callback used on error.""" termobj = failure.value - errmsg = '%s failed with code %s:\n%s' % \ - (self.cmd, termobj.exitCode, str(failure.value)) + errmsg = '%s failed with code %s:\n%s\nProcess stderr:\n%s' % \ + (self.cmd, termobj.exitCode, str(failure.value), + failure.value.stderr) logging.error(errmsg) logging.error(failure.getTraceback()) - logging.error(failure.value.stderr) raise SendingMailFailed(errmsg) # make sure self.mail is a string -- cgit v1.2.3 From 04bd1ed039cd17ddbea9785eee46cd25356b3eaa Mon Sep 17 00:00:00 2001 From: Julian Mehne Date: Wed, 6 Dec 2017 20:55:11 +0100 Subject: Clean up theme finder logic. - Replace directory check with file check - we only check for a single file, no need to check for existende of the directory separately. Also, this way we can distinguish between file validation error and file not found. - Be consistent: we stop iterating, if we find a file and it can be parsed. If we can't parse it, stop iterating as well, don't keep looking for the file. --- alot/settings/manager.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'alot') diff --git a/alot/settings/manager.py b/alot/settings/manager.py index 98d534fb..10a441ed 100644 --- a/alot/settings/manager.py +++ b/alot/settings/manager.py @@ -116,22 +116,19 @@ class SettingsManager(object): # 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_) + theme_path = os.path.join(dir_, themestring) + if not os.path.exists(os.path.expanduser(theme_path)): + logging.warning('Theme `%s` does not exist.', theme_path) 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, e) + raise ConfigError('Theme file `%s` failed ' + 'validation:\n%s' % (theme_path, e)) else: break else: - raise ConfigError('Cannot load theme {}, see log for more ' + raise ConfigError('Could not find theme {}, see log for more ' 'information'.format(themestring)) # if still no theme is set, resort to default -- cgit v1.2.3 From bea1848b595572521963a3533c224909b294a7f0 Mon Sep 17 00:00:00 2001 From: Julian Mehne Date: Sun, 7 Jan 2018 22:26:52 +0100 Subject: Report error if configuration file cannot be reloaded. --- alot/commands/globals.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'alot') diff --git a/alot/commands/globals.py b/alot/commands/globals.py index 92ef3e7e..bf1cad01 100644 --- a/alot/commands/globals.py +++ b/alot/commands/globals.py @@ -35,7 +35,7 @@ from ..widgets.utils import DialogBox from ..db.errors import DatabaseLockedError from ..db.envelope import Envelope from ..settings.const import settings -from ..settings.errors import NoMatchingAccount +from ..settings.errors import ConfigError, NoMatchingAccount from ..utils import argparse as cargparse MODE = 'global' @@ -973,4 +973,8 @@ class ReloadCommand(Command): """Reload configuration.""" def apply(self, ui): - settings.reload() + try: + settings.reload() + except ConfigError as e: + ui.notify('Error when reloading config files:\n {}'.format(e), + priority='error') -- cgit v1.2.3 From edd6c76123c033b9ddc53d674ae396f5ac9fe305 Mon Sep 17 00:00:00 2001 From: Julian Mehne Date: Sun, 7 Jan 2018 22:28:30 +0100 Subject: Log unknown settings in configuration and theme files. --- alot/settings/manager.py | 2 +- alot/settings/theme.py | 2 +- alot/settings/utils.py | 20 ++++++++++++++++++-- tests/settings/manager_test.py | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 4 deletions(-) (limited to 'alot') diff --git a/alot/settings/manager.py b/alot/settings/manager.py index 10a441ed..dffe1b32 100644 --- a/alot/settings/manager.py +++ b/alot/settings/manager.py @@ -79,7 +79,7 @@ class SettingsManager(object): """parse alot's config file from path""" spec = os.path.join(DEFAULTSPATH, 'alot.rc.spec') newconfig = read_config( - self.alot_rc_path, spec, checks={ + self.alot_rc_path, spec, report_extra=True, checks={ 'mail_container': checks.mail_container, 'force_list': checks.force_list, 'align': checks.align_mode, diff --git a/alot/settings/theme.py b/alot/settings/theme.py index f2a59763..d74cb0e7 100644 --- a/alot/settings/theme.py +++ b/alot/settings/theme.py @@ -22,7 +22,7 @@ class Theme(object): :raises: :class:`~alot.settings.errors.ConfigError` """ self._spec = os.path.join(DEFAULTSPATH, 'theme.spec') - self._config = read_config(path, self._spec, + self._config = read_config(path, self._spec, report_extra=True, checks={'align': checks.align_mode, 'widthtuple': checks.width_tuple, 'force_list': checks.force_list, diff --git a/alot/settings/utils.py b/alot/settings/utils.py index 242fa63c..8d656b79 100644 --- a/alot/settings/utils.py +++ b/alot/settings/utils.py @@ -5,14 +5,16 @@ from __future__ import absolute_import import logging -from configobj import ConfigObj, ConfigObjError, flatten_errors +from configobj import (ConfigObj, ConfigObjError, flatten_errors, + get_extra_values) from validate import Validator from urwid import AttrSpec from .errors import ConfigError -def read_config(configpath=None, specpath=None, checks=None): +def read_config(configpath=None, specpath=None, checks=None, + report_extra=False): """ get a (validated) config object for given config file path. @@ -23,6 +25,8 @@ def read_config(configpath=None, specpath=None, checks=None): :param checks: custom checks to use for validator. see `validate docs `_ :type checks: dict str->callable, + :param report_extra: log if a setting is not present in the spec file + :type report_extra: boolean :raises: :class:`~alot.settings.errors.ConfigError` :rtype: `configobj.ConfigObj` """ @@ -65,6 +69,18 @@ def read_config(configpath=None, specpath=None, checks=None): msg = 'section "%s" is missing' % '.'.join(section_list) error_msg += msg + '\n' raise ConfigError(error_msg) + + extra_values = get_extra_values(config) if report_extra else None + if extra_values: + msg = ['Unknown values were found in `%s`. Please check for ' + 'typos if a specified setting does not seem to work:' + % configpath] + for sections, val in extra_values: + if sections: + msg.append('%s: %s' % ('->'.join(sections), val)) + else: + msg.append(str(val)) + logging.info('\n'.join(msg)) return config diff --git a/tests/settings/manager_test.py b/tests/settings/manager_test.py index 42bc584a..eb2f37fd 100644 --- a/tests/settings/manager_test.py +++ b/tests/settings/manager_test.py @@ -11,6 +11,8 @@ import tempfile import textwrap import unittest +import mock + from alot.settings.manager import SettingsManager from alot.settings.errors import ConfigError, NoMatchingAccount @@ -80,6 +82,40 @@ class TestSettingsManager(unittest.TestCase): manager.get_theming_attribute('global', 'body') + def test_unknown_settings_in_config_are_logged(self): + # todo: For py3, don't mock the logger, use assertLogs + unknown_settings = ['templates_dir', 'unknown_section', 'unknown_1', + 'unknown_2'] + with tempfile.NamedTemporaryFile(delete=False) as f: + f.write(textwrap.dedent("""\ + {x[0]} = /templates/dir + [{x[1]}] + # Values in unknown sections are not reported. + barfoo = barfoo + [tags] + [[foobar]] + {x[2]} = baz + translated = translation + {x[3]} = bar + """.format(x=unknown_settings))) + self.addCleanup(os.unlink, f.name) + + with mock.patch('alot.settings.utils.logging') as mock_logger: + SettingsManager(alot_rc=f.name) + success = False + for call_args in mock_logger.info.call_args_list: + msg = call_args[0][0] + if all([s in msg for s in unknown_settings]): + success = True + break + else: + print('Could not find all unknown settings in logging.info.\n' + 'Unknown settings:') + print(unknown_settings) + print('Calls to mocked logging.info:') + print(mock_logger.info.call_args_list) + self.assertTrue(success) + def test_read_notmuch_config_doesnt_exist(self): with tempfile.NamedTemporaryFile(delete=False) as f: f.write(textwrap.dedent("""\ -- cgit v1.2.3