[core/theme] Dynamically create theme accessors

Dynamically generate the accessors for a theme's attributes.

NB: This might be even nicer coming from a JSON rather than inside the
code.
This commit is contained in:
Tobias Witek 2020-02-16 21:15:14 +01:00
parent 6143c5e5a1
commit d3fc648c08

View file

@ -22,6 +22,14 @@ class Theme(object):
core.event.register('start', self.__start)
core.event.register('next-widget', self.__next_widget)
for attr, default in [
('fg', None), ('bg', None),
('default-separators', True),
('separator-block-width', 0),
('separator', None)
]:
setattr(self, attr.replace('-', '_'), lambda widget=None, default=default, attr=attr: self.__get(widget, attr, default))
def load(self, name):
for path in PATHS:
theme_file = os.path.join(path, '{}.json'.format(name))
@ -30,18 +38,6 @@ class Theme(object):
return json.load(data)
raise RuntimeError('unable to find theme {}'.format(name))
def fg(self, widget=None):
return self.__get(widget, 'fg')
def bg(self, widget=None):
return self.__get(widget, 'bg')
def default_separators(self, widget=None):
return self.__get(widget, 'default-separators', True)
def separator_block_width(self, widget=None):
return self.__get(widget, 'separator-block-width', 0)
def __start(self):
self.__widget_count = 0