From a6b7e329c7badb57c0b9c6aef4e9bfdd18d5da31 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sun, 7 Jan 2018 19:37:31 +0100 Subject: [PATCH] [module/stock] Error message if stock service unavailable If the stock rate cannot be retrieved for some reason, print an error message in the log and present the value as "n/a". fixes #219 --- bumblebee/modules/stock.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/bumblebee/modules/stock.py b/bumblebee/modules/stock.py index 28b5162..a93cf78 100644 --- a/bumblebee/modules/stock.py +++ b/bumblebee/modules/stock.py @@ -19,13 +19,15 @@ import bumblebee.util import requests +import logging + class Module(bumblebee.engine.Module): def __init__(self, engine, config): super(Module, self).__init__(engine, config, bumblebee.output.Widget(full_text=self.value) ) - self._symbols = self.parameter("symbols", "") - self._change = bumblebee.util.asbool(self.parameter("change", True)) + self._symbols = self.parameter('symbols', '') + self._change = bumblebee.util.asbool(self.parameter('change', True)) self._currencies = self.parameter('currencies', None) self._baseurl = 'http://download.finance.yahoo.com/d/quotes.csv' self._value = self.fetch() @@ -34,11 +36,13 @@ class Module(bumblebee.engine.Module): self._currencies = '$' * len(self._symbols) # The currencies could be unicode, like the € symbol. Convert to a unicode object. - if hasattr(self._currencies, "decode"): - self._currencies = self._currencies.decode("utf-8", "ignore") + if hasattr(self._currencies, 'decode'): + self._currencies = self._currencies.decode('utf-8', 'ignore') def value(self, widget): results = [] + if not self._value: + return 'n/a' for i, val in enumerate(self._value.split('\n')): try: currency_symbol = self._currencies[i] @@ -55,7 +59,8 @@ class Module(bumblebee.engine.Module): url += 'c1' return requests.get(url).text.strip() else: - return '' + logging.error('unable to retrieve stock exchange rate') + return None def update(self, widgets): self._value = self.fetch()