From 667cd4bce58beea9a7ef92f389d5285a068e2cc6 Mon Sep 17 00:00:00 2001 From: ZyX Date: Thu, 4 Sep 2014 21:21:46 +0400 Subject: [PATCH] Assume highlight group is always an iterable According to the documentation and linter `'highlight_group': 'string'` was not correct even at the current stage, but it worked. This commit fixes this situation. --- powerline/colorscheme.py | 12 ++---------- powerline/segment.py | 2 +- powerline/segments/common.py | 4 ++-- powerline/segments/shell.py | 12 +++++++++--- tests/test_segments.py | 12 ++++++------ 5 files changed, 20 insertions(+), 22 deletions(-) diff --git a/powerline/colorscheme.py b/powerline/colorscheme.py index 4ae8d74f..8d718863 100644 --- a/powerline/colorscheme.py +++ b/powerline/colorscheme.py @@ -32,14 +32,6 @@ def pick_gradient_value(grad_list, gradient_level): return grad_list[int(round(gradient_level * (len(grad_list) - 1) / 100))] -def hl_iter(value): - if type(value) is list: - for v in value: - yield v - else: - yield value - - class Colorscheme(object): def __init__(self, colorscheme_config, colors_config): '''Initialize a colorscheme.''' @@ -105,12 +97,12 @@ class Colorscheme(object): def get_highlighting(self, groups, mode, gradient_level=None): trans = self.translations.get(mode, {}) - for group in hl_iter(groups): + for group in groups: group_props = self.get_group_props(mode, trans, group) if group_props: break else: - raise KeyError('Highlighting groups not found in colorscheme: ' + ', '.join(hl_iter(groups))) + raise KeyError('Highlighting groups not found in colorscheme: ' + ', '.join(groups)) if gradient_level is None: pick_color = self.colors.__getitem__ diff --git a/powerline/segment.py b/powerline/segment.py index 96fd2652..0f19f786 100644 --- a/powerline/segment.py +++ b/powerline/segment.py @@ -142,7 +142,7 @@ def set_segment_highlighting(pl, colorscheme, segment): ) if segment['divider_highlight_group']: segment['divider_highlight'] = colorscheme.get_highlighting( - segment['divider_highlight_group'], + (segment['divider_highlight_group'],), segment['mode'] ) else: diff --git a/powerline/segments/common.py b/powerline/segments/common.py index 144e9596..bb83ea89 100644 --- a/powerline/segments/common.py +++ b/powerline/segments/common.py @@ -750,7 +750,7 @@ def user(pl, segment_info=None, hide_user=None): euid = _geteuid() return [{ 'contents': username, - 'highlight_group': 'user' if euid != 0 else ['superuser', 'user'], + 'highlight_group': ['user'] if euid != 0 else ['superuser', 'user'], }] if 'psutil' not in globals(): user = requires_segment_info(user) @@ -966,7 +966,7 @@ class EmailIMAPSegment(KwThreadedSegment): elif type(unread_count) != int or not max_msgs: return [{ 'contents': str(unread_count), - 'highlight_group': 'email_alert', + 'highlight_group': ['email_alert'], }] else: return [{ diff --git a/powerline/segments/shell.py b/powerline/segments/shell.py index e7e71f19..f1ac2845 100644 --- a/powerline/segments/shell.py +++ b/powerline/segments/shell.py @@ -29,7 +29,7 @@ def last_status(pl, segment_info): ''' if not segment_info['args'].last_exit_code: return None - return [{'contents': str(segment_info['args'].last_exit_code), 'highlight_group': 'exit_fail'}] + return [{'contents': str(segment_info['args'].last_exit_code), 'highlight_group': ['exit_fail']}] @requires_segment_info @@ -40,8 +40,14 @@ def last_pipe_status(pl, segment_info): ''' last_pipe_status = segment_info['args'].last_pipe_status if any(last_pipe_status): - return [{'contents': str(status), 'highlight_group': 'exit_fail' if status else 'exit_success', 'draw_inner_divider': True} - for status in last_pipe_status] + return [ + { + 'contents': str(status), + 'highlight_group': ['exit_fail' if status else 'exit_success'], + 'draw_inner_divider': True + } + for status in last_pipe_status + ] else: return None diff --git a/tests/test_segments.py b/tests/test_segments.py index 67dc1cac..2cc6545b 100644 --- a/tests/test_segments.py +++ b/tests/test_segments.py @@ -30,7 +30,7 @@ class TestShell(TestCase): pl = Pl() segment_info = {'args': Args(last_exit_code=10)} self.assertEqual(shell.last_status(pl=pl, segment_info=segment_info), [ - {'contents': '10', 'highlight_group': 'exit_fail'} + {'contents': '10', 'highlight_group': ['exit_fail']} ]) segment_info['args'].last_exit_code = 0 self.assertEqual(shell.last_status(pl=pl, segment_info=segment_info), None) @@ -45,9 +45,9 @@ class TestShell(TestCase): self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=segment_info), None) segment_info['args'].last_pipe_status = [0, 2, 0] self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=segment_info), [ - {'contents': '0', 'highlight_group': 'exit_success', 'draw_inner_divider': True}, - {'contents': '2', 'highlight_group': 'exit_fail', 'draw_inner_divider': True}, - {'contents': '0', 'highlight_group': 'exit_success', 'draw_inner_divider': True} + {'contents': '0', 'highlight_group': ['exit_success'], 'draw_inner_divider': True}, + {'contents': '2', 'highlight_group': ['exit_fail'], 'draw_inner_divider': True}, + {'contents': '0', 'highlight_group': ['exit_success'], 'draw_inner_divider': True} ]) def test_jobnum(self): @@ -335,10 +335,10 @@ class TestCommon(TestCase): with replace_attr(common, 'psutil', new_psutil): with replace_attr(common, '_geteuid', lambda: 5): self.assertEqual(common.user(pl=pl, segment_info=segment_info), [ - {'contents': 'def', 'highlight_group': 'user'} + {'contents': 'def', 'highlight_group': ['user']} ]) self.assertEqual(common.user(pl=pl, segment_info=segment_info, hide_user='abc'), [ - {'contents': 'def', 'highlight_group': 'user'} + {'contents': 'def', 'highlight_group': ['user']} ]) self.assertEqual(common.user(pl=pl, segment_info=segment_info, hide_user='def'), None) with replace_attr(common, '_geteuid', lambda: 0):