[gnome-music/wip/mschraal/playlists-fixes: 6/10] utils: Move SortListModel PyGI wrapper here



commit c84bb114efc71c6c811649fe6108fd9d60478699
Author: Marinus Schraal <mschraal gnome org>
Date:   Fri Feb 21 14:56:52 2020 +0100

    utils: Move SortListModel PyGI wrapper here

 gnomemusic/coremodel.py     | 22 ++++++----------------
 gnomemusic/player.py        | 13 ++-----------
 gnomemusic/songliststore.py | 12 +-----------
 gnomemusic/utils.py         | 12 ++++++++++++
 4 files changed, 21 insertions(+), 38 deletions(-)
---
diff --git a/gnomemusic/coremodel.py b/gnomemusic/coremodel.py
index 4e76c346..17eb5167 100644
--- a/gnomemusic/coremodel.py
+++ b/gnomemusic/coremodel.py
@@ -27,7 +27,6 @@ import math
 import gi
 gi.require_version("Gfm", "0.1")
 from gi.repository import GObject, Gio, Gfm, Gtk
-from gi._gi import pygobject_new_full
 
 from gnomemusic.coreartist import CoreArtist
 from gnomemusic.coregrilo import CoreGrilo
@@ -94,12 +93,12 @@ class CoreModel(GObject.GObject):
         self._album_model = Gio.ListStore()
         self._album_model_sort = Gfm.SortListModel.new(self._album_model)
         self._album_model_sort.set_sort_func(
-            self._wrap_list_store_sort_func(self._albums_sort))
+            utils.wrap_list_store_sort_func(self._albums_sort))
 
         self._artist_model = Gio.ListStore.new(CoreArtist)
         self._artist_model_sort = Gfm.SortListModel.new(self._artist_model)
         self._artist_model_sort.set_sort_func(
-            self._wrap_list_store_sort_func(self._artist_sort))
+            utils.wrap_list_store_sort_func(self._artist_sort))
 
         self._playlist_model = Gio.ListStore.new(CoreSong)
         self._playlist_model_sort = Gfm.SortListModel.new(self._playlist_model)
@@ -128,14 +127,14 @@ class CoreModel(GObject.GObject):
         self._playlists_model_sort = Gfm.SortListModel.new(
             self._playlists_model_filter)
         self._playlists_model_sort.set_sort_func(
-            self._wrap_list_store_sort_func(self._playlists_sort))
+            utils.wrap_list_store_sort_func(self._playlists_sort))
 
         self._user_playlists_model_filter = Gfm.FilterListModel.new(
             self._playlists_model)
         self._user_playlists_model_sort = Gfm.SortListModel.new(
             self._user_playlists_model_filter)
         self._user_playlists_model_sort.set_sort_func(
-            self._wrap_list_store_sort_func(self._playlists_sort))
+            utils.wrap_list_store_sort_func(self._playlists_sort))
 
         self.props.grilo = CoreGrilo(self, application)
         # FIXME: Not all instances of internal _grilo use have been
@@ -184,15 +183,6 @@ class CoreModel(GObject.GObject):
             playlist_a.props.creation_date)
         return math.copysign(1, date_diff)
 
-    def _wrap_list_store_sort_func(self, func):
-
-        def wrap(a, b, *user_data):
-            a = pygobject_new_full(a, False)
-            b = pygobject_new_full(b, False)
-            return func(a, b, *user_data)
-
-        return wrap
-
     def get_album_model(self, media):
         disc_model = Gio.ListStore()
         disc_model_sort = Gfm.SortListModel.new(disc_model)
@@ -201,7 +191,7 @@ class CoreModel(GObject.GObject):
             return disc_a.props.disc_nr - disc_b.props.disc_nr
 
         disc_model_sort.set_sort_func(
-            self._wrap_list_store_sort_func(_disc_order_sort))
+            utils.wrap_list_store_sort_func(_disc_order_sort))
 
         self.props.grilo.get_album_discs(media, disc_model)
 
