summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorinfirit <[email protected]>2014-02-08 16:56:25 +0100
committerinfirit <[email protected]>2014-02-08 17:26:53 +0100
commitc36ff1bb4e59d9afcb8b845a411d21d2b53aaf69 (patch)
treeae41f430c1ee76ce0c9be0557370b708728a1ae1
parentbc75ed06615f2eaaf8cc2846112fd71dcdc5210c (diff)
downloadmate-applets-c36ff1bb4e59d9afcb8b845a411d21d2b53aaf69.tar.bz2
mate-applets-c36ff1bb4e59d9afcb8b845a411d21d2b53aaf69.tar.xz
invest-applet: downloaded quotes get cached
- this allows to show most recently retrieved quotes on next start up in case the system is offline by that time Author: Enrico Minack <[email protected]> Gnome commit: 65212e8cd3a101e69fc9f7877450ab5198af9681 Url: https://git.gnome.org/browse/gnome-applets/commit/?id=65212e8cd3a101e69fc9f7877450ab5198af9681
-rw-r--r--invest-applet/invest/__init__.py2
-rw-r--r--invest-applet/invest/quotes.py54
2 files changed, 42 insertions, 14 deletions
diff --git a/invest-applet/invest/__init__.py b/invest-applet/invest/__init__.py
index da6a2512..d99328c2 100644
--- a/invest-applet/invest/__init__.py
+++ b/invest-applet/invest/__init__.py
@@ -149,6 +149,8 @@ try:
except Exception, msg:
CONFIG = {} # default configuration
+QUOTES_FILE = join(USER_INVEST_DIR, "quotes.csv")
+
# set default proxy config
PROXY = None
diff --git a/invest-applet/invest/quotes.py b/invest-applet/invest/quotes.py
index 011ce4f2..b6182d07 100644
--- a/invest-applet/invest/quotes.py
+++ b/invest-applet/invest/quotes.py
@@ -1,4 +1,4 @@
-from os.path import join
+from os.path import join, getmtime
from mate_invest.defs import GTK_API_VERSION
import gi
@@ -65,7 +65,7 @@ class QuotesRetriever(Thread, _IdleObject):
quotes_url = QUOTES_URL % {"s": self.tickers}
try:
quotes_file = urlopen(quotes_url, proxies = mate_invest.PROXY)
- self.data = quotes_file.readlines ()
+ self.data = quotes_file.read ()
quotes_file.close ()
except Exception, msg:
mate_invest.debug("Error while retrieving quotes data (url = %s): %s" % (quotes_url, msg))
@@ -86,11 +86,37 @@ class QuoteUpdater(Gtk.ListStore):
self.change_icon_callback = change_icon_callback
self.set_tooltip_callback = set_tooltip_callback
self.set_sort_column_id(1, Gtk.SortType.ASCENDING)
- self.refresh()
+ self.load() # read the last cached quotes file
+ self.refresh() # download a new quotes file, this may fail if disconnected
# tell the network manager to notify me when network status changes
mate_invest.nm.set_statechange_callback(self.nm_state_changed)
+ # loads the cached csv file and its last-modification-time as self.last_updated
+ def load(self):
+ invest.debug("Loading quotes");
+ try:
+ f = open(invest.QUOTES_FILE, 'r')
+ data = f.readlines()
+ f.close()
+
+ self.populate(self.parse_yahoo_csv(csv.reader(data)))
+ self.updated = True
+ self.last_updated = datetime.datetime.fromtimestamp(getmtime(invest.QUOTES_FILE))
+ self.update_tooltip()
+ except Exception, msg:
+ invest.error("Could not load the cached quotes file %s: %s" % (invest.QUOTES_FILE, msg) )
+
+ # stores the csv content on disk so it can be used on next start up
+ def save(self, data):
+ invest.debug("Storing quotes")
+ try:
+ f = open(invest.QUOTES_FILE, 'w')
+ f.write(data)
+ f.close()
+ except Exception, msg:
+ invest.error("Could not save the retrieved quotes file to %s: %s" % (invest.QUOTES_FILE, msg) )
+
def set_update_interval(self, interval):
if self.timeout_id != None:
mate_invest.debug("Canceling refresh timer")
@@ -142,17 +168,14 @@ class QuoteUpdater(Gtk.ListStore):
def on_retriever_completed(self, retriever):
if retriever.retrieved == False:
invest.debug("QuotesRetriever failed");
- tooltip = [_('Invest could not connect to Yahoo! Finance')]
- if self.last_updated != None:
- # Translators: %s is an hour (%H:%M)
- tooltip.append(_('Updated at %s') % self.last_updated.strftime("%H:%M"))
- self.set_tooltip_callback('\n'.join(tooltip))
+ self.update_tooltip(_('Invest could not connect to Yahoo! Finance'))
+
else:
invest.debug("QuotesRetriever completed");
- self.populate(self.parse_yahoo_csv(csv.reader(retriever.data)))
- self.updated = True
- self.last_updated = datetime.datetime.now()
- self.update_tooltip()
+ # cache the retrieved csv file
+ self.save(retriever.data)
+ # load the cache and parse it
+ self.load()
def on_currency_retriever_completed(self, retriever):
if retriever.retrieved == False:
@@ -161,7 +184,7 @@ class QuoteUpdater(Gtk.ListStore):
self.convert_currencies(self.parse_yahoo_csv(csv.reader(retriever.data)))
self.update_tooltip()
- def update_tooltip(self):
+ def update_tooltip(self, msg = None):
tooltip = []
if self.quotes_count > 0:
# Translators: This is share-market jargon. It is the average percentage change of all stock prices. The %s gets replaced with the string value of the change (localized), including the percent sign.
@@ -175,7 +198,10 @@ class QuoteUpdater(Gtk.ListStore):
# Translators: This is share-market jargon. It refers to the total difference between the current price and purchase price for all the shares put together for a particular currency. i.e. How much money would be earned if they were sold right now. The first string is the change value, the second the currency, and the third value is the percentage of the change, formatted using user's locale.
tooltip.append(_('Positions balance: %s %s (%s)') % (balance, currency, change))
- tooltip.append(_('Updated at %s') % self.last_updated.strftime("%H:%M"))
+ if self.last_updated != None:
+ tooltip.append(_('Updated at %s') % self.last_updated.strftime("%H:%M"))
+ if msg != None:
+ tooltip.append(msg)
self.set_tooltip_callback('\n'.join(tooltip))