[core/modules] Add widget producer for module

simplifies and debugs widget creation
This commit is contained in:
tobi-wan-kenobi 2020-05-01 10:03:50 +02:00
parent 859a387676
commit 2a663deb7b
2 changed files with 16 additions and 14 deletions

View file

@ -73,6 +73,11 @@ class Module(core.input.Object):
self.__widgets = widgets self.__widgets = widgets
return self.__widgets return self.__widgets
def add_widget(self, full_text='', name=None):
widget = core.widget.Widget(full_text=full_text, name=name, module=self)
self.widgets().append(widget)
return widget
def widget(self, name=None): def widget(self, name=None):
if not name: return self.widgets()[0] if not name: return self.widgets()[0]

View file

@ -34,7 +34,7 @@ class Module(core.module.Module):
self.__data = {} self.__data = {}
self.__update() self.__update()
self.widgets(self.__create_widgets()) self.__create_widgets()
def update(self): def update(self):
self.__update() self.__update()
@ -54,7 +54,6 @@ class Module(core.module.Module):
return [widget_type] return [widget_type]
def __create_widgets(self): def __create_widgets(self):
widgets = []
show_temp = util.format.asbool(self.parameter('showtemp', True)) show_temp = util.format.asbool(self.parameter('showtemp', True))
show_fan = util.format.asbool(self.parameter('showfan', True)) show_fan = util.format.asbool(self.parameter('showfan', True))
show_other = util.format.asbool(self.parameter('showother', False)) show_other = util.format.asbool(self.parameter('showother', False))
@ -66,9 +65,8 @@ class Module(core.module.Module):
exclude_chip_field = tuple(filter(len, util.format.aslist(self.parameter('chip_field_exclude', '')))) exclude_chip_field = tuple(filter(len, util.format.aslist(self.parameter('chip_field_exclude', ''))))
if util.format.asbool(self.parameter('showcpu', True)): if util.format.asbool(self.parameter('showcpu', True)):
widget = core.widget.Widget(full_text=self.__cpu, module=self) widget = self.add_widget(full_text=self.__cpu)
widget.set('type', 'cpu') widget.set('type', 'cpu')
widgets.append(widget)
for adapter in self.__data: for adapter in self.__data:
if include_chip or exclude_chip: if include_chip or exclude_chip:
@ -88,12 +86,11 @@ class Module(core.module.Module):
for package in self.__data[adapter]: for package in self.__data[adapter]:
if util.format.asbool(self.parameter('showname', False)): if util.format.asbool(self.parameter('showname', False)):
widget = core.widget.Widget(full_text=package, module=self) widget = self.add_widget(full_text=package)
widget.set('data', self.__data[adapter][package]) widget.set('data', self.__data[adapter][package])
widget.set('package', package) widget.set('package', package)
widget.set('field', '') widget.set('field', '')
widget.set('adapter', adapter) widget.set('adapter', adapter)
widgets.append(widget)
for field in self.__data[adapter][package]: for field in self.__data[adapter][package]:
if include_field or exclude_field: if include_field or exclude_field:
@ -115,23 +112,23 @@ class Module(core.module.Module):
except: except:
pass pass
widget = core.widget.Widget(module=self) widget = None
widget.set('package', package)
widget.set('field', field)
widget.set('adapter', adapter)
if 'temp' in field and show_temp: if 'temp' in field and show_temp:
# seems to be a temperature # seems to be a temperature
widget = self.add_widget()
widget.set('type', 'temp') widget.set('type', 'temp')
widgets.append(widget)
if 'fan' in field and show_fan: if 'fan' in field and show_fan:
# seems to be a fan # seems to be a fan
widget = self.add_widget()
widget.set('type', 'fan') widget.set('type', 'fan')
widgets.append(widget)
elif show_other: elif show_other:
# everything else # everything else
widget = self.add_widget()
widget.set('type', 'other') widget.set('type', 'other')
widgets.append(widget) if widget:
return widgets widget.set('package', package)
widget.set('field', field)
widget.set('adapter', adapter)
def __update_widget(self, widget): def __update_widget(self, widget):
if widget.get('field', '') == '': if widget.get('field', '') == '':