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
This commit is contained in:
ZyX 2013-02-18 08:31:00 +04:00 committed by Kim Silkebækken
parent 2fa64c42d1
commit 186ad2d423
2 changed files with 35 additions and 24 deletions

View File

@ -4,35 +4,44 @@ from importlib import import_module
import sys import sys
class Segment(object): def get_function(data, segment):
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(self, segment):
oldpath = sys.path oldpath = sys.path
sys.path = self.path + sys.path sys.path = data['path'] + sys.path
segment_module = str(segment.get('module', self.default_module)) segment_module = str(segment.get('module', data['default_module']))
try: try:
return None, getattr(import_module(segment_module), segment['name']), '{0}.{1}'.format(segment_module, segment['name']) return None, getattr(import_module(segment_module), segment['name']), '{0}.{1}'.format(segment_module, segment['name'])
finally: finally:
sys.path = oldpath sys.path = oldpath
@staticmethod
def get_string(segment): def get_string(data, segment):
return segment.get('contents'), None, None return segment.get('contents'), None, None
@staticmethod
def get_filler(segment): def get_filler(data, segment):
return None, None, None return None, None, None
def get(self, segment, side):
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') segment_type = segment.get('type', 'function')
try: try:
get_segment_info = getattr(self, 'get_{0}'.format(segment_type)) get_segment_info = segment_getters[segment_type]
except AttributeError: except KeyError:
raise TypeError('Unknown segment type: {0}'.format(segment_type)) 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')) highlight_group = segment.get('highlight_group', segment.get('name'))
divider_highlight_group = segment.get('divider_highlight_group') divider_highlight_group = segment.get('divider_highlight_group')
return { return {
@ -58,3 +67,5 @@ class Segment(object):
'_space_left': 0, '_space_left': 0,
'_space_right': 0, '_space_right': 0,
} }
return get

View File

@ -3,7 +3,7 @@
from collections import defaultdict from collections import defaultdict
from copy import copy from copy import copy
from .segment import Segment from .segment import gen_segment_getter
try: try:
@ -31,7 +31,7 @@ class Theme(object):
'highlight': defaultdict(lambda: {'fg': False, 'bg': False, 'attr': 0}) 'highlight': defaultdict(lambda: {'fg': False, 'bg': False, 'attr': 0})
} }
self.segment_info = segment_info 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']: for side in ['left', 'right']:
self.segments[side].extend((get_segment(segment, side) for segment in theme_config['segments'].get(side, []))) self.segments[side].extend((get_segment(segment, side) for segment in theme_config['segments'].get(side, [])))