From da867b26a9a98f19119987d26433a6524dbd52e5 Mon Sep 17 00:00:00 2001 From: ZyX Date: Tue, 12 Aug 2014 20:33:03 +0400 Subject: [PATCH 1/2] Fix VimPowerline.add_local_theme After #783 it started to fail as it made Theme require defining dividers in theme and add_local_theme did not merge in other themes (__main__ and top). --- powerline/__init__.py | 35 +++++++++++++++++++++++++---------- powerline/vim.py | 11 ++++++++++- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/powerline/__init__.py b/powerline/__init__.py index ff568cf1..f78b1135 100644 --- a/powerline/__init__.py +++ b/powerline/__init__.py @@ -389,9 +389,16 @@ class Powerline(object): if interval is not None and not self.config_loader.is_alive(): self.config_loader.start() - self.default_top_theme = self.common_config['default_top_theme'] - self.ext_config = config['ext'][self.ext] + + self.theme_levels = ( + os.path.join('themes', ( + self.ext_config.get('top_theme') + or self.common_config['default_top_theme'] + )), + os.path.join('themes', self.ext, '__main__'), + ) + if self.ext_config != self.prev_ext_config: ext_config_differs = True if ( @@ -468,8 +475,18 @@ class Powerline(object): ''' return get_config_paths() - def _load_config(self, cfg_path, cfg_type): - '''Load configuration and setup watches.''' + def load_config(self, cfg_path, cfg_type): + '''Load configuration and setup watches + + :param str cfg_path: + Path to the configuration file without any powerline configuration + directory or ``.json`` suffix. + :param str cfg_type: + Configuration type. May be one of ``main`` (for ``config.json`` + file), ``colors``, ``colorscheme``, ``theme``. + + :return: dictionary with loaded configuration. + ''' return load_config( cfg_path, self.find_config_files, @@ -487,7 +504,7 @@ class Powerline(object): :return: dictionary with :ref:`top-level configuration `. ''' - return self._load_config('config', 'main') + return self.load_config('config', 'main') def _load_hierarhical_config(self, cfg_type, levels, ignore_levels): '''Load and merge multiple configuration files @@ -509,7 +526,7 @@ class Powerline(object): exceptions = [] for i, cfg_path in enumerate(levels): try: - lvl_config = self._load_config(cfg_path, cfg_type) + lvl_config = self.load_config(cfg_path, cfg_type) except IOError as e: if sys.version_info < (3,): tb = sys.exc_info()[2] @@ -553,9 +570,7 @@ class Powerline(object): :return: dictionary with :ref:`theme configuration ` ''' - levels = ( - os.path.join('themes', self.ext_config.get('top_theme') or self.default_top_theme), - os.path.join('themes', self.ext, '__main__'), + levels = self.theme_levels + ( os.path.join('themes', self.ext, name), ) return self._load_hierarhical_config('theme', levels, (0, 1,)) @@ -565,7 +580,7 @@ class Powerline(object): :return: dictionary with :ref:`colors configuration `. ''' - return self._load_config('colors', 'colors') + return self.load_config('colors', 'colors') @staticmethod def get_local_themes(local_themes): diff --git a/powerline/vim.py b/powerline/vim.py index c60ee20c..29afa9b9 100644 --- a/powerline/vim.py +++ b/powerline/vim.py @@ -51,8 +51,17 @@ class VimPowerline(Powerline): ''' self.update_renderer() key = self.get_matcher(key) + theme_config = {} + for cfg_path in self.theme_levels: + try: + lvl_config = self.load_config(cfg_path, 'theme') + except IOError: + pass + else: + mergedicts(theme_config, lvl_config) + mergedicts(theme_config, config) try: - self.renderer.add_local_theme(key, {'config': config}) + self.renderer.add_local_theme(key, {'config': theme_config}) except KeyError: return False else: From 1afab26cecfe369f0d56a2b77dad0a2b4b7c1657 Mon Sep 17 00:00:00 2001 From: ZyX Date: Tue, 12 Aug 2014 20:48:49 +0400 Subject: [PATCH 2/2] Test that VimPowerline.add_local_theme works --- powerline/vim.py | 3 ++- tests/matchers.py | 7 +++++++ tests/test_configuration.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/matchers.py diff --git a/powerline/vim.py b/powerline/vim.py index 29afa9b9..df009856 100644 --- a/powerline/vim.py +++ b/powerline/vim.py @@ -78,10 +78,11 @@ class VimPowerline(Powerline): 'powerline_theme_overrides__' + name) def get_local_themes(self, local_themes): + self.get_matcher = gen_matcher_getter(self.ext, self.import_paths) + if not local_themes: return {} - self.get_matcher = gen_matcher_getter(self.ext, self.import_paths) return dict(((None if key == '__tabline__' else self.get_matcher(key), {'config': self.load_theme_config(val)}) for key, val in local_themes.items())) diff --git a/tests/matchers.py b/tests/matchers.py new file mode 100644 index 00000000..3937f2f1 --- /dev/null +++ b/tests/matchers.py @@ -0,0 +1,7 @@ +# vim:fileencoding=utf-8:noet + +from __future__ import (unicode_literals, division, absolute_import, print_function) + + +def always_true(matcher_info): + return True diff --git a/tests/test_configuration.py b/tests/test_configuration.py index 6a64ce36..ec05dab4 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -402,6 +402,34 @@ class TestVim(TestCase): vim_module._environ['TEST'] = 'def' self.assertEqual(powerline.render(window, window_id, winnr), '%#Pl_3_8404992_4_192_underline#\xa0def%#Pl_4_192_NONE_None_NONE#>>') + def test_local_themes(self): + # Regression test: VimPowerline.add_local_theme did not work properly. + from powerline.vim import VimPowerline + import powerline as powerline_module + import vim + with swap_attributes(config, powerline_module): + with get_powerline_raw(config, VimPowerline) as powerline: + powerline.add_local_theme('tests.matchers.always_true', { + 'segment_data': { + 'foo': { + 'contents': '“bar”' + } + }, + 'segments': { + 'left': [ + { + 'type': 'string', + 'name': 'foo', + 'highlight_group': ['g1'] + } + ] + } + }) + window = vim_module.current.window + window_id = 1 + winnr = window.number + self.assertEqual(powerline.render(window, window_id, winnr), '%#Pl_5_12583104_6_32896_NONE#\xa0\u201cbar\u201d%#Pl_6_32896_NONE_None_NONE#>>') + def setUpModule(): sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), 'path')))