[core] Remove "intelligent" theme accessors

To simplify code, remove the auto-generated theme accessors for
attributes, and instead use a generic "get" method.
This commit is contained in:
tobi-wan-kenobi 2020-04-07 20:41:09 +02:00
parent f32affa563
commit c0cc1ccd75
4 changed files with 26 additions and 38 deletions

View file

@ -34,7 +34,7 @@ class block(object):
self.__attributes['name'] = module.id self.__attributes['name'] = module.id
self.__attributes['instance'] = widget.id self.__attributes['instance'] = widget.id
self.__attributes['prev-bg'] = theme.get('bg', 'previous') self.__attributes['prev-bg'] = theme.get('prev-bg')
def set(self, key, value): def set(self, key, value):
self.__attributes[key] = value self.__attributes[key] = value

View file

@ -45,21 +45,6 @@ class Theme(object):
core.event.register('update', self.__start) core.event.register('update', self.__start)
core.event.register('next-widget', self.__next_widget) 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),
('border-top', 0),
('border-bottom', 0),
('border-left', 0),
('border-right', 0),
('padding', ''),
('prefix', ''), ('suffix', ''),
('pango', None),
]:
setattr(self, attr.replace('-', '_'), lambda widget=None, default=default, attr=attr: self.__get(widget, attr, default))
def keywords(self): def keywords(self):
return self.__keywords return self.__keywords
@ -101,9 +86,6 @@ class Theme(object):
self.__current.clear() self.__current.clear()
def get(self, key, widget=None, default=None): def get(self, key, widget=None, default=None):
return self.__get(widget, key, default)
def __get(self, widget, key, default=None):
if not widget: if not widget:
widget = core.widget.Widget('') widget = core.widget.Widget('')
if isinstance(widget, str): if isinstance(widget, str):
@ -126,11 +108,11 @@ class Theme(object):
value = merge_replace(value, self.__data.get(key, value), key) value = merge_replace(value, self.__data.get(key, value), key)
if widget.module(): if widget.module():
value = merge_replace(value, self.__get(None, widget.module().name(), {}).get(key, value), key) value = merge_replace(value, self.get(widget.module().name(), None, {}).get(key, value), key)
if not key in widget.state(): if not key in widget.state():
for state in widget.state(): for state in widget.state():
theme = self.__get(widget, state, {}) theme = self.get(state, widget, {})
value = merge_replace(value, theme.get(key, value), key) value = merge_replace(value, theme.get(key, value), key)
if not type(value) in (list, dict): if not type(value) in (list, dict):

View file

