diff --git a/powerline/segment.py b/powerline/segment.py index 4afe37a3..a085da2e 100644 --- a/powerline/segment.py +++ b/powerline/segment.py @@ -45,6 +45,18 @@ segment_getters = { } +def get_attr_func(contents_func, key, kwargs): + try: + func = getattr(contents_func, key) + except AttributeError: + return None + else: + if kwargs is None: + return lambda : func() + else: + return lambda pl, shutdown_event: func(pl=pl, shutdown_event=shutdown_event, **kwargs) + + def gen_segment_getter(pl, ext, path, theme_configs, default_module=None): data = { 'default_module': default_module or 'powerline.segments.' + ext, @@ -74,20 +86,17 @@ def gen_segment_getter(pl, ext, path, theme_configs, default_module=None): 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) + kwargs = dict(((str(k), v) for k, v in get_key(segment, module, 'args', {}).items())) + startup_func = get_attr_func(_contents_func, 'startup', kwargs) + shutdown_func = get_attr_func(_contents_func, 'shutdown', None) if hasattr(_contents_func, 'powerline_requires_segment_info'): - contents_func = lambda pl, segment_info: _contents_func(pl=pl, segment_info=segment_info, **args) + contents_func = lambda pl, segment_info: _contents_func(pl=pl, segment_info=segment_info, **kwargs) else: - contents_func = lambda pl, segment_info: _contents_func(pl=pl, **args) + contents_func = lambda pl, segment_info: _contents_func(pl=pl, **kwargs) else: startup_func = None + shutdown_func = None contents_func = None return { @@ -109,8 +118,8 @@ def gen_segment_getter(pl, ext, path, theme_configs, default_module=None): 'include_modes': segment.get('include_modes', []), 'width': segment.get('width'), 'align': segment.get('align', 'l'), - 'shutdown': getattr(contents_func, 'shutdown', None), 'startup': startup_func, + 'shutdown': shutdown_func, '_rendered_raw': '', '_rendered_hl': '', '_len': 0,