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:
parent
2fa64c42d1
commit
186ad2d423
|
@ -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
|
||||
|
|
|
@ -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, [])))
|
||||
|
||||
|
|
Loading…
Reference in New Issue