diff options
-rw-r--r-- | alot/__main__.py | 11 | ||||
-rw-r--r-- | alot/settings/manager.py | 59 | ||||
-rw-r--r-- | tests/commands/envelope_test.py | 3 | ||||
-rw-r--r-- | tests/settings/manager_test.py | 59 |
4 files changed, 54 insertions, 78 deletions
diff --git a/alot/__main__.py b/alot/__main__.py index b46985cb..1000f2c9 100644 --- a/alot/__main__.py +++ b/alot/__main__.py @@ -90,20 +90,17 @@ def main(): filemode='w', format=logformat) # locate alot config files + cpath = options.config if options.config is None: xdg_dir = get_xdg_env('XDG_CONFIG_HOME', os.path.expanduser('~/.config')) alotconfig = os.path.join(xdg_dir, 'alot', 'config') if os.path.exists(alotconfig): - settings.alot_rc_path = alotconfig - else: - settings.alot_rc_path = options.config - - settings.notmuch_rc_path = options.notmuch_config + cpath = alotconfig try: - settings.read_config() - settings.read_notmuch_config() + settings.read_config(cpath) + settings.read_notmuch_config(options.notmuch_config) except (ConfigError, OSError, IOError) as e: print('Error when parsing a config file. ' 'See log for potential details.') 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) diff --git a/tests/commands/envelope_test.py b/tests/commands/envelope_test.py index e46b33a1..aea84327 100644 --- a/tests/commands/envelope_test.py +++ b/tests/commands/envelope_test.py @@ -319,7 +319,8 @@ class TestSignCommand(unittest.TestCase): self.addCleanup(os.unlink, f.name) # Set the gpg_key separately to avoid validation failures - manager = SettingsManager(alot_rc=f.name) + manager = SettingsManager() + manager.read_config(f.name) manager.get_accounts()[0].gpg_key = mock.sentinel.gpg_key return manager diff --git a/tests/settings/manager_test.py b/tests/settings/manager_test.py index dec676b3..e82d5f27 100644 --- a/tests/settings/manager_test.py +++ b/tests/settings/manager_test.py @@ -27,7 +27,8 @@ class TestSettingsManager(unittest.TestCase): """)) self.addCleanup(os.unlink, f.name) - manager = SettingsManager(notmuch_rc=f.name) + manager = SettingsManager() + manager.read_notmuch_config(f.name) actual = manager.get_notmuch_setting('maildir', 'synchronize_flags') self.assertTrue(actual) @@ -40,7 +41,8 @@ class TestSettingsManager(unittest.TestCase): self.addCleanup(os.unlink, f.name) with self.assertRaises(ConfigError): - SettingsManager(notmuch_rc=f.name) + manager = SettingsManager() + manager.read_notmuch_config(f.name) def test_reload_notmuch_config(self): with tempfile.NamedTemporaryFile(mode='w+', delete=False) as f: @@ -49,7 +51,8 @@ class TestSettingsManager(unittest.TestCase): synchronize_flags = false """)) self.addCleanup(os.unlink, f.name) - manager = SettingsManager(notmuch_rc=f.name) + manager = SettingsManager() + manager.read_notmuch_config(f.name) with tempfile.NamedTemporaryFile(mode='w+', delete=False) as f: f.write(textwrap.dedent("""\ @@ -58,8 +61,7 @@ class TestSettingsManager(unittest.TestCase): """)) self.addCleanup(os.unlink, f.name) - manager.notmuch_rc_path = f.name - manager.reload() + manager.read_notmuch_config(f.name) actual = manager.get_notmuch_setting('maildir', 'synchronize_flags') self.assertTrue(actual) @@ -76,7 +78,8 @@ class TestSettingsManager(unittest.TestCase): synchronize_flags = true """)) self.addCleanup(os.unlink, f.name) - manager = SettingsManager(notmuch_rc=f.name) + manager = SettingsManager() + manager.read_config(f.name) manager.get_theming_attribute('global', 'body') @@ -99,7 +102,9 @@ class TestSettingsManager(unittest.TestCase): self.addCleanup(os.unlink, f.name) with mock.patch('alot.settings.utils.logging') as mock_logger: - SettingsManager(alot_rc=f.name) + manager = SettingsManager() + manager.read_config(f.name) + success = any(all([s in call_args[0][0] for s in unknown_settings]) for call_args in mock_logger.info.call_args_list) self.assertTrue(success, msg='Could not find all unknown settings in ' @@ -116,7 +121,8 @@ class TestSettingsManager(unittest.TestCase): address = thatguy@example.com """)) self.addCleanup(os.unlink, f.name) - manager = SettingsManager(alot_rc=f.name) + manager = SettingsManager() + manager.read_notmuch_config(f.name) setting = manager.get_notmuch_setting('foo', 'bar') self.assertIsNone(setting) @@ -130,7 +136,8 @@ class TestSettingsManager(unittest.TestCase): normal = '','', 'white','light red', 'white','#d66' """.format(tag=tag))) self.addCleanup(os.unlink, f.name) - manager = SettingsManager(alot_rc=f.name) + manager = SettingsManager() + manager.read_config(f.name) manager.get_tagstring_representation(tag) @@ -143,28 +150,6 @@ class TestSettingsManagerExpandEnvironment(unittest.TestCase): xdg_custom = '/foo/bar/.config' default_expanded = default.replace('$%s' % xdg_name, xdg_fallback) - def test_no_user_setting_and_env_not_set(self): - with mock.patch.dict('os.environ'): - if self.xdg_name in os.environ: - del os.environ[self.xdg_name] - manager = SettingsManager() - self.assertEqual(manager._config.get(self.setting_name), - os.path.expanduser(self.default_expanded)) - - def test_no_user_setting_and_env_empty(self): - with mock.patch.dict('os.environ', {self.xdg_name: ''}): - manager = SettingsManager() - self.assertEqual(manager._config.get(self.setting_name), - os.path.expanduser(self.default_expanded)) - - def test_no_user_setting_and_env_not_empty(self): - with mock.patch.dict('os.environ', {self.xdg_name: self.xdg_custom}): - manager = SettingsManager() - actual = manager._config.get(self.setting_name) - expected = self.default.replace('$%s' % self.xdg_name, - self.xdg_custom) - self.assertEqual(actual, expected) - def test_user_setting_and_env_not_empty(self): user_setting = '/path/to/template/dir' @@ -173,7 +158,8 @@ class TestSettingsManagerExpandEnvironment(unittest.TestCase): f.write('template_dir = {}'.format(user_setting)) self.addCleanup(os.unlink, f.name) - manager = SettingsManager(alot_rc=f.name) + manager = SettingsManager() + manager.read_config(f.name) self.assertEqual(manager._config.get(self.setting_name), os.path.expanduser(user_setting)) @@ -193,7 +179,8 @@ class TestSettingsManagerExpandEnvironment(unittest.TestCase): """.format(foo_in_config))) self.addCleanup(os.unlink, f.name) - manager = SettingsManager(alot_rc=f.name) + manager = SettingsManager() + manager.read_config(f.name) self.assertEqual(manager._config.get(self.setting_name), os.path.join(self.xdg_custom, foo_env, foo_in_config, foo_env)) @@ -225,7 +212,8 @@ class TestSettingsManagerGetAccountByAddress(utilities.TestCaseClassCleanup): # Replace the actual settings object with our own using mock, but # ensure it's put back afterwards - cls.manager = SettingsManager(alot_rc=f.name) + cls.manager = SettingsManager() + cls.manager.read_config(f.name) def test_exists_addr(self): acc = self.manager.get_account_by_address(u'that_guy@example.com') @@ -243,7 +231,8 @@ class TestSettingsManagerGetAccountByAddress(utilities.TestCaseClassCleanup): def test_doesnt_exist_no_default(self): with tempfile.NamedTemporaryFile() as f: f.write(b'') - settings = SettingsManager(alot_rc=f.name) + settings = SettingsManager() + settings.read_config(f.name) with self.assertRaises(NoMatchingAccount): settings.get_account_by_address('that_guy@example.com', return_default=True) |