[gnome-music/wip/merge: 280/343] Fix redundant Renderers and replace "is not None" conditions and fix small pep8 issue as well as rem



commit 268a3787de1d57c5eb0edc6ef50f26e409d2a66c
Author: Seif Lotfy <seif lotfy com>
Date:   Fri Jul 19 01:20:41 2013 +0200

    Fix redundant Renderers and replace "is not None" conditions and
    fix small pep8 issue as well as remove unused methods

 gnomemusic/albumArtCache.py |   20 +++++++----------
 gnomemusic/player.py        |    4 +-
 gnomemusic/view.py          |   19 +++++++++--------
 gnomemusic/widgets.py       |   48 ++++++++++++++++++++----------------------
 4 files changed, 43 insertions(+), 48 deletions(-)
---
diff --git a/gnomemusic/albumArtCache.py b/gnomemusic/albumArtCache.py
index ad6e6e1..7bb4c14 100644
--- a/gnomemusic/albumArtCache.py
+++ b/gnomemusic/albumArtCache.py
@@ -26,10 +26,10 @@ class AlbumArtCache:
         return self.instance
 
     @classmethod
-    def getMediaTitle(self, media, escaped=False):
+    def get_media_title(self, media, escaped=False):
         title = media.get_title()
-        if title is not None:
-            return title
+        if title:
+            return GLib.markup_escape_text(title)
         uri = media.get_url()
         if uri is None:
             return "Untitled"
@@ -185,14 +185,14 @@ class AlbumArtCache:
 
     def lookup_or_resolve(self, item, width, height, callback):
         artist = None
-        if item.get_author() is not None:
+        if item.get_author():
             artist = item.get_author()
-        if item.get_string(Grl.METADATA_KEY_ARTIST) is not None:
+        if item.get_string(Grl.METADATA_KEY_ARTIST):
             artist = item.get_string(Grl.METADATA_KEY_ARTIST)
         album = item.get_string(Grl.METADATA_KEY_ALBUM)
 
         def lookup_ready(icon, path=None):
-            if icon is not None:
+            if icon:
                 # Cache the path on the original item for faster retrieval
                 item._thumbnail = path
                 callback(icon, path)
@@ -203,12 +203,8 @@ class AlbumArtCache:
                 if uri is None:
                     return
 
-                def get_from_uri_ready(image, path):
-                    item._thumbnail = path
-                    callback(image, path)
-
                 self.get_from_uri(uri, artist, album, width, height,
-                                  get_from_uri_ready)
+                                  callback)
 
             options = Grl.OperationOptions.new(None)
             options.set_flags(Grl.ResolutionFlags.FULL |
@@ -224,7 +220,7 @@ class AlbumArtCache:
     def _normalize_and_hash(self, input_str):
         normalized = " "
 
-        if input_str is not None and len(input_str) > 0:
+        if input_str and len(input_str) > 0:
             normalized = self._strip_invalid_entities(input_str)
             normalized = GLib.utf8_normalize(normalized, -1,
                                              GLib.NormalizeMode.NFKD)
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index 02834c4..7a1ece0 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -258,7 +258,7 @@ class Player(GObject.GObject):
         self.coverImg.set_from_pixbuf(self._symbolicIcon)
         self.cache.lookup(ART_SIZE, media.get_artist(), media.get_string(Grl.METADATA_KEY_ALBUM), 
self._on_cache_lookup)
 
-        self.titleLabel.set_label(AlbumArtCache.getMediaTitle(media))
+        self.titleLabel.set_label(AlbumArtCache.get_media_title(media))
 
         if media.get_artist() is not None:
             self.artistLabel.set_label(media.get_artist())
@@ -599,7 +599,7 @@ class Player(GObject.GObject):
             'xesam:userRating': GLib.Variant.new('d', media.get_rating()),
         }
 
-        title = AlbumArtCache.getMediaTitle(media)
+        title = AlbumArtCache.get_media_title(media)
         if title:
             metadata['xesam:title'] = GLib.Variant.new('s', title)
 
diff --git a/gnomemusic/view.py b/gnomemusic/view.py
index a860e85..0bd355b 100644
--- a/gnomemusic/view.py
+++ b/gnomemusic/view.py
@@ -165,18 +165,18 @@ class ViewContainer(Gtk.Stack):
     def _add_item(self, source, param, item):
         if item is not None:
             self._offset += 1
-            iter = self._model.append()
+            itr = self._model.append()
             artist = "Unknown"
             if item.get_author() is not None:
                 artist = item.get_author()
             if item.get_string(Grl.METADATA_KEY_ARTIST) is not None:
                 artist = item.get_string(Grl.METADATA_KEY_ARTIST)