@ -81,6 +81,12 @@ class i3(unittest.TestCase):
self.assertEqual(1, len(result)) self.assertEqual(1, len(result))
self.assertEqual('***', result[0].dict()['full_text']) self.assertEqual('***', result[0].dict()['full_text'])
self.assertTrue(result[0].dict().get('_decorator', False)) self.assertTrue(result[0].dict().get('_decorator', False))
self.assertEqual(self.separatorTheme.bg(self.someModule.widget()), result[0].dict()['color']) self.assertEqual(self.separatorTheme.get('bg', self.someModule.widget()), result[0].dict()['color'])
def test_dump_json(self):
obj = unittest.mock.MagicMock()
obj.dict = unittest.mock.MagicMock()
core.output.dump_json(obj)
obj.dict_assert_called_once_with()
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -50,32 +50,32 @@ class theme(unittest.TestCase):
def test_defaults(self): def test_defaults(self):
theme = core.theme.Theme(raw_data=self.defaultsTheme) theme = core.theme.Theme(raw_data=self.defaultsTheme)
self.assertEqual(self.defaultsTheme['defaults']['fg'], theme.fg()) self.assertEqual(self.defaultsTheme['defaults']['fg'], theme.get('fg'))
self.assertEqual(self.defaultsTheme['defaults']['bg'], theme.bg()) self.assertEqual(self.defaultsTheme['defaults']['bg'], theme.get('bg'))
def test_cycle(self): def test_cycle(self):
theme = core.theme.Theme(raw_data=self.cycleTheme) theme = core.theme.Theme(raw_data=self.cycleTheme)
self.assertEqual(None, theme.bg('previous')) self.assertEqual(None, theme.get('prev-bg'))
self.assertEqual(self.cycleTheme['cycle'][0]['fg'], theme.fg()) self.assertEqual(self.cycleTheme['cycle'][0]['fg'], theme.get('fg'))
self.assertEqual(self.cycleTheme['cycle'][0]['bg'], theme.bg()) self.assertEqual(self.cycleTheme['cycle'][0]['bg'], theme.get('bg'))
core.event.trigger('next-widget') core.event.trigger('next-widget')
self.assertEqual(self.cycleTheme['cycle'][0]['bg'], theme.bg('previous')) self.assertEqual(self.cycleTheme['cycle'][0]['bg'], theme.get('bg', 'previous'))
core.event.trigger('next-widget') core.event.trigger('next-widget')
self.assertEqual(self.cycleTheme['cycle'][2]['fg'], theme.fg()) self.assertEqual(self.cycleTheme['cycle'][2]['fg'], theme.get('fg'))
self.assertEqual(self.cycleTheme['cycle'][2]['bg'], theme.bg()) self.assertEqual(self.cycleTheme['cycle'][2]['bg'], theme.get('bg'))
with unittest.mock.patch('core.output.sys.stdout'): with unittest.mock.patch('core.output.sys.stdout'):
core.event.trigger('update') core.event.trigger('update')
self.assertEqual(self.cycleTheme['cycle'][0]['fg'], theme.fg()) self.assertEqual(self.cycleTheme['cycle'][0]['fg'], theme.get('fg'))
self.assertEqual(self.cycleTheme['cycle'][0]['bg'], theme.bg()) self.assertEqual(self.cycleTheme['cycle'][0]['bg'], theme.get('bg'))
def test_custom_iconset(self): def test_custom_iconset(self):
theme = core.theme.Theme(raw_data=self.defaultsTheme) theme = core.theme.Theme(raw_data=self.defaultsTheme)
self.assertNotEqual('aaa', theme.padding()) self.assertNotEqual('aaa', theme.get('padding'))
theme = core.theme.Theme(raw_data=self.defaultsTheme, iconset={ theme = core.theme.Theme(raw_data=self.defaultsTheme, iconset={
'defaults': { 'padding': 'aaa' } 'defaults': { 'padding': 'aaa' }
}) })
self.assertEqual('aaa', theme.padding()) self.assertEqual('aaa', theme.get('padding'))
def test_colors(self): def test_colors(self):
theme = core.theme.Theme(raw_data=self.defaultsTheme) theme = core.theme.Theme(raw_data=self.defaultsTheme)
@ -107,18 +107,18 @@ class theme(unittest.TestCase):
theme = core.theme.Theme(raw_data=self.cycleValueTheme) theme = core.theme.Theme(raw_data=self.cycleValueTheme)
for i in range(0, len(expected)*3): for i in range(0, len(expected)*3):
self.assertEqual(expected[i%len(expected)], theme.fg(widget)) self.assertEqual(expected[i%len(expected)], theme.get('fg', widget))
def test_state(self): def test_state(self):
widget = core.widget.Widget() widget = core.widget.Widget()
theme = core.theme.Theme(raw_data=self.stateTheme) theme = core.theme.Theme(raw_data=self.stateTheme)
self.assertEqual(None, theme.fg(widget)) self.assertEqual(None, theme.get('fg', widget))
widget.state = types.MethodType(lambda self: ['warning'], widget) widget.state = types.MethodType(lambda self: ['warning'], widget)
self.assertEqual(self.stateTheme['warning']['fg'], theme.fg(widget)) self.assertEqual(self.stateTheme['warning']['fg'], theme.get('fg', widget))
widget.state = types.MethodType(lambda self: ['critical'], widget) widget.state = types.MethodType(lambda self: ['critical'], widget)
self.assertEqual(self.stateTheme['critical']['fg'], theme.fg(widget)) self.assertEqual(self.stateTheme['critical']['fg'], theme.get('fg', widget))
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4