[gnome-applets] [invest-applet] numbers are parsed and formated using user's locale, fixes bug #608059



commit 46f711745a9df31e6c044e01b690d7cd71cd5023
Author: Enrico Minack <enrico-minack gmx de>
Date:   Mon Jul 5 15:33:52 2010 +0200

    [invest-applet] numbers are parsed and formated using user's locale, fixes bug #608059

 invest-applet/invest/preferences.py |   20 +++++++++++-----
 invest-applet/invest/quotes.py      |   15 +++++++++--
 invest-applet/invest/widgets.py     |   43 ++++++++++++++++++++++++----------
 3 files changed, 56 insertions(+), 22 deletions(-)
---
diff --git a/invest-applet/invest/preferences.py b/invest-applet/invest/preferences.py
index 66f8fbe..cde62bf 100644
--- a/invest-applet/invest/preferences.py
+++ b/invest-applet/invest/preferences.py
@@ -1,9 +1,8 @@
 from gettext import gettext as _
+import locale
 from os.path import join
 import gtk, gobject, gconf
 import invest
-from gettext import gettext as _
-
 import cPickle
 
 class PrefsDialog:
@@ -31,10 +30,17 @@ class PrefsDialog:
 			try:
 				if col == 0:    # stock symbols must be uppercase
 					new_text = str.upper(new_text)
-				store[path][col] = typ(new_text)
+				if col < 2:
+					store[path][col] = new_text
+				else:
+					value = locale.atof(new_text)
+					store[path][col] = value
 			except:
 				pass
 
+		def format(fmt, value):
+			return locale.format(fmt, value, True)
+
 		def get_cell_data(column, cell, model, iter, data):
 			typ, col = data
 			if typ == int:
@@ -43,20 +49,22 @@ class PrefsDialog:
 				# provide float numbers with at least 2 fractional digits
 				val = model[iter][col]
 				digits = fraction_digits(val)
-				format = "%%.%df" % max(digits, 2)
-				cell.set_property('text', format % val)
+				fmt = "%%.%df" % max(digits, 2)
+				cell.set_property('text', format(fmt, val))
 			else:
 				cell.set_property('text', typ(model[iter][col]))
 
 		# determine the number of non zero digits in the fraction of the value
 		def fraction_digits(value):
-			text = "%g" % value
+			text = "%g" % value	# do not use locale here, so that %g always is rendered to a number with . as decimal separator
 			if text.find(".") < 0:
 				return 0
 			return len(text) - text.find(".") - 1
 
 		def create_cell (view, column, name, typ):
 			cell_description = gtk.CellRendererText ()
+			if typ == float:
+				cell_description.set_property("xalign", 1.0)
 			cell_description.set_property("editable", True)
 			cell_description.connect("edited", on_cell_edited, column, typ)
 			column_description = gtk.TreeViewColumn (name, cell_description)
diff --git a/invest-applet/invest/quotes.py b/invest-applet/invest/quotes.py
index 3f1a096..c123978 100644
--- a/invest-applet/invest/quotes.py
+++ b/invest-applet/invest/quotes.py
@@ -2,6 +2,7 @@ from os.path import join
 import gnomeapplet, gtk, gtk.gdk, gconf, gobject
 from gettext import gettext as _
 import csv
+import locale
 from urllib import urlopen
 import datetime
 from threading import Thread
@@ -115,6 +116,14 @@ class QuoteUpdater(gtk.ListStore):
 		return True
 
 
+	# locale-aware formatting of the percent float (decimal point, thousand grouping point) with 2 decimal digits
+	def format_percent(self, value):
+		return locale.format("%+.2f", value, True) + "%"
+
+	# locale-aware formatting of the float value (decimal point, thousand grouping point) with sign and 2 decimal digits
+	def format_difference(self, value):
+		return locale.format("%+.2f", value, True, True)
+
 	def on_retriever_completed(self, retriever):
 		if retriever.retrieved == False:
 			tooltip = [_('Invest could not connect to Yahoo! Finance')]
@@ -127,11 +136,11 @@ class QuoteUpdater(gtk.ListStore):
 			self.last_updated = datetime.datetime.now()
 			tooltip = []
 			if self.simple_quotes_count > 0:
-				# Translators: This is share-market jargon. It is the percentage change in the price of a stock. The %% gets changed to a single percent sign and the %+.2f gets replaced with the value of the change.
-				tooltip.append(_('Quotes average change %%: %+.2f%%') % self.avg_simple_quotes_change)
+				# Translators: This is share-market jargon. It is the percentage change in the price of a stock. The %% gets changed to a single percent sign and the %s gets replaced with the string value of the change (localized), including the percent sign.
+				tooltip.append(_('Quotes average change %%: %s') % self.format_percent(self.avg_simple_quotes_change))
 			if self.positions_count > 0:
 				# 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. i.e. How much money would be earned if they were sold right now.
