Skip to content

Commit

Permalink
alot/settings: Use notmuch config to read configuration
Browse files Browse the repository at this point in the history
The main benefit from this is that we get the defaults directly from
notmuch and do not risk using inconsistent configuration values.

A good example of this is getting the database path. Getting the path
directly from the notmuch config file is not enough, because the user
might be using the default location with no explicit entry in the file.
Since notmuch does is a bit of logic notmuch to get the database path
(see section "DATABASE LOCATION" in man page for notmuch-config(1)), it
is better to get it directly from "notmuch config".

Since get_notmuch_setting() now always returns a string, also refactor
the call to settings.get_notmuch_setting('maildir', 'synchronize_flags')
for this new reality.
  • Loading branch information
guludo authored and pazz committed Apr 14, 2023
1 parent d67bb1b commit 023cf16
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 13 deletions.
2 changes: 1 addition & 1 deletion alot/db/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def flush(self):
raise DatabaseROError()
if self.writequeue:
# read notmuch's config regarding imap flag synchronization
sync = settings.get_notmuch_setting('maildir', 'synchronize_flags')
sync = settings.get_notmuch_setting('maildir', 'synchronize_flags') == 'true'

# go through writequeue entries
while self.writequeue:
Expand Down
3 changes: 0 additions & 3 deletions alot/defaults/notmuch.rc

This file was deleted.

3 changes: 0 additions & 3 deletions alot/defaults/notmuch.rc.spec

This file was deleted.

12 changes: 7 additions & 5 deletions alot/settings/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ..utils import configobj as checks

from .errors import ConfigError, NoMatchingAccount
from .utils import read_config
from .utils import read_config, read_notmuch_config
from .utils import resolve_att
from .theme import Theme

Expand All @@ -36,12 +36,13 @@ def __init__(self):
self._accounts = None
self._accountmap = None
self._notmuchconfig = None
self._notmuchconfig_path = None
self._config = ConfigObj()
self._bindings = None

def reload(self):
"""Reload notmuch and alot config files"""
self.read_notmuch_config(self._notmuchconfig.filename)
self.read_notmuch_config(self._notmuchconfig_path)
self.read_config(self._config.filename)

def read_notmuch_config(self, path):
Expand All @@ -50,8 +51,8 @@ def read_notmuch_config(self, path):
:param path: path to notmuch's config file
:type path: str
"""
spec = os.path.join(DEFAULTSPATH, 'notmuch.rc.spec')
self._notmuchconfig = read_config(path, spec)
self._notmuchconfig = read_notmuch_config(path)
self._notmuchconfig_path = path # Set the path *after* succesful read.

def _update_bindings(self, newbindings):
assert isinstance(newbindings, Section)
Expand Down Expand Up @@ -275,7 +276,8 @@ def get_notmuch_setting(self, section, key, fallback=None):
:type key: str
:param fallback: fallback returned if key is not present
:type fallback: str
:returns: config value with type as specified in the spec-file
:returns: the config value
:rtype: str
"""
value = None
if section in self._notmuchconfig:
Expand Down
35 changes: 35 additions & 0 deletions alot/settings/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from urwid import AttrSpec

from .errors import ConfigError
from ..helper import call_cmd


def read_config(configpath=None, specpath=None, checks=None,
Expand Down Expand Up @@ -82,6 +83,40 @@ def read_config(configpath=None, specpath=None, checks=None,
return config


def read_notmuch_config(path):
"""
Read notmuch configuration.
This function calls the command "notmuch --config {path} config list" and
parses its output into a ``config`` dictionary, which is then returned.
The configuration value for a key under a section can be accessed with
``config[section][key]``.
The returned value is a dict ``config`` with
:param path: path to the configuration file, which is passed as
argument to the --config option of notmuch.
:type path: str
:raises: :class:`~alot.settings.errors.ConfigError`
:rtype: `dict`
"""
cmd = ['notmuch', '--config', path, 'config', 'list']
out, err, code = call_cmd(cmd)
if code != 0:
msg = f'failed to read notmuch config with command {cmd} (exit error: {code}):\n{err}'
logging.error(msg)
raise ConfigError(msg)

config = {}
for line in out.splitlines():
left_hand, right_hand = line.split("=", maxsplit=1)
section, key = left_hand.split(".", maxsplit=1)
config.setdefault(section, {})[key] = right_hand

return config


def resolve_att(a, fallback):
""" replace '' and 'default' by fallback values """
if a is None:
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
package_data={
'alot': [
'defaults/alot.rc.spec',
'defaults/notmuch.rc.spec',
'defaults/abook_contacts.spec',
'defaults/default.theme',
'defaults/default.bindings',
Expand Down

0 comments on commit 023cf16

Please sign in to comment.