-            title = albumArtCache.getMediaTitle(item)
+            title = albumArtCache.get_media_title(item)
             item.set_title(title)
             try:
                 if item.get_url():
                     self.player.discoverer.discover_uri(item.get_url())
-                self._model.set(iter,
+                self._model.set(itr,
                                 [0, 1, 2, 3, 4, 5, 7, 8, 9, 10],
                                 [str(item.get_id()), "", title,
                                  artist, self._symbolicIcon, item,
@@ -188,7 +188,7 @@ class ViewContainer(Gtk.Stack):
                                 [str(item.get_id()), "", title,
                                  artist, self._symbolicIcon, item,
                                  -1, self.errorIconName, False, True])
-            GLib.idle_add(self._update_album_art, item, iter)
+            GLib.idle_add(self._update_album_art, item, itr)
 
     def _get_remaining_item_count(self):
         count = -1
@@ -198,14 +198,15 @@ class ViewContainer(Gtk.Stack):
                 count = cursor.get_integer(0)
         return count - self._offset
 
-    def _update_album_art(self, item, iter):
+    def _update_album_art(self, item, itr):
         def _album_art_cache_look_up(icon, data=None):
+            item._thumbnail = icon
             if icon:
                 self._model.set_value(
-                    iter, 4,
+                    itr, 4,
                     albumArtCache.get_default()._make_icon_frame(icon))
             else:
-                self._model.set_value(iter, 4, None)
+                self._model.set_value(itr, 4, None)
                 self.emit("album-art-updated")
             pass
 
@@ -302,7 +303,7 @@ class Songs(ViewContainer):
         if item is not None:
             self._offset += 1
             iter = self._model.append()
-            item.set_title(albumArtCache.getMediaTitle(item))
+            item.set_title(albumArtCache.get_media_title(item))
             try:
                 if item.get_url():
                     self.player.discoverer.discover_uri(item.get_url())
@@ -366,7 +367,7 @@ class Songs(ViewContainer):
         cell.set_property("yalign", 0.5)
         cell.set_property("height", 48)
         cell.set_property("ellipsize", Pango.EllipsizeMode.END)
-        cell.set_property("text", albumArtCache.getMediaTitle(item))
+        cell.set_property("text", albumArtCache.get_media_title(item))
 
     def _on_list_widget_star_render(self, col, cell, model, itr, data):
         showstar = model.get_value(itr, 9)
diff --git a/gnomemusic/widgets.py b/gnomemusic/widgets.py
index fc2ec07..9286edb 100644
--- a/gnomemusic/widgets.py
+++ b/gnomemusic/widgets.py
@@ -110,7 +110,7 @@ class AlbumWidget(Gtk.EventBox):
         if(self.model.get_value(iter, 7) != ERROR_ICON_NAME):
             if (self.iterToClean and self.player.playlistId == self.album):
                 item = self.model.get_value(self.iterToClean, 5)
-                title = AlbumArtCache.getMediaTitle(item)
+                title = AlbumArtCache.get_media_title(item)
                 self.model.set_value(self.iterToClean, 0, title)
                 #Hide now playing icon
                 self.model.set_value(self.iterToClean, 6, False)
@@ -145,7 +145,7 @@ class AlbumWidget(Gtk.EventBox):
             ellipsize=Pango.EllipsizeMode.END,
             xalign=0.0
         )
-        list_widget.add_renderer(type_renderer, self._type_renderer_text, None)
+        list_widget.add_renderer(type_renderer, lambda *args: None, None)
         cols[0].clear_attributes(type_renderer)
         cols[0].add_attribute(type_renderer, "markup", 0)
 
@@ -155,17 +155,9 @@ class AlbumWidget(Gtk.EventBox):
             xalign=1.0
         )
         durationRenderer.add_class('dim-label')
-        list_widget.add_renderer(durationRenderer,
-                                 self._duration_renderer_text, None)
-
-    def _type_renderer_text(self, col, cell, model, itr, data):
-        pass
-
-    def _duration_renderer_text(self, col, cell, model, itr, data):
-        item = self.model.get_value(itr, 5)
-        if item:
-            duration = item.get_duration()
-            cell.set_property("text", self.player.seconds_to_string(duration))
+        list_widget.add_renderer(durationRenderer, lambda *args: None, None)
+        cols[0].clear_attributes(durationRenderer)
+        cols[0].add_attribute(durationRenderer, "markup", 1)
 
     def update(self, artist, album, item, header_bar, selection_toolbar):
         self.selection_toolbar = selection_toolbar
