[gnome-applets/wip/dont-use-deprecated] invest-applet: further moved to gobject introspection - ListStore.set is not introspectable, use .se



commit db5dade30d5c1ecef69e11c0fe99859c13a9aaa8
Author: Enrico Minack <enrico-minack gmx de>
Date:   Sat Apr 9 01:53:54 2011 +0200

    invest-applet: further moved to gobject introspection
    - ListStore.set is not introspectable, use .set_value
    - numeric values need to be forced into float before added to list store
    - moved back to old idle object implementation of trheading as this works again
    - moved from TreeViewColumn.set_attributes to .add_attribute
    - some callbacks require data pointer
    - GdkPixbuf.PixbufLoader.write(buf) works since 2.23.2, see bug 646487

 invest-applet/data/Invest_Applet.xml |   14 ++++--
 invest-applet/invest/__init__.py     |    2 +-
 invest-applet/invest/applet.py       |    8 ++--
 invest-applet/invest/chart.py        |   70 +++++++++++-----------------
 invest-applet/invest/preferences.py  |   10 ++--
 invest-applet/invest/quotes.py       |   86 +++++++++++++--------------------
 6 files changed, 81 insertions(+), 109 deletions(-)
---
diff --git a/invest-applet/data/Invest_Applet.xml b/invest-applet/data/Invest_Applet.xml
index 5e7f712..d48e4cc 100644
--- a/invest-applet/data/Invest_Applet.xml
+++ b/invest-applet/data/Invest_Applet.xml
@@ -1,4 +1,10 @@
-<menuitem name="Refresh" action="Refresh" />
-<menuitem name="Prefs" action="Prefs" />
-<menuitem name="Help" action="Help" />
-<menuitem name="About" action="About" />
+<Root>
+	<popups>
+		<popup name="button3">
+			<menuitem name="Refresh" action="Refresh" />
+			<menuitem name="Prefs" action="Prefs" />
+			<menuitem name="Help" action="Help" />
+			<menuitem name="About" action="About" />
+		</popup>
+	</popups>
+</Root>
\ No newline at end of file
diff --git a/invest-applet/invest/__init__.py b/invest-applet/invest/__init__.py
index 71bd2ca..b8fdb92 100644
--- a/invest-applet/invest/__init__.py
+++ b/invest-applet/invest/__init__.py
@@ -11,7 +11,7 @@ import random
 # Autotools set the actual data_dir in defs.py
 from defs import *
 
-DEBUGGING = True
+DEBUGGING = False
 
 LOGFILE = open('/tmp/investapplet.%d.log' % random.randint(1000000, 9999999), 'w')
 
diff --git a/invest-applet/invest/applet.py b/invest-applet/invest/applet.py
index b8af64b..18f361e 100644
--- a/invest-applet/invest/applet.py
+++ b/invest-applet/invest/applet.py
@@ -19,10 +19,10 @@ class InvestApplet(PanelApplet.Applet):
 
 		# name, stock_id, label, accellerator, tooltip, callback
 		invest.debug("defining menu actions");