@@ -219,7 +209,7 @@ class CoreModel(GObject.GObject):
             return album_a.props.year > album_b.props.year
 
         albums_model_sort.set_sort_func(
-            self._wrap_list_store_sort_func(_album_sort))
+            utils.wrap_list_store_sort_func(_album_sort))
 
         return albums_model_sort
 
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index 013295fa..7104fcac 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -29,11 +29,11 @@ import time
 import gi
 gi.require_version('GstPbutils', '1.0')
 from gi.repository import GObject, GstPbutils
-from gi._gi import pygobject_new_full
 
 from gnomemusic.coresong import CoreSong
 from gnomemusic.gstplayer import GstPlayer, Playback
 from gnomemusic.widgets.songwidget import SongWidget
+import gnomemusic.utils as utils
 
 
 class RepeatMode(IntEnum):
@@ -247,22 +247,13 @@ class PlayerPlaylist(GObject.GObject):
         return None
 
     def _on_repeat_mode_changed(self, klass, param):
-
-        def _wrap_list_store_sort_func(func):
-            def wrap(a, b, *user_data):
-                a = pygobject_new_full(a, False)
-                b = pygobject_new_full(b, False)
-                return func(a, b, *user_data)
-
-            return wrap
-
         # FIXME: This shuffle is too simple.
         def _shuffle_sort(song_a, song_b):
             return randint(-1, 1)
 
         if self.props.repeat_mode == RepeatMode.SHUFFLE:
             self._model.set_sort_func(
-                _wrap_list_store_sort_func(_shuffle_sort))
+                utils.wrap_list_store_sort_func(_shuffle_sort))
         elif self.props.repeat_mode in [RepeatMode.NONE, RepeatMode.ALL]:
             self._model.set_sort_func(None)
 
diff --git a/gnomemusic/songliststore.py b/gnomemusic/songliststore.py
index baf80810..ecd0c77c 100644
--- a/gnomemusic/songliststore.py
+++ b/gnomemusic/songliststore.py
@@ -23,7 +23,6 @@
 # delete this exception statement from your version.
 
 from gi.repository import Gfm, Gio, GObject, Gtk
-from gi._gi import pygobject_new_full
 
 import gnomemusic.utils as utils
 
@@ -39,7 +38,7 @@ class SongListStore(Gtk.ListStore):
 
         self._model = Gfm.SortListModel.new(model)
         self._model.set_sort_func(
-            self._wrap_list_store_sort_func(self._songs_sort))
+            utils.wrap_list_store_sort_func(self._songs_sort))
 
         self.set_column_types([
             GObject.TYPE_STRING,    # play or invalid icon
@@ -56,15 +55,6 @@ class SongListStore(Gtk.ListStore):
 
         self._model.connect("items-changed", self._on_items_changed)
 
-    def _wrap_list_store_sort_func(self, func):
-
-        def wrap(a, b, *user_data):
-            a = pygobject_new_full(a, False)
-            b = pygobject_new_full(b, False)
-            return func(a, b, *user_data)
-
-        return wrap
-
     def _songs_sort(self, song_a, song_b):
         title_a = song_a.props.title
         title_b = song_b.props.title
diff --git a/gnomemusic/utils.py b/gnomemusic/utils.py
index 8090cf27..b00f5ec1 100644
--- a/gnomemusic/utils.py
+++ b/gnomemusic/utils.py
@@ -28,6 +28,7 @@ import unicodedata
 
 from gettext import gettext as _
 from gi.repository import Gio
+from gi._gi import pygobject_new_full
 
 
 class SongStateIcon(Enum):
@@ -167,3 +168,14 @@ def natural_sort_names(name_a, name_b):
                 for tmp in re.split(r"(\d+)", normalize_caseless(text))]
 
     return _extract_numbers(name_b) < _extract_numbers(name_a)
+
+
+def wrap_list_store_sort_func(func):
+    """PyGI wrapper for SortListModel set_sort_func.
+    """
+    def wrap(a, b, *user_data):
+        a = pygobject_new_full(a, False)
+        b = pygobject_new_full(b, False)
+        return func(a, b, *user_data)
+
+    return wrap


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