@@ -199,7 +191,8 @@ class AlbumWidget(Gtk.EventBox):
                                        GObject.TYPE_BOOLEAN,
                                        GObject.TYPE_BOOLEAN,
                                        )
-            GLib.idle_add(grilo.populate_album_songs, item.get_id(), self._on_populate_album_songs)
+            GLib.idle_add(grilo.populate_album_songs, item.get_id(),
+                          self._on_populate_album_songs)
         header_bar._selectButton.connect('toggled',
                                          self._on_header_select_button_toggled)
         header_bar._cancelButton.connect('clicked',
@@ -249,14 +242,16 @@ class AlbumWidget(Gtk.EventBox):
             self.tracks.append(track)
             self.duration = self.duration + track.get_duration()
             iter = self.model.append()
-            escapedTitle = AlbumArtCache.getMediaTitle(track, True)
+            escapedTitle = AlbumArtCache.get_media_title(track, True)
             try:
                 self.player.discoverer.discover_uri(track.get_url())
                 self.model.set(iter,
                                [0, 1, 2, 3, 4, 5, 7, 9],
-                               [escapedTitle, "", "", "",
-                                None, track,
-                                NOW_PLAYING_ICON_NAME, False])
+                               [escapedTitle,
+                                self.player.seconds_to_string(
+                                    track.get_duration()),
+                                "", "", None, track, NOW_PLAYING_ICON_NAME,
+                                False])
             except Exception as err:
                 logging.debug(err.message)
                 logging.debug("failed to discover url " + track.get_url())
@@ -282,7 +277,7 @@ class AlbumWidget(Gtk.EventBox):
         while iter:
             song = playlist.get_value(iter, 5)
 
-            escapedTitle = AlbumArtCache.getMediaTitle(song, True)
+            escapedTitle = AlbumArtCache.get_media_title(song, True)
             if (song == currentSong):
                 title = "<b>%s</b>" % escapedTitle
                 iconVisible = True
@@ -364,7 +359,7 @@ class ArtistAlbums(Gtk.VBox):
             if not song_widget.can_be_played:
                 continue
 
-            escapedTitle = AlbumArtCache.getMediaTitle(song, True)
+            escapedTitle = AlbumArtCache.get_media_title(song, True)
             if (song == currentSong):
                 song_widget.now_playing_sign.show()
                 song_widget.title.set_markup("<b>%s</b>" % escapedTitle)
@@ -384,7 +379,7 @@ class ArtistAlbums(Gtk.VBox):
         while itr:
             song = self.model.get_value(itr, 5)
             song_widget = song.song_widget
-            escapedTitle = AlbumArtCache.getMediaTitle(song, True)
+            escapedTitle = AlbumArtCache.get_media_title(song, True)
             if song_widget.can_be_played:
                 song_widget.now_playing_sign.hide()
             song_widget.title.set_markup("<span>%s</span>" % escapedTitle)
@@ -443,7 +438,8 @@ class AllArtistsAlbums(ArtistAlbums):
 
     def _populate(self, data=None):
         if grilo.tracker:
-            GLib.idle_add(grilo.populate_albums, self._offset, self.add_item, 5)
+            GLib.idle_add(grilo.populate_albums,
+                          self._offset, self.add_item, 5)
 
     def add_item(self, source, param, item):
         if item:
@@ -481,9 +477,11 @@ class ArtistAlbumWidget(Gtk.HBox):
         if album.get_creation_date():
             self.ui.get_object("year").set_markup(
                 "<span color='grey'>(%s)</span>" %
-                str(album.get_creation_date().get_year()))
+                str(album.get_creation_date().get_year())
+            )
         self.tracks = []
-        GLib.idle_add(grilo.populate_album_songs, album.get_id(), self.get_songs)
+        GLib.idle_add(grilo.populate_album_songs,
+                      album.get_id(), self.get_songs)
         self.pack_start(self.ui.get_object("ArtistAlbumWidget"), True, True, 0)
         self.show_all()
 
@@ -499,7 +497,7 @@ class ArtistAlbumWidget(Gtk.HBox):
                 ui.get_object("num")\
                     .set_markup("<span color='grey'>%d</span>"
                                 % len(self.songs))
-                title = AlbumArtCache.getMediaTitle(track)
+                title = AlbumArtCache.get_media_title(track)
                 ui.get_object("title").set_text(title)
                 ui.get_object("title").set_alignment(0.0, 0.5)
                 self.ui.get_object("grid1").attach(


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