-		menu_actions = [("InvestAbout", Gtk.STOCK_HELP, "About", None, None, self.on_about), 
-				("InvestHelp", Gtk.STOCK_HELP, "Help", None, None, self.on_help),
-				("InvestPrefs", Gtk.STOCK_PREFERENCES, "Prefs", None, None, self.on_preferences),
-				("InvestRefresh", Gtk.STOCK_REFRESH, "Refresh", None, None, self.on_refresh)
+		menu_actions = [("About", Gtk.STOCK_HELP, _("About"), None, None, self.on_about), 
+				("Help", Gtk.STOCK_HELP, _("Help"), None, None, self.on_help),
+				("Prefs", Gtk.STOCK_PREFERENCES, _("Preferences"), None, None, self.on_preferences),
+				("Refresh", Gtk.STOCK_REFRESH, _("Refresh"), None, None, self.on_refresh)
 				]
 
 		invest.debug("creating action group")
diff --git a/invest-applet/invest/chart.py b/invest-applet/invest/chart.py
index 8a01970..0d2784b 100644
--- a/invest-applet/invest/chart.py
+++ b/invest-applet/invest/chart.py
@@ -1,7 +1,6 @@
 #!/usr/bin/env python
 
 from gi.repository import GObject, Gtk, Gdk, GdkPixbuf, Gio
-
 import os
 import invest
 from gettext import gettext as _
@@ -11,50 +10,42 @@ from os.path import join
 import urllib
 from threading import Thread
 import time
-import ctypes
 
 AUTOREFRESH_TIMEOUT = 15*60*1000 # 15 minutes
 
 # based on http://www.johnstowers.co.nz/blog/index.php/2007/03/12/threading-and-pygtk/
-#class _IdleObject(GObject.Object):
-#	"""
-#	Override GObject.Object to always emit signals in the main thread
-#	by emmitting on an idle handler
-#	"""
-#	def __init__(self):
-#		GObject.Object.__init__(self)
-#
-#	def emit(self, *args):
-#		GObject.idle_add(GObject.Object.emit,self,*args)
-#
-#class ImageRetriever(Thread, _IdleObject):
-class ImageRetriever(Thread):
-#	"""
-#	Thread which uses gobject signals to return information
-#	to the GUI.
-#	"""
-#	__gsignals__ =  { 
-#			"completed": (
-#				GObject.SignalFlags.RUN_LAST, GObject.TYPE_NONE, []),
-#			# FIXME: should we be making use of this?
-#			#"progress": (
-#			#	gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, [
-#			#	gobject.TYPE_FLOAT])        #percent complete
-#			}
+class _IdleObject(GObject.Object):
+	"""
+	Override GObject.Object to always emit signals in the main thread
+	by emmitting on an idle handler
+	"""
+	def __init__(self):
+		GObject.Object.__init__(self)
 
-	completed_callback = None
-	completed_callback_arg = None
+	def emit(self, *args):
+		GObject.idle_add(GObject.Object.emit,self,*args)
+
+class ImageRetriever(Thread, _IdleObject):
+	"""
+	Thread which uses gobject signals to return information
+	to the GUI.
+	"""
+	__gsignals__ =  { 
+			"completed": (
+				GObject.SignalFlags.RUN_LAST, GObject.TYPE_NONE, []),
+			# FIXME: should we be making use of this?
+			#"progress": (
+			#	gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, [
+			#	gobject.TYPE_FLOAT])        #percent complete
+			}
 
 	def __init__(self, image_url):
 		Thread.__init__(self)
-#		_IdleObject.__init__(self)	
+		_IdleObject.__init__(self)	
 		self.image_url = image_url
 		self.retrieved = False
 		
 	def run(self):
-		if self.completed_callback == None:
-			invest.debug("Completed callback not set before ImageRetriever got started")
-		
 		self.image = Gtk.Image()
 		try: sock = urllib.urlopen(self.image_url, proxies = invest.PROXY)
 		except Exception, msg:
@@ -62,17 +53,11 @@ class ImageRetriever(Thread):
 		else:
 			loader = GdkPixbuf.PixbufLoader()
 			loader.connect("closed", lambda loader: self.image.set_from_pixbuf(loader.get_pixbuf()))
-			loader.write(sock.read(), -1) 
+			loader.write(sock.read()) 
 			sock.close()
 			loader.close()
 			self.retrieved = True
-#		self.emit("completed")
-		if self.completed_callback != None:
-			invest.debug("Calling ImageRetriever callback %s with arg %s" % (self.completed_callback, self.completed_callback_arg))
-			if self.completed_callback_arg == None:
-				self.completed_callback(self)
-			else:
-				self.completed_callback(self, self.completed_callback_arg)
+		self.emit("completed")
 
 # p:
 #  eX = Exponential Moving Average
@@ -238,8 +223,7 @@ class FinancialChart:
 		progress.show()
 		
 		image_retriever = ImageRetriever(url)
-#		image_retriever.connect("completed", self.on_retriever_completed)
-		image_retriever.completed_callback = self.on_retriever_completed
+		image_retriever.connect("completed", self.on_retriever_completed)
 		image_retriever.start()
 
 		# Update timer if needed
diff --git a/invest-applet/invest/preferences.py b/invest-applet/invest/preferences.py
index fc4e8c4..0aadc63 100644
--- a/invest-applet/invest/preferences.py
+++ b/invest-applet/invest/preferences.py
@@ -66,7 +66,7 @@ class PrefsDialog:
 					exchange =  purchase["exchange"]
 				else:
 					exchange = 0.0
-				store.append([key, label, purchase["amount"], purchase["bought"], purchase["comission"], exchange])
+				store.append([key, label, float(purchase["amount"]), float(purchase["bought"]), float(purchase["comission"]), float(exchange)])
 
 		self.sync_ui()
 
@@ -114,7 +114,7 @@ class PrefsDialog:
 		cell_description.connect("edited", self.on_cell_edited, column, typ)
 		column_description = Gtk.TreeViewColumn (name, cell_description)
 		if typ == str:
-			column_description.set_attributes (cell_description, text=column)
+			column_description.add_attribute(cell_description, "text", column)
 			column_description.set_sort_column_id(column)
 		if typ == float:
 			column_description.set_cell_data_func(cell_description, self.get_cell_data, (float, column))
@@ -140,7 +140,7 @@ class PrefsDialog:
 
 		invest.STOCKS = {}
 
-		def save_symbol(model, path, iter):
+		def save_symbol(model, path, iter, data):
 			#if int(model[iter][1]) == 0 or float(model[iter][2]) < 0.0001:
 			#	return
 
@@ -153,7 +153,7 @@ class PrefsDialog:
 				"comission": float(model[iter][4]),
 				"exchange": float(model[iter][5])
 			})
