← Back to team overview

weather-indicator-team team mailing list archive

[Merge] lp:~marcelstimberg/weather-indicator/locale_number_format into lp:weather-indicator

 

Marcel Stimberg has proposed merging lp:~marcelstimberg/weather-indicator/locale_number_format into lp:weather-indicator.

Requested reviews:
  Weather Indicator Team (weather-indicator-team)
Related bugs:
  Bug #756704 in Weather Indicator: "Numbers (temperature etc.) do not use local number format"
  https://bugs.launchpad.net/weather-indicator/+bug/756704

For more details, see:
https://code.launchpad.net/~marcelstimberg/weather-indicator/locale_number_format/+merge/57080

This fixes bug #756704 by introducing a little NumberFormatter helper class that formats numbers according to the current locale. As a minor refactoring, I imported the helpers modules as 'helpers'.
-- 
https://code.launchpad.net/~marcelstimberg/weather-indicator/locale_number_format/+merge/57080
Your team Weather Indicator Team is requested to review the proposed merge of lp:~marcelstimberg/weather-indicator/locale_number_format into lp:weather-indicator.
=== modified file 'bin/indicator-weather'
--- bin/indicator-weather	2011-04-10 13:18:53 +0000
+++ bin/indicator-weather	2011-04-10 18:12:31 +0000
@@ -53,7 +53,7 @@
     sys.path.insert(0, PROJECT_ROOT_DIRECTORY)
     os.putenv('PYTHONPATH', PROJECT_ROOT_DIRECTORY) # for subprocesses
 
-from indicator_weather.helpers import *
+import indicator_weather.helpers as helpers
 
 class Settings:
     """ Class to read/write settings """
@@ -632,16 +632,17 @@
                 _unit  = "ËšF"
         if self.__weather_datasource == WeatherDataSource.YAHOO:
             if (self.__metric_system == MetricSystem.SI):
-                _value = "%.1f" % ((float(self.__report['condition']['temp']) - 32) * 5/9)
+                _value = helpers.NumberFormatter.format("%.1f", (float(self.__report['condition']['temp']) - 32) * 5/9, remove_zero=True)
                 _unit  = "ËšC"
             else:
                 _value = self.__report['condition']['temp']
                 _unit  = "ËšF"
         # round the value if required
         if needs_rounding:
-            _value = round(float(_value))
-        # Removing unnecessary '.0' in the end, if exists
-        return ("%s %s" % (_value, _unit)).replace(".0", "")
+            _rounded = round(locale.atof(_value))
+            _value = helpers.NumberFormatter.format("%.1f", _rounded, remove_zero=True)
+
+        return ("%s %s" % (_value, _unit))
 
     # Get temperature label
     def get_temperature_label(self):
@@ -694,16 +695,16 @@
 
         # Join wind_info data in a label
         localized_wind_info[len(localized_wind_info)-1] = _unit
-        localized_wind_info[len(localized_wind_info)-2] = ("%0.1f" % _value).replace(".0", "")
+        localized_wind_info[len(localized_wind_info)-2] = helpers.NumberFormatter.format("%0.1f", _value, remove_zero=True)
         return ' '.join(localized_wind_info)
 
     # Get sunrise label
     def get_sunrise_label(self):
-        return "%s: %s" % (_("Sunrise"), TimeFormatter.format_time(self.__sunrise_t))
+        return "%s: %s" % (_("Sunrise"), helpers.TimeFormatter.format_time(self.__sunrise_t))
 
     # Get sunset label
     def get_sunset_label(self):
-        return "%s: %s" % (_("Sunset"), TimeFormatter.format_time(self.__sunset_t))
+        return "%s: %s" % (_("Sunset"), helpers.TimeFormatter.format_time(self.__sunset_t))
 
 
     # Additional functions
@@ -793,7 +794,7 @@
         self.winder.set_attention_icon ("weather-indicator-error")
 
         self.refreshed_minutes_ago = 0
-        monitor_upower(self.on_system_sleep, self.on_system_resume, log)
+        helpers.monitor_upower(self.on_system_sleep, self.on_system_resume, log)
 
         log.debug("Indicator: reading settings")
         self.settings = Settings()
@@ -1292,7 +1293,7 @@
     # Creating a new preferences dialog
     def __new__(cls):
         log.debug("Preferences: creating")
-        builder = get_builder('PreferencesDialog')
+        builder = helpers.get_builder('PreferencesDialog')
         new_object = builder.get_object("preferences_dialog")
         new_object.finish_initializing(builder)
         return new_object
@@ -1464,7 +1465,7 @@
     # Create forecast
     def __new__(cls):
         log.debug("ExtendedForecast: creating")
-        builder = get_builder('ExtendedForecast')
+        builder = helpers.get_builder('ExtendedForecast')
         new_object = builder.get_object("extended_forecast")
         new_object.finish_initializing(builder)
         return new_object
@@ -1548,7 +1549,7 @@
     # Create new object
     def __new__(cls):
         log.debug("Assistant: creating new Assistance instance")
-        builder = get_builder('Assistant')
+        builder = helpers.get_builder('Assistant')
         new_object = builder.get_object("assistant")
         new_object.finish_initializing(builder)  
         return new_object
@@ -1713,10 +1714,10 @@
         sys.exit(_("Another instance of this program is already running"))
 
     # Set http proxy support
-    ProxyMonitor.monitor_proxy(log)
+    helpers.ProxyMonitor.monitor_proxy(log)
     # Use date-time format as in indicator-datetime
-    TimeFormatter.monitor_indicator_datetime(log)
-        
+    helpers.TimeFormatter.monitor_indicator_datetime(log)
+
     # not running, safe to continue...
     gtk.gdk.threads_init()
     gtk.gdk.threads_enter()

=== modified file 'etc/xdg/autostart/indicator-weather.desktop' (properties changed: +x to -x)
=== modified file 'indicator_weather/helpers.py'
--- indicator_weather/helpers.py	2011-04-10 07:11:55 +0000
+++ indicator_weather/helpers.py	2011-04-10 18:12:31 +0000
@@ -32,6 +32,7 @@
 import gtk
 import urllib2
 import dbus
+import locale
 from dbus.mainloop.glib import DBusGMainLoop
 from indicator_weather.indicator_weatherconfig import get_data_file
 
@@ -219,3 +220,30 @@
             # ignore this as it might contain date params
             #TimeFormatter.format = gsettings.get_string("custom-time-format")
             TimeFormatter.format = "%X"
+
+class NumberFormatter:
+    """
+    Formats a number with respect to the locale settings
+    """
+
+    decimal_point = None
+
+    @staticmethod
+    def format(format, number, remove_zero=False):
+        """ 
+        Formats a number according to the current locale. If remove_zero is True, a trailing ".0" 
+        (again, according to the locale) will be removed. Note that this assumes that there is never
+        more than one decimal after the decimal point.
+        """
+
+        formatted = locale.format(format, number)
+
+        if remove_zero:
+            #get decimal point character if not yet known
+            if NumberFormatter.decimal_point is None:
+                conventions = locale.localeconv()
+                NumberFormatter.decimal_point = conventions['decimal_point']
+
+            return formatted.rstrip("0").rstrip(NumberFormatter.decimal_point)
+        else:
+            return formatted