From 5071f127a82b1e43226f788ba5ea04f463fb7faf Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 3 Feb 2014 21:56:16 +0400 Subject: [PATCH] Recreate args dictionary with str() arguments, use closures Fixes #776 --- powerline/segment.py | 23 ++++++++++++++++++++--- powerline/theme.py | 8 ++------ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/powerline/segment.py b/powerline/segment.py index c96f1c0e..4afe37a3 100644 --- a/powerline/segment.py +++ b/powerline/segment.py @@ -63,7 +63,7 @@ def gen_segment_getter(pl, ext, path, theme_configs, default_module=None): raise TypeError('Unknown segment type: {0}'.format(segment_type)) try: - contents, contents_func, module = get_segment_info(data, segment) + contents, _contents_func, module = get_segment_info(data, segment) except Exception as e: pl.exception('Failed to generate segment from {0!r}: {1}', segment, str(e), prefix='segment_generator') return None @@ -73,6 +73,23 @@ def gen_segment_getter(pl, ext, path, theme_configs, default_module=None): else: highlight_group = segment.get('highlight_group') or segment.get('name') + if segment_type == 'function': + args = dict(((str(k), v) for k, v in get_key(segment, module, 'args', {}).items())) + try: + _startup_func = _contents_func.startup + except AttributeError: + startup_func = None + else: + startup_func = lambda pl, shutdown_event: _startup_func(pl=pl, shutdown_event=shutdown_event, **args) + + if hasattr(_contents_func, 'powerline_requires_segment_info'): + contents_func = lambda pl, segment_info: _contents_func(pl=pl, segment_info=segment_info, **args) + else: + contents_func = lambda pl, segment_info: _contents_func(pl=pl, **args) + else: + startup_func = None + contents_func = None + return { 'name': segment.get('name'), 'type': segment_type, @@ -82,7 +99,7 @@ def gen_segment_getter(pl, ext, path, theme_configs, default_module=None): 'after': get_key(segment, module, 'after', ''), 'contents_func': contents_func, 'contents': contents, - 'args': get_key(segment, module, 'args', {}) if segment_type == 'function' else {}, + 'args': args if segment_type == 'function' else {}, 'priority': segment.get('priority', None), 'draw_hard_divider': segment.get('draw_hard_divider', True), 'draw_soft_divider': segment.get('draw_soft_divider', True), @@ -93,7 +110,7 @@ def gen_segment_getter(pl, ext, path, theme_configs, default_module=None): 'width': segment.get('width'), 'align': segment.get('align', 'l'), 'shutdown': getattr(contents_func, 'shutdown', None), - 'startup': getattr(contents_func, 'startup', None), + 'startup': startup_func, '_rendered_raw': '', '_rendered_hl': '', '_len': 0, diff --git a/powerline/theme.py b/powerline/theme.py index 56a2a1bb..0e76727c 100644 --- a/powerline/theme.py +++ b/powerline/theme.py @@ -51,7 +51,7 @@ class Theme(object): if not run_once: if segment['startup']: try: - segment['startup'](pl=pl, shutdown_event=shutdown_event, **segment['args']) + segment['startup'](pl, shutdown_event) except Exception as e: pl.error('Exception during {0} startup: {1}', segment['name'], str(e)) continue @@ -84,11 +84,7 @@ class Theme(object): if segment['type'] == 'function': self.pl.prefix = segment['name'] try: - if (hasattr(segment['contents_func'], 'powerline_requires_segment_info') - and segment['contents_func'].powerline_requires_segment_info): - contents = segment['contents_func'](pl=self.pl, segment_info=segment_info, **segment['args']) - else: - contents = segment['contents_func'](pl=self.pl, **segment['args']) + contents = segment['contents_func'](self.pl, segment_info) except Exception as e: self.pl.exception('Exception while computing segment: {0}', str(e)) continue