~ubuntu-branches/ubuntu/maverick/awn-extras-applets/maverick

« back to all changes in this revision

Viewing changes to applets/maintained/weather/weather.py

  • Committer: Bazaar Package Importer
  • Author(s): Julien Lavergne
  • Date: 2010-08-29 14:29:52 UTC
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: james.westby@ubuntu.com-20100829142952-rhvuetyms9bv5uu7
Tags: upstream-0.4.0+bzr1372
ImportĀ upstreamĀ versionĀ 0.4.0+bzr1372

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
#   Mike (mosburger) Desjardins <desjardinsmike@gmail.com>
5
5
#     Please do not email the above person for support. The 
6
6
#     email address is only there for license/copyright purposes.
7
 
# Copyright (C) 2009  onox <denkpadje@gmail.com>
 
7
# Copyright (C) 2009 - 2010  onox <denkpadje@gmail.com>
8
8
#
9
9
# This library is free software; you can redistribute it and/or
10
10
# modify it under the terms of the GNU Lesser General Public
38
38
import cairo
39
39
import glib
40
40
 
41
 
applet_name = "Weather"
42
 
applet_description = "Applet to display current weather and forecast"
 
41
applet_name = _("Weather")
 
42
applet_description = _("Applet to display current weather and forecast")
43
43
 
44
44
# Applet's themed icon, also shown in the GTK About dialog
45
45
applet_logo = "weather-few-clouds"
56
56
 
57
57
ui_file = os.path.join(os.path.dirname(__file__), "weather.ui")
58
58
 
59
 
temperature_units = ["Celcius", "Fahrenheit"]
 
59
temperature_units = [_("Celsius"), _("Fahrenheit")]
60
60
font_sizes = (15.0, 18.0, 23.0)
61
61
 
62
 
system_theme_name = "System theme"
 
62
system_theme_name = _("System theme")
63
63
 
64
64
theme_dir = "/usr/share/icons"
65
65
 
68
68
"weather-snow", "weather-fog", "weather-storm", "weather-severe-alert",
69
69
"weather-clear-night", "weather-few-clouds-night"]
70
70
 
71
 
network_error_message = "Could not retrieve weather data. You may be experiencing connectivity issues."
 
71
network_error_message = _("Could not retrieve weather data. You may be experiencing connectivity issues.")
72
72
 
73
73
import forecast
74
74
 
147
147
    """
148
148
    throbber_counter[func] = False
149
149
    disconnect_counter[func] = False
150
 
    def activate_throbber(show):
151
 
        throbber_counter[func] = show
 
150
    def activate_throbber(do_show):
 
151
        throbber_counter[func] = do_show
152
152
        overlay_fsm.evaluate()
153
 
    def active_icon(error):
154
 
        disconnect_counter[func] = error
 
153
    def active_icon(is_error):
 
154
        disconnect_counter[func] = is_error
155
155
        overlay_fsm.evaluate()
156
156
    def bound_func(obj, *args, **kwargs):
157
157
        activate_throbber(True)
198
198
    def __init__(self, applet):
199
199
        self.applet = applet
200
200
 
201
 
        self.cached_conditions = None
202
 
        self.map_vbox = None
203
 
        self.image_map = None
204
 
        self.map_pixbuf = None
 
201
        self.weather_fail = [False, False, False]
 
202
        self.reset_weather_data()
205
203
 
206
204
        self.network_handler = self.NetworkHandler()
207
 
        self.notification = applet.notify.create_notification("Network error in Weather", network_error_message, "dialog-warning", 20)
 
205
        self.notification = applet.notify.create_notification(_("Network error in Weather"), network_error_message, "dialog-warning", 20)
208
206
 
209
207
        self.setup_context_menu()
210
208
 
240
238
        if type(e) is NetworkException:
241
239
            print "Error in Weather:", e
242
240
            self.notification.show()
 
241
 
 
242
            if all(self.weather_fail):
 
243
                # Clean up weather map
 
244
                self.map_item.set_sensitive(False)
 
245
                if self.map_dialog is not None:
 
246
                    self.applet.dialog.unregister("secondary")
 
247
                self.reset_weather_data()
 
248
 
 
249
                # Clean up forecast dialog
 
250
                self.forecaster.finalize_dialog()
 
251
 
 
252
                # Clean up conditions (icon)
 
253
                self.__temp_overlay.props.active = False
 
254
                self.set_icon()
 
255
                self.applet.tooltip.set("%s %s..."%(_("Fetching conditions for"), self.applet.settings['location']))
243
256
        else:
244
257
            self.applet.errors.set_error_icon_and_click_to_restart()
245
258
            self.applet.errors.general(e, traceback=tb, callback=gtk.main_quit)
246
259
 
 
260
    def reset_weather_data(self):
 
261
        self.cached_conditions = None
 
262
        self.map_dialog = None
 
263
        self.map_vbox = None
 
264
        self.map_pixbuf = None
 
265
 
247
266
    def setup_context_menu(self):
248
267
        """Add "refresh" to the context menu and setup the preferences.
249
268
 
251
270
        menu = self.applet.dialog.menu
252
271
        menu_index = len(menu) - 1
253
272
 
254
 
        map_item = gtk.MenuItem(_("Show _Map"))
255
 
        map_item.connect("activate", self.activate_map_cb)
