From 776be11137cae3db8fbc55a4f98b051d993fff0a Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sun, 7 Jan 2018 20:25:32 +0100 Subject: [PATCH] [engine] Add generic interval mechanism Add a generic mechanism to set a specific interval (in minutes, as this is primarily intended for modules that want to "slow down" updates). This gives *all* modules the parameter "interval" and allows each module to set the default interval using the method "interval() in the module constructor. see #220 --- bumblebee/engine.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/bumblebee/engine.py b/bumblebee/engine.py index e281aeb..df5b0da 100644 --- a/bumblebee/engine.py +++ b/bumblebee/engine.py @@ -2,6 +2,7 @@ import os import json +import time import pkgutil import logging import importlib @@ -37,6 +38,8 @@ class Module(object): self.name = config.get("name", self.__module__.split(".")[-1]) self._config = config self.id = self.name + self._next = int(time.time()) + self._default_interval = 0 self._configFile = None for cfg in [os.path.expanduser("~/.bumblebee-status.conf"), os.path.expanduser("~/.config/bumblebee-status.conf")]: @@ -74,8 +77,18 @@ class Module(object): """By default, update() is a NOP""" pass - def update_all(self): + def update_wrapper(self, widgets): + if self._next > int(time.time()): + return self.update(self._widgets) + self._next += int(self.parameter("interval", self._default_interval))*60 + + def interval(self, intvl): + self._default_interval = intvl + self._next = int(time.time()) + + def update_all(self): + self.update_wrapper(self._widgets) def has_parameter(self, name): v = self.parameter(name) @@ -230,7 +243,7 @@ class Engine(object): self._output.begin() for module in self._modules: self._current_module = module - module.update(module.widgets()) + module.update_wrapper(module.widgets()) for widget in module.widgets(): widget.link_module(module) self._output.draw(widget=widget, module=module, engine=self)