[gnome-applets] invest-applet: downloaded quotes get cached - this allows to show most recently retrieved quotes on



commit 65212e8cd3a101e69fc9f7877450ab5198af9681
Author: Enrico Minack <enrico-minack gmx de>
Date:   Tue Jun 28 22:18:39 2011 +0200

    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

 invest-applet/invest/__init__.py |    2 +
 invest-applet/invest/quotes.py   |   53 ++++++++++++++++++++++++++++----------
 2 files changed, 41 insertions(+), 14 deletions(-)
---
diff --git a/invest-applet/invest/__init__.py b/invest-applet/invest/__init__.py
index 5f4f914..014d42d 100644
--- a/invest-applet/invest/__init__.py
+++ b/invest-applet/invest/__init__.py
@@ -164,6 +164,8 @@ except Exception, msg:
 	error("Could not load the configuration from %s: %s" % (CONFIG_FILE, 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 ed85dcf..4d09426 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 gi.repository import GObject, Gtk, Gdk, GdkPixbuf, GConf, PanelApplet
 from gettext import gettext as _
 import csv
@@ -58,7 +58,7 @@ class QuotesRetriever(Thread, _IdleObject):
 		quotes_url = QUOTES_URL % {"s": self.tickers}
 		try:
 			quotes_file = urlopen(quotes_url, proxies = invest.PROXY)
-			self.data = quotes_file.readlines ()
+			self.data = quotes_file.read ()
 			quotes_file.close ()
 		except Exception, msg:
 			invest.debug("Error while retrieving quotes data (url = %s): %s" % (quotes_url, msg))
@@ -79,11 +79,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
 		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:
 			invest.debug("Canceling refresh timer")
@@ -135,17 +161,13 @@ 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:
@@ -154,7 +176,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.
@@ -168,7 +190,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))
 
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]