From d3fc648c08acf6798a5176f29aeed344fd6e8892 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sun, 16 Feb 2020 21:15:14 +0100 Subject: [PATCH] [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. --- core/theme.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/core/theme.py b/core/theme.py index 473f11b..01b6fc6 100644 --- a/core/theme.py +++ b/core/theme.py @@ -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