-		self.model.foreach(save_symbol)
+		self.model.foreach(save_symbol, None)
 		try:
 			cPickle.dump(invest.STOCKS, file(invest.STOCKS_FILE, 'w'))
 			invest.debug('Stocks written to file')
@@ -173,7 +173,7 @@ class PrefsDialog:
 		pass
 
 	def on_add_stock(self, w):
-		iter = self.model.append(["GOOG", "Google Inc.", 0, 0, 0, 0])
+		iter = self.model.append(["GOOG", "Google Inc.", 0.0, 0.0, 0.0, 0.0])
 		path = self.model.get_path(iter)
 		self.treeview.set_cursor(path, self.treeview.get_column(0), True)
 
diff --git a/invest-applet/invest/quotes.py b/invest-applet/invest/quotes.py
index 066ff91..56b5e53 100644
--- a/invest-applet/invest/quotes.py
+++ b/invest-applet/invest/quotes.py
@@ -19,38 +19,34 @@ QUOTES_URL="http://finance.yahoo.com/d/quotes.csv?s=%(s)s&f=snc4l1d1t1c1ohgv&e=.
 QUOTES_CSV_FIELDS=["ticker", "label", "currency", ("trade", float), "date", "time", ("variation", float), ("open", float), ("high", float), ("low", float), ("volume", int)]
 
 # based on http://www.johnstowers.co.nz/blog/index.php/2007/03/12/threading-and-pygtk/
-#class _IdleObject(GObject.Object):
-#	"""
-#	Override gobject.Object to always emit signals in the main thread
-#	by emmitting on an idle handler
-#	"""
-#	def __init__(self):
-#		GObject.Object.__init__(self)
-#
-#	def emit(self, *args):
-#		GObject.idle_add(GObject.Object.emit,self,*args)
-#
-#class QuotesRetriever(Thread, _IdleObject):
-class QuotesRetriever(Thread):
-#	"""
-#	Thread which uses gobject signals to return information
-#	to the GUI.
-#	"""
-#	__gsignals__ =  {
-#			"completed": (
-#				GObject.SignalFlags.RUN_LAST, GObject.TYPE_NONE, []),
-#			# FIXME: We don't monitor progress, yet ...
-#			#"progress": (
-#			#	gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, [
-#			#	gobject.TYPE_FLOAT])        #percent complete
-#			}
-
-	completed_callback = None
-	completed_callback_arg = None
+class _IdleObject(GObject.Object):
+	"""
+	Override gobject.Object to always emit signals in the main thread
+	by emmitting on an idle handler
+	"""
+	def __init__(self):
+		GObject.Object.__init__(self)
+
+	def emit(self, *args):
+		GObject.idle_add(GObject.Object.emit,self,*args)
+
+class QuotesRetriever(Thread, _IdleObject):
+	"""
+	Thread which uses gobject signals to return information
+	to the GUI.
+	"""
+	__gsignals__ =  {
+			"completed": (
+				GObject.SignalFlags.RUN_LAST, GObject.TYPE_NONE, []),
+			# FIXME: We don't monitor progress, yet ...
+			#"progress": (
+			#	gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, [
+			#	gobject.TYPE_FLOAT])        #percent complete
+			}
 
 	def __init__(self, tickers):
 		Thread.__init__(self)
