diff options
author | Patrick Totzke <patricktotzke@gmail.com> | 2018-06-19 21:44:24 +0100 |
---|---|---|
committer | Patrick Totzke <patricktotzke@gmail.com> | 2018-06-21 21:34:13 +0100 |
commit | 6bb014f251b0a3b5c6d0b3aa972c588d771d6867 (patch) | |
tree | f4a7aa54d3e07186bd2e69bff2688e6024461f9e /alot/settings | |
parent | c30afc72150e30770da6fc1e5f6e68dce2b9a6bb (diff) |
lazy reading of config files in SettingsManager
This prevents SettingsManager from reading the config files right when
it is isntantiated and instead waits for the main module to call
`read_[notmuch]config` with the right path.
This should prevent problems with accidentally reading the default
config paths despite being told otherwise (via commandline options)
Diffstat (limited to 'alot/settings')
-rw-r--r-- | alot/settings/manager.py | 59 |
1 files changed, 24 insertions, 35 deletions
diff --git a/alot/settings/manager.py b/alot/settings/manager.py index 1e275aeb..d1f57083 100644 --- a/alot/settings/manager.py +++ b/alot/settings/manager.py @@ -29,55 +29,46 @@ DATA_DIRS = get_xdg_env('XDG_DATA_DIRS', class SettingsManager(object): """Organizes user settings""" - def __init__(self, alot_rc=None, notmuch_rc=None): - """ - :param alot_rc: path to alot's config file - :type alot_rc: str - :param notmuch_rc: path to notmuch's config file - :type notmuch_rc: str - """ - assert alot_rc is None or (isinstance(alot_rc, str) and - os.path.exists(alot_rc)) - assert notmuch_rc is None or (isinstance(notmuch_rc, str) and - os.path.exists(notmuch_rc)) + def __init__(self): self.hooks = None self._mailcaps = mailcap.getcaps() self._notmuchconfig = None self._theme = None self._accounts = None self._accountmap = None - self.alot_rc_path = alot_rc - self.notmuch_rc_path = notmuch_rc self._notmuchconfig = None self._config = ConfigObj() self._bindings = None - self.reload() def reload(self): - """Reload All configurations. - - This first resets all configs to default (in case an overwritten - binding is removed from the user config), then reloads the notmuch - config, and finally reads the alot config. + """Reload notmuch and alot config files""" + self.read_notmuch_config(self._notmuchconfig.filename) + self.read_config(self._config.filename) - Implementation Detail: this is the same code called by the constructor - to set bindings at alot startup. + def read_notmuch_config(self, path): """ + parse notmuch's config file + :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) + + def _update_bindings(self, newbindings): + assert isinstance(newbindings, Section) + self._bindings = ConfigObj(os.path.join(DEFAULTSPATH, 'default.bindings')) - self.read_notmuch_config() - self.read_config() - - def read_notmuch_config(self): - """parse notmuch's config file from path""" - spec = os.path.join(DEFAULTSPATH, 'notmuch.rc.spec') - self._notmuchconfig = read_config(self.notmuch_rc_path, spec) + self._bindings.merge(newbindings) - def read_config(self): - """parse alot's config file from path""" + def read_config(self, path): + """ + parse alot's config file + :param path: path to alot's config file + :type path: str + """ spec = os.path.join(DEFAULTSPATH, 'alot.rc.spec') - newconfig = read_config( - self.alot_rc_path, spec, report_extra=True, checks={ + newconfig = read_config(path, spec, report_extra=True, checks={ 'mail_container': checks.mail_container, 'force_list': checks.force_list, 'align': checks.align_mode, @@ -94,9 +85,7 @@ class SettingsManager(object): except: logging.exception('unable to load hooks file:%s', hooks_path) if 'bindings' in newconfig: - newbindings = newconfig['bindings'] - if isinstance(newbindings, Section): - self._bindings.merge(newbindings) + self._update_bindings(newconfig['bindings']) tempdir = self._config.get('template_dir') logging.debug('template directory: `%s`' % tempdir) |