From 186ad2d423677de0d95cc9a1346c3b671d94e053 Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 18 Feb 2013 08:31:00 +0400 Subject: [PATCH] Remove useless Segment class It was used solely for Segment.get (the only place where it was used just saved its .get() method, throwing away instance itself) and looks more like abusal of the purpose of the classes --- powerline/segment.py | 55 ++++++++++++++++++++++++++------------------ powerline/theme.py | 4 ++-- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/powerline/segment.py b/powerline/segment.py index a843a46b..999195e1 100644 --- a/powerline/segment.py +++ b/powerline/segment.py @@ -4,35 +4,44 @@ from importlib import import_module import sys -class Segment(object): - def __init__(self, ext, path, default_module=None): - self.default_module = default_module or 'powerline.segments.{0}'.format(ext) - self.path = path +def get_function(data, segment): + oldpath = sys.path + sys.path = data['path'] + sys.path + segment_module = str(segment.get('module', data['default_module'])) + try: + return None, getattr(import_module(segment_module), segment['name']), '{0}.{1}'.format(segment_module, segment['name']) + finally: + sys.path = oldpath - def get_function(self, segment): - oldpath = sys.path - sys.path = self.path + sys.path - segment_module = str(segment.get('module', self.default_module)) - try: - return None, getattr(import_module(segment_module), segment['name']), '{0}.{1}'.format(segment_module, segment['name']) - finally: - sys.path = oldpath - @staticmethod - def get_string(segment): - return segment.get('contents'), None, None +def get_string(data, segment): + return segment.get('contents'), None, None - @staticmethod - def get_filler(segment): - return None, None, None - def get(self, segment, side): +def get_filler(data, segment): + return None, None, None + + +segment_getters = { + "function": get_function, + "string": get_string, + "filler": get_filler, + } + + +def gen_segment_getter(ext, path, default_module=None): + data = { + "default_module": default_module or 'powerline.segments.'+ext, + "path": path + } + + def get(segment, side): segment_type = segment.get('type', 'function') try: - get_segment_info = getattr(self, 'get_{0}'.format(segment_type)) - except AttributeError: + get_segment_info = segment_getters[segment_type] + except KeyError: raise TypeError('Unknown segment type: {0}'.format(segment_type)) - contents, contents_func, key = get_segment_info(segment) + contents, contents_func, key = get_segment_info(data, segment) highlight_group = segment.get('highlight_group', segment.get('name')) divider_highlight_group = segment.get('divider_highlight_group') return { @@ -58,3 +67,5 @@ class Segment(object): '_space_left': 0, '_space_right': 0, } + + return get diff --git a/powerline/theme.py b/powerline/theme.py index 187c87d1..d6d98fe6 100644 --- a/powerline/theme.py +++ b/powerline/theme.py @@ -3,7 +3,7 @@ from collections import defaultdict from copy import copy -from .segment import Segment +from .segment import gen_segment_getter try: @@ -31,7 +31,7 @@ class Theme(object): 'highlight': defaultdict(lambda: {'fg': False, 'bg': False, 'attr': 0}) } self.segment_info = segment_info - get_segment = Segment(ext, common_config['paths'], theme_config.get('default_module')).get + get_segment = gen_segment_getter(ext, common_config['paths'], theme_config.get('default_module')) for side in ['left', 'right']: self.segments[side].extend((get_segment(segment, side) for segment in theme_config['segments'].get(side, [])))