-#		_IdleObject.__init__(self)
+		_IdleObject.__init__(self)
 		self.tickers = tickers
 		self.retrieved = False
 		self.data = []
@@ -59,10 +55,6 @@ class QuotesRetriever(Thread):
 
 	def run(self):
 		invest.debug("QuotesRetriever started");
-
-		if self.completed_callback == None:
-			invest.debug("Completed callback not set before QuotesRetriever got started")
-		
 		quotes_url = QUOTES_URL % {"s": self.tickers}
 		try:
 			quotes_file = urlopen(quotes_url, proxies = invest.PROXY)
@@ -72,14 +64,7 @@ class QuotesRetriever(Thread):
 			invest.debug("Error while retrieving quotes data (url = %s): %s" % (quotes_url, msg))
 		else:
 			self.retrieved = True
-
-#		self.emit("completed")
-		if self.completed_callback != None:
-			invest.debug("Calling ImageRetriever callback %s with arg %s" % (self.completed_callback, self.completed_callback_arg))
-			if self.completed_callback_arg == None:
-				self.completed_callback(self)
-			else:
-				self.completed_callback(self, self.completed_callback_arg)
+		self.emit("completed")
 
 
 class QuoteUpdater(Gtk.ListStore):
@@ -130,11 +115,9 @@ class QuoteUpdater(Gtk.ListStore):
 			return True
 
 		tickers = '+'.join(invest.STOCKS.keys())
-		invest.debug("creating quores retriever")
+		invest.debug("creating quotes retriever")
 		quotes_retriever = QuotesRetriever(tickers)
-#		quotes_retriever.connect("completed", self.on_retriever_completed)
-		invest.debug("adding callback")
-		quotes_retriever.completed_callback = self.on_retriever_completed
+		quotes_retriever.connect("completed", self.on_retriever_completed)
 		invest.debug("starting")
 		quotes_retriever.start()
 		invest.debug("started")
@@ -310,8 +293,7 @@ class QuoteUpdater(Gtk.ListStore):
 				url = 'http://ichart.yahoo.com/h?s=%s' % ticker
 
 				image_retriever = invest.chart.ImageRetriever(url)
-				image_retriever.completed_callback = self.set_pb_callback
-				image_retriever.completed_callback_arg = row
+				image_retriever.connect("completed", self.set_pb_callback, row)
 				image_retriever.start()
 
 				quotes_change += val['variation_pct']
@@ -355,7 +337,7 @@ class QuoteUpdater(Gtk.ListStore):
 			if len(symbols) > 0:
 				tickers = '+'.join(symbols)
 				quotes_retriever = QuotesRetriever(tickers)
-				quotes_retriever.completed_callback = self.on_currency_retriever_completed
+				quotes_retriever.connect("completed", self.on_currency_retriever_completed)
 				quotes_retriever.start()
 
 	def convert_currencies(self, quotes):
@@ -389,15 +371,15 @@ class QuoteUpdater(Gtk.ListStore):
 					ticker = self.get_value(iter, self.SYMBOL)
 					value = self.get_value(iter, self.VALUE)
 					(balance, change) = self.balance(invest.STOCKS[ticker]["purchases"], value, rates[currency])
-					self.set(iter, self.BALANCE, balance)
-					self.set(iter, self.BALANCE_PCT, change)
+					self.set_value(iter, self.BALANCE, balance)
+					self.set_value(iter, self.BALANCE_PCT, change)
 					self.add_balance_change(balance, change, target_currency)
 
 				# now, convert the value
 				value = self.get_value(iter, self.VALUE)
 				value *= rates[currency]
-				self.set(iter, self.VALUE, value)
-				self.set(iter, self.CURRENCY, target_currency)
+				self.set_value(iter, self.VALUE, value)
+				self.set_value(iter, self.CURRENCY, target_currency)
 
 			else:
 				# consider non-converted stocks here



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