-				tooltip.append(_('Positions balance: %+.2f') % self.positions_balance)
+				tooltip.append(_('Positions balance: %s') % self.format_difference(self.positions_balance))
 			tooltip.append(_('Updated at %s') % self.last_updated.strftime("%H:%M"))
 			self.set_tooltip_callback('\n'.join(tooltip))
 
diff --git a/invest-applet/invest/widgets.py b/invest-applet/invest/widgets.py
index ba0b19c..37ed6f6 100644
--- a/invest-applet/invest/widgets.py
+++ b/invest-applet/invest/widgets.py
@@ -2,11 +2,9 @@ import os, time
 from os.path import *
 import gnomeapplet, gtk, gtk.gdk, gconf, gobject, pango
 from gettext import gettext as _
-import gtk, gobject
-import csv, os
-from gettext import gettext as _
+import locale
+import csv
 import invest, invest.about, invest.chart
-from invest import *
 
 COLORSCALE_POSITIVE = [
 	"white",
@@ -57,6 +55,8 @@ class InvestWidget(gtk.TreeView):
 		for i, col_name in enumerate(col_names):
 			if i < 3:
 				cell = gtk.CellRendererText()
+				if i > 0:
+					cell.set_property("xalign", 1.0)
 				column = gtk.TreeViewColumn (col_name, cell)
 				if i == 0:
 					column.set_sort_column_id(quotes_updater.LABEL)
@@ -72,6 +72,7 @@ class InvestWidget(gtk.TreeView):
 				# add the last two column only if we have any positions
 				if simple_quotes_only == False:
 					cell = gtk.CellRendererText()
+					cell.set_property("xalign", 1.0)
 					column = gtk.TreeViewColumn (col_name, cell)
 					if i == 4:
 						column.set_sort_column_id(quotes_updater.BALANCE)
@@ -86,11 +87,25 @@ class InvestWidget(gtk.TreeView):
 		self.connect('row-activated', self.on_row_activated)
 		self.set_model(quotes_updater)
 
+
+	# locale-aware formatting of the value as monetary, without currency symbol, using 2 decimal digits
+	def format_currency(self, value):
+		return locale.format("%.2f", value, True, True)
+
+	# locale-aware formatting of the percent float (decimal point, thousand grouping point) with 2 decimal digits
+	def format_percent(self, value):
+		return locale.format("%+.2f", value, True) + "%"
+
+	# locale-aware formatting of the float value (decimal point, thousand grouping point) with sign and 2 decimal digits
+	def format_difference(self, value):
+		return locale.format("%+.2f", value, True, True)
+
+
 	def _getcelldata_label(self, column, cell, model, iter):
 		cell.set_property('text', model[iter][model.LABEL])
 
 	def _getcelldata_value(self, column, cell, model, iter):
-		cell.set_property('text', "%.5g" % model[iter][model.VALUE])
+		cell.set_property('text', self.format_currency(model[iter][model.VALUE]))
 
 	def is_selected(self, model, iter):
 		m, it = self.get_selection().get_selected()
@@ -107,9 +122,9 @@ class InvestWidget(gtk.TreeView):
 
 	def _getcelldata_variation(self, column, cell, model, iter):
 		color = self.get_color(model, iter, model.VARIATION_PCT)
-		change_pct = model[iter][model.VARIATION_PCT]
+		change_pct = self.format_percent(model[iter][model.VARIATION_PCT])
 		cell.set_property('markup',
-			"<span foreground='%s'>%+.2f%%</span>" %
+			"<span foreground='%s'>%s</span>" %
 			(color, change_pct))
 
 	def _getcelldata_balance(self, column, cell, model, iter):
@@ -118,9 +133,10 @@ class InvestWidget(gtk.TreeView):
 		if is_ticker_only:
 			cell.set_property('text', '')
 		else:
-					cell.set_property('markup',
-			"<span foreground='%s'>%+.2f</span>" %
-			(color, model[iter][model.BALANCE]))
+			balance = self.format_difference(model[iter][model.BALANCE])
+			cell.set_property('markup',
+				"<span foreground='%s'>%s</span>" %
+				(color, balance))
 
 	def _getcelldata_balancepct(self, column, cell, model, iter):
 		is_ticker_only = model[iter][model.TICKER_ONLY]
@@ -128,9 +144,10 @@ class InvestWidget(gtk.TreeView):
 		if is_ticker_only:
 			cell.set_property('text', '')
 		else:
-					cell.set_property('markup',
-			"<span foreground='%s'>%+.2f%%</span>" %
-			(color, model[iter][model.BALANCE_PCT]))
+			balance_pct = self.format_percent(model[iter][model.BALANCE_PCT])
+			cell.set_property('markup',
+				"<span foreground='%s'>%s</span>" %
+				(color, balance_pct))
 
 	def on_row_activated(self, treeview, path, view_column):
 		ticker = self.get_model()[self.get_model().get_iter(path)][0]



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