From 3638aa24203138476a2a5c8a02bffe9bdc7f27f5 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sun, 7 Jan 2018 20:27:11 +0100 Subject: [PATCH] [modules/various] Use new interval mechanism Use generic interval mechanism in most of the modules that use slow updates. Only exception: getcrypto, as the interval is specified in seconds there and I want to retain backwards-compatibility. fixes #220 --- bumblebee/modules/currency.py | 22 +++++++---------- bumblebee/modules/dnf.py | 8 ++---- bumblebee/modules/github.py | 33 +++++++++++-------------- bumblebee/modules/hipchat.py | 20 ++++++--------- bumblebee/modules/ping.py | 4 +-- bumblebee/modules/stock.py | 1 + bumblebee/modules/weather.py | 46 +++++++++++++++-------------------- 7 files changed, 55 insertions(+), 79 deletions(-) diff --git a/bumblebee/modules/currency.py b/bumblebee/modules/currency.py index 2fd466f..3412ff2 100644 --- a/bumblebee/modules/currency.py +++ b/bumblebee/modules/currency.py @@ -20,7 +20,6 @@ Note: source and destination names right now must correspond to the names used b import bumblebee.input import bumblebee.output import bumblebee.engine -import time try: import requests except ImportError: @@ -38,7 +37,7 @@ class Module(bumblebee.engine.Module): bumblebee.output.Widget(full_text=self.price) ) self._data = [] - self._interval = int(self.parameter("interval", 1)) + self.interval(1) self._base = self.parameter("source", "GBP") self._symbols = self.parameter("destination", "USD,EUR").split(",") self._nextcheck = 0 @@ -57,16 +56,13 @@ class Module(bumblebee.engine.Module): return basefmt.format(SYMBOL[self._base] if self._base in SYMBOL else self._base, ratefmt.join(rates)) def update(self, widgets): - timestamp = int(time.time()) - if self._nextcheck < timestamp: - self._data = [] - self._nextcheck = timestamp + self._interval*60 - for symbol in self._symbols: - url = API_URL.format(self._base, symbol) - try: - response = requests.get(url).json() - self._data.append((symbol, response['data']['exchangeRate'])) - except Exception: - pass + self._data = [] + for symbol in self._symbols: + url = API_URL.format(self._base, symbol) + try: + response = requests.get(url).json() + self._data.append((symbol, response['data']['exchangeRate'])) + except Exception: + pass # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/dnf.py b/bumblebee/modules/dnf.py index f36dfa9..3942156 100644 --- a/bumblebee/modules/dnf.py +++ b/bumblebee/modules/dnf.py @@ -6,11 +6,10 @@ Requires the following executable: * dnf Parameters: - * dnf.interval: Time in seconds between two consecutive update checks (defaulst to 1800) + * dnf.interval: Time in seconds between two consecutive update checks (defaulst to 30 minutes) """ -import time import threading import bumblebee.util @@ -53,7 +52,7 @@ class Module(bumblebee.engine.Module): def __init__(self, engine, config): widget = bumblebee.output.Widget(full_text=self.updates) super(Module, self).__init__(engine, config, widget) - self._next_check = 0 + self.interval(30) def updates(self, widget): result = [] @@ -62,11 +61,8 @@ class Module(bumblebee.engine.Module): return "/".join(result) def update(self, widgets): - if int(time.time()) < self._next_check: - return thread = threading.Thread(target=get_dnf_info, args=(widgets[0],)) thread.start() - self._next_check = int(time.time()) + self.parameter("interval", 30*60) def state(self, widget): cnt = 0 diff --git a/bumblebee/modules/github.py b/bumblebee/modules/github.py index d9a5576..f5be42c 100644 --- a/bumblebee/modules/github.py +++ b/bumblebee/modules/github.py @@ -10,7 +10,6 @@ Parameters: * github.interval: Interval in minutes """ -import time import functools import bumblebee.input import bumblebee.output @@ -27,8 +26,7 @@ class Module(bumblebee.engine.Module): bumblebee.output.Widget(full_text=self.github) ) self._count = 0 - self._interval = int(self.parameter("interval", "5")) - self._nextcheck = 0 + self.interval(5) self._requests = requests.Session() self._requests.headers.update({"Authorization":"token {}".format(self.parameter("token", ""))}) engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, @@ -41,23 +39,20 @@ class Module(bumblebee.engine.Module): return str(self._count) def update(self, _, immediate=False): - if immediate or self._nextcheck < int(time.time()): - self._nextcheck = int(time.time()) + self._interval * 60 + try: + self._count = 0 + url = "https://api.github.com/notifications" + while True: + notifications = self._requests.get(url) + self._count += len(list(filter(lambda notification: notification['unread'], notifications.json()))) + next_link = notifications.links.get('next') + if next_link is not None: + url = next_link.get('url') + else: + break - try: - self._count = 0 - url = "https://api.github.com/notifications" - while True: - notifications = self._requests.get(url) - self._count += len(list(filter(lambda notification: notification['unread'], notifications.json()))) - next_link = notifications.links.get('next') - if next_link is not None: - url = next_link.get('url') - else: - break - - except Exception: - self._count = "n/a" + except Exception: + self._count = "n/a" # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/hipchat.py b/bumblebee/modules/hipchat.py index b360d2b..b85edc2 100644 --- a/bumblebee/modules/hipchat.py +++ b/bumblebee/modules/hipchat.py @@ -8,7 +8,6 @@ Parameters: * hipchat.interval: Refresh interval in minutes (defaults to 5) """ -import time import functools import bumblebee.input import bumblebee.output @@ -27,8 +26,7 @@ class Module(bumblebee.engine.Module): bumblebee.output.Widget(full_text=self.output) ) self._count = 0 - self._interval = int(self.parameter("interval", "5")) - self._nextcheck = 0 + self.interval(5) self._requests = requests.Session() self._requests.headers.update({"Authorization":"Bearer {}".format(self.parameter("token", ""))}) @@ -40,16 +38,12 @@ class Module(bumblebee.engine.Module): return str(self._count) def update(self, _, immediate=False): - if immediate or self._nextcheck < int(time.time()): - self._nextcheck = int(time.time()) + self._interval * 60 - - try: - self._count = 0 - items = self._requests.get(HIPCHAT_API_URL).json().get('items') - self._count = sum([item.get('unreadCount').get('count') for item in items]) - - except Exception: - self._count = "n/a" + try: + self._count = 0 + items = self._requests.get(HIPCHAT_API_URL).json().get('items') + self._count = sum([item.get('unreadCount').get('count') for item in items]) + except Exception: + self._count = "n/a" # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/ping.py b/bumblebee/modules/ping.py index 0a1419b..5619b18 100644 --- a/bumblebee/modules/ping.py +++ b/bumblebee/modules/ping.py @@ -6,7 +6,7 @@ Requires the following executable: * ping Parameters: - * ping.interval: Time in seconds between two RTT checks (defaults to 60) + * ping.ping_interval: Time in seconds between two RTT checks (defaults to 60) * ping.address : IP address to check * ping.timeout : Timeout for waiting for a reply (defaults to 5.0) * ping.probes : Number of probes to send (defaults to 5) @@ -51,7 +51,7 @@ class Module(bumblebee.engine.Module): super(Module, self).__init__(engine, config, widget) widget.set("address", self.parameter("address", "8.8.8.8")) - widget.set("interval", self.parameter("interval", 60)) + widget.set("interval", self.parameter("ping_interval", 60)) widget.set("rtt-probes", self.parameter("probes", 5)) widget.set("rtt-timeout", self.parameter("timeout", 5.0)) widget.set("rtt-avg", 0.0) diff --git a/bumblebee/modules/stock.py b/bumblebee/modules/stock.py index a93cf78..6eb4b64 100644 --- a/bumblebee/modules/stock.py +++ b/bumblebee/modules/stock.py @@ -31,6 +31,7 @@ class Module(bumblebee.engine.Module): self._currencies = self.parameter('currencies', None) self._baseurl = 'http://download.finance.yahoo.com/d/quotes.csv' self._value = self.fetch() + self.interval(60) if not self._currencies: self._currencies = '$' * len(self._symbols) diff --git a/bumblebee/modules/weather.py b/bumblebee/modules/weather.py index 9838a0f..8233db0 100644 --- a/bumblebee/modules/weather.py +++ b/bumblebee/modules/weather.py @@ -7,7 +7,6 @@ Requires the following python packages: * requests Parameters: - * weather.interval: Interval (in minutes) for updating weather information * weather.location: Set location (ISO 3166 country code), defaults to 'auto' for getting location from http://ipinfo.io * weather.unit: metric (default), kelvin, imperial * weather.apikey: API key from http://api.openweathermap.org @@ -18,7 +17,6 @@ import bumblebee.output import bumblebee.engine import re import json -import time try: import requests from requests.exceptions import RequestException @@ -34,10 +32,9 @@ class Module(bumblebee.engine.Module): self._apikey = self.parameter("apikey", "af7bfe22287c652d032a3064ffa44088") self._location = self.parameter("location", "auto") self._city = self.parameter("location", "") - self._interval = int(self.parameter("interval", "15")) self._unit = self.parameter("unit", "metric") - self._nextcheck = 0 self._valid = False + self.interval(15) def _unit_suffix(self): if self._unit == "metric": @@ -82,27 +79,24 @@ class Module(bumblebee.engine.Module): return [] def update(self, widgets): - timestamp = int(time.time()) - if self._nextcheck < timestamp: - try: - self._nextcheck = timestamp + self._interval*60 - weather_url = "http://api.openweathermap.org/data/2.5/weather?appid={}".format(self._apikey) - weather_url = "{}&units={}".format(weather_url, self._unit) - if self._location == "auto": - location_url = "http://ipinfo.io/json" - location = json.loads(requests.get(location_url).text) - coord = location["loc"].split(",") - self._city = location["city"] - weather_url = "{url}&lat={lat}&lon={lon}".format(url=weather_url, lat=coord[0], lon=coord[1]) - else: - weather_url = "{url}&q={city}".format(url=weather_url, city=self._location) - weather = json.loads(requests.get(weather_url).text) - self._temperature = int(weather['main']['temp']) - self._weather = weather['weather'][0]['main'].lower() - self._valid = True - except RequestException: - self._valid = False - except Exception: - self._valid = False + try: + weather_url = "http://api.openweathermap.org/data/2.5/weather?appid={}".format(self._apikey) + weather_url = "{}&units={}".format(weather_url, self._unit) + if self._location == "auto": + location_url = "http://ipinfo.io/json" + location = json.loads(requests.get(location_url).text) + coord = location["loc"].split(",") + self._city = location["city"] + weather_url = "{url}&lat={lat}&lon={lon}".format(url=weather_url, lat=coord[0], lon=coord[1]) + else: + weather_url = "{url}&q={city}".format(url=weather_url, city=self._location) + weather = json.loads(requests.get(weather_url).text) + self._temperature = int(weather['main']['temp']) + self._weather = weather['weather'][0]['main'].lower() + self._valid = True + except RequestException: + self._valid = False + except Exception: + self._valid = False # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4