[gnome-music/wip/jfelder/musicbrainz-tageditordialog: 29/31] utils: Handle more tags



commit 7e24bb2f6eef3a31a58fc3af13f36c4e14b84b8d
Author: Jean Felder <jfelder src gnome org>
Date:   Tue Aug 21 00:24:40 2018 +0200

    utils: Handle more tags
    
    Add utils functions to retrieve album_disc_number, composer_name,
    genre and track number.

 gnomemusic/mpris.py                     |  8 +++---
 gnomemusic/utils.py                     | 50 +++++++++++++++++++++++++++++++++
 gnomemusic/widgets/albumwidget.py       |  4 +--
 gnomemusic/widgets/artistalbumwidget.py |  2 +-
 gnomemusic/widgets/songwidget.py        |  6 ++--
 5 files changed, 59 insertions(+), 11 deletions(-)
---
diff --git a/gnomemusic/mpris.py b/gnomemusic/mpris.py
index a160cca1..5fed0ea9 100644
--- a/gnomemusic/mpris.py
+++ b/gnomemusic/mpris.py
@@ -353,7 +353,7 @@ class MPRIS(DBusInterface):
             'xesam:albumArtist': GLib.Variant('as', [artist])
         }
 
-        genre = media.get_genre()
+        genre = utils.get_media_genre(media)
         if genre is not None:
             metadata['xesam:genre'] = GLib.Variant('as', [genre])
 
@@ -362,9 +362,9 @@ class MPRIS(DBusInterface):
             last_played_str = last_played.format("%FT%T%:z")
             metadata['xesam:lastUsed'] = GLib.Variant('s', last_played_str)
 
-        track_nr = media.get_track_number()
-        if track_nr > 0:
-            metadata['xesam:trackNumber'] = GLib.Variant('i', track_nr)
+        track_nr = utils.get_media_track_nr(media)
+        if track_nr:
+            metadata['xesam:trackNumber'] = GLib.Variant('i', int(track_nr))
 
         # If the media has already been part of an MPRIS playlist, its
         # thumbnail is already set. Otherwise, try to look for it in the
diff --git a/gnomemusic/utils.py b/gnomemusic/utils.py
index bae746e7..56b7e350 100644
--- a/gnomemusic/utils.py
+++ b/gnomemusic/utils.py
@@ -58,6 +58,19 @@ def get_album_title(item):
     return album
 
 
+def get_album_disc_nr(item):
+    """Returns the album song number associated with the media item
+
+    :param Grl.Media item: song
+    :return: The album title
+    :rtype: string
+    """
+    track_number = item.get_album_disc_number()
+    if track_number == 0:
+        track_number = ""
+    return str(track_number)
+
+
 def get_artist_name(item):
     """Returns the preferred artist for a media item.
 
@@ -77,6 +90,43 @@ def get_artist_name(item):
             or _("Unknown Artist"))
 
 
+def get_composer_name(item):
+    """Returns the composer for a media item.
+
+    :param Grl.Media item: song
+    :return: The artist name
+    :rtype: string
+    """
+
+    return (item.get_composer()
+            or None)
+
+
+def get_media_genre(item):
+    """Returns the title of the media item.
+
+    :param item: A Grilo Media object
+    :return: The title
+    :rtype:
+    """
+
+    return (item.get_genre()
+            or None)
+
+
+def get_media_track_nr(item):
+    """Returns the title of the media item.
+
+    :param item: A Grilo Media object
+    :return: The title
+    :rtype:
+    """
+    track_number = item.get_track_number()
+    if track_number == 0:
+        return ""
+    return str(track_number)
+
+
 def get_media_title(item):
     """Returns the title of the media item.
 
diff --git a/gnomemusic/widgets/albumwidget.py b/gnomemusic/widgets/albumwidget.py
index 1eea57f0..38c1f645 100644
--- a/gnomemusic/widgets/albumwidget.py
+++ b/gnomemusic/widgets/albumwidget.py
@@ -142,7 +142,7 @@ class AlbumWidget(Gtk.EventBox):
 
     @log
     def _set_composer_label(self, album):
-        composer = album.get_composer()
+        composer = utils.get_composer_name(album)
         show = False
 
         if composer:
@@ -210,7 +210,7 @@ class AlbumWidget(Gtk.EventBox):
         if remaining == 0:
             discs = {}
             for song in self._songs:
-                disc_nr = song.get_album_disc_number()
+                disc_nr = utils.get_album_disc_nr(song)
                 if disc_nr not in discs.keys():
                     discs[disc_nr] = [song]
                 else:
diff --git a/gnomemusic/widgets/artistalbumwidget.py b/gnomemusic/widgets/artistalbumwidget.py
index 8d8fda88..621aef3d 100644
--- a/gnomemusic/widgets/artistalbumwidget.py
+++ b/gnomemusic/widgets/artistalbumwidget.py
@@ -117,7 +117,7 @@ class ArtistAlbumWidget(Gtk.Box):
 
         discs = {}
         for song in self._songs:
-            disc_nr = song.get_album_disc_number()
+            disc_nr = utils.get_album_disc_nr(song)
             if disc_nr not in discs.keys():
                 discs[disc_nr] = [song]
             else:
diff --git a/gnomemusic/widgets/songwidget.py b/gnomemusic/widgets/songwidget.py
index 78c87181..851d137a 100644
--- a/gnomemusic/widgets/songwidget.py
+++ b/gnomemusic/widgets/songwidget.py
@@ -88,10 +88,8 @@ class SongWidget(Gtk.EventBox):
         self._selection_mode = False
         self._state = SongWidget.State.UNPLAYED
 
-        song_number = media.get_track_number()
-        if song_number == 0:
-            song_number = ""
-        self._number_label.set_text(str(song_number))
+        song_number = utils.get_media_track_nr(media)
+        self._number_label.props.label = song_number
 
         title = utils.get_media_title(media)
         self._title_label.set_max_width_chars(50)


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