256
 
        menu.insert(map_item, menu_index)
 
273
        self.map_item = gtk.MenuItem(_("Show _Map"))
 
274
        self.map_item.connect("activate", self.activate_map_cb)
 
275
        menu.insert(self.map_item, menu_index)
 
276
        self.map_item.set_sensitive(False)
257
277
 
258
278
        refresh_item = gtk.ImageMenuItem(stock_id=gtk.STOCK_REFRESH)
259
279
        refresh_item.connect("activate", self.activate_refresh_cb)
391
411
        self.search_window.hide()
392
412
 
393
413
    def activate_map_cb(self, widget):
394
 
        self.map_dialog.show_all()
 
414
        if self.map_dialog is not None:
 
415
            self.map_dialog.show_all()
395
416
 
396
417
    def activate_refresh_cb(self, widget=None, map=True):
397
418
        """Refresh the icon, forecast, and map data. Called by the
432
453
 
433
454
        """
434
455
        def cb(conditions):
 
456
            self.weather_fail[0] = False
435
457
            if conditions != self.cached_conditions:
436
458
                self.cached_conditions = conditions
437
459
                self.refresh_icon()
442
464
                print "Reattempt (%d retries remaining) in %d seconds" % (retries, delay_seconds)
443
465
                self.applet.timing.delay(lambda: self.refresh_conditions(retries - 1), delay_seconds)
444
466
            else:
 
467
                self.weather_fail[0] = True
445
468
                self.network_error_cb(e, tb)
446
469
        self.network_handler.get_conditions(self.applet.settings['location_code'], callback=cb, error=error_cb)
447
470
 
459
482
            self.applet.tooltip.set(title)
460
483
            self.set_icon(self.cached_conditions["CODE"])
461
484
 
462
 
    def fetch_forecast(self, cb, retries=3):
 
485
    def fetch_forecast(self, forecast_cb, retries=3):
463
486
        """Use weather.com's XML service to download the latest 5-day
464
487
        forecast.
465
488
 
466
489
        """
 
490
        def cb(forecast):
 
491
            self.weather_fail[1] = False
 
492
            forecast_cb(forecast)
467
493
        def error_cb(e, tb):
468
494
            if type(e) is NetworkException and retries > 0:
469
495
                print "Warning in Weather:", e
471
497
                print "Reattempt (%d retries remaining) in %d seconds" % (retries, delay_seconds)
472
498
                self.applet.timing.delay(lambda: self.fetch_forecast(cb, retries - 1), delay_seconds)
473
499
            else:
 
500
                self.weather_fail[1] = True
474
501
                self.network_error_cb(e, tb)
475
502
        self.network_handler.get_forecast(self.applet.settings['location_code'], callback=cb, error=error_cb)
476
503
 
480
507
 
481
508
        """
482
509
        def cb(pixbuf):
 
510
            self.weather_fail[2] = False
483
511
            self.set_map_pixbuf(pixbuf)
484
512
        def error_cb(e, tb):
485
513
            if type(e) is NetworkException and retries > 0:
488
516
                print "Reattempt (%d retries remaining) in %d seconds" % (retries, delay_seconds)
489
517
                self.applet.timing.delay(lambda: self.fetch_weather_map(retries - 1), delay_seconds)
490
518
            else:
 
519
                self.weather_fail[2] = True
491
520
                self.network_error_cb(e, tb)
492
521
        self.network_handler.get_weather_map(self.applet.settings['location_code'], callback=cb, error=error_cb)
493
522
 
499
528
        """
500
529
        if pixbuf is None:
501
530
            return
502
 
        if self.map_vbox is None:
 
531
        if self.map_dialog is None or self.map_vbox is None:
503
532
            self.map_dialog = self.applet.dialog.new("secondary", title=self.applet.settings['location'])
504
533
            self.map_vbox = gtk.VBox()
505
534
            self.map_dialog.add(self.map_vbox)
519
548
            pixbuf = pixbuf.scale_simple(width, height, gtk.gdk.INTERP_BILINEAR)
520
549
 
521
550
        self.map_vbox.add(gtk.image_new_from_pixbuf(pixbuf))
 
551
        self.map_item.set_sensitive(True)
522
552
 
523
553
    def convert_temperature(self, value):
524
554
        unit = temperature_units[self.applet.settings["temperature-unit"]]
527
557
        except ValueError:
528
558
            return "N/A"
529
559
 
530
 
        if "Fahrenheit" == unit:
 
560
        if _("Fahrenheit") == unit:
531
561
            converted_value = value
532
 
        elif "Celcius" == unit:
 
562
        elif _("Celsius") == unit:
533
563
            converted_value = 5.0 * (value - 32.0) / 9.0
534
564
        return str(int(round(converted_value)))
535
565
 
664
694
        "copyright-year": "2007 - 2009",
665
695
        "theme": applet_logo,
666
696
        "authors": ["Mike Desjardins", "Mike Rooney", "Isaac J.", "onox <denkpadje@gmail.com>"],
667
 
        "artists": ["Wojciech Grzanka", "Mike Desjardins"],
668
 
        "type": ["Network", "Weather"]},
669
 
        ["settings-per-instance"])
 
697
        "artists": ["Wojciech Grzanka", "Mike Desjardins"]})