[gnome-music/wip/jfelder/tracker-not-found-view: 1/2] tracker: Simplify TrackerWrapper instanciation



commit 7ea55a9d97d0b20edb155a97c039d8bf11b636f8
Author: Jean Felder <jfelder src gnome org>
Date:   Wed Jan 30 10:51:26 2019 +0100

    tracker: Simplify TrackerWrapper instanciation
    
    Remove all the singleton logic. Instanciate TrackerWrapper in Grilo
    class and then pass it to Playlists class.

 gnomemusic/__init__.py  | 37 +++++++++++++++----------------------
 gnomemusic/grilo.py     |  3 ++-
 gnomemusic/playlists.py | 43 +++++++++++++++++++++----------------------
 3 files changed, 38 insertions(+), 45 deletions(-)
---
diff --git a/gnomemusic/__init__.py b/gnomemusic/__init__.py
index 452bfdbd..6da3044c 100644
--- a/gnomemusic/__init__.py
+++ b/gnomemusic/__init__.py
@@ -31,7 +31,7 @@ import logging
 
 import gi
 gi.require_version('Tracker', '2.0')
-from gi.repository import Tracker
+from gi.repository import GObject, Tracker
 
 logger = logging.getLogger(__name__)
 tabbing = 0
@@ -77,26 +77,19 @@ def log(fn):
     return wrapped
 
 
-class TrackerWrapper:
-    class __TrackerWrapper:
-        def __init__(self):
-            try:
-                self.tracker = Tracker.SparqlConnection.get(None)
-            except Exception as e:
-                from sys import exit
-                logger.error(
-                    "Cannot connect to tracker, error {}\nExiting".format(
-                        str(e)))
-                exit(1)
-
-        def __str__(self):
-            return repr(self)
-
-    _instance = None
+class TrackerWrapper(GObject.GObject):
+    """Create a connection to an instance of Tracker"""
 
     def __init__(self):
-        if not TrackerWrapper._instance:
-            TrackerWrapper._instance = TrackerWrapper.__TrackerWrapper()
-
-    def __getattr__(self, name):
-        return getattr(self._instance, name)
+        self._tracker = None
+        try:
+            self._tracker = Tracker.SparqlConnection.get(None)
+        except Exception as e:
+            from sys import exit
+            logger.error(
+                "Cannot connect to tracker, error {}\nExiting".format(str(e)))
+            exit(1)
+
+    @GObject.Property(type=object, flags=GObject.ParamFlags.READABLE)
+    def tracker(self):
+        return self._tracker
diff --git a/gnomemusic/grilo.py b/gnomemusic/grilo.py
index 65d0373f..5f90e41f 100644
--- a/gnomemusic/grilo.py
+++ b/gnomemusic/grilo.py
@@ -123,7 +123,8 @@ class Grilo(GObject.GObject):
 
         self.registry = Grl.Registry.get_default()
 
-        self.sparqltracker = TrackerWrapper().tracker
+        tracker_wrapper = TrackerWrapper()
+        self.sparqltracker = tracker_wrapper.props.tracker
 
         self._find_sources()
 
diff --git a/gnomemusic/playlists.py b/gnomemusic/playlists.py
index da427a6b..d7559270 100644
--- a/gnomemusic/playlists.py
+++ b/gnomemusic/playlists.py
@@ -29,7 +29,6 @@
 import gi
 gi.require_version('Grl', '0.3')
 from gi.repository import Grl, GLib, GObject
-from gnomemusic import TrackerWrapper
 from gnomemusic.grilo import grilo
 from gnomemusic.query import Query
 from gettext import gettext as _
@@ -131,7 +130,7 @@ class Playlists(GObject.GObject):
     }
 
     instance = None
-    tracker = None
+    _tracker = None
 
     def __repr__(self):
         return '<Playlists>'
@@ -148,7 +147,6 @@ class Playlists(GObject.GObject):
     def __init__(self):
         super().__init__()
 
-        self.tracker = TrackerWrapper().tracker
         self._static_playlists = StaticPlaylists()
 
         grilo.connect('ready', self._on_grilo_ready)
@@ -186,9 +184,10 @@ class Playlists(GObject.GObject):
             # Search for the playlist ID
             cursor.next_async(None, playlist_id_fetched_cb, playlist)
 
+        self._tracker = grilo.sparqltracker
         # Start fetching all the static playlists
         for playlist in self._static_playlists.get_all():
-            self.tracker.query_async(
+            self._tracker.query_async(
                 Query.get_playlist_with_tag(playlist.TAG_TEXT), None,
                 callback, playlist)
 
@@ -234,7 +233,7 @@ class Playlists(GObject.GObject):
             query = Query.get_playlist_with_urn(playlist_urn)
 
             # Start fetching the playlist
-            self.tracker.query_async(
+            self._tracker.query_async(
                 query, None, playlist_queried_cb, playlist)
 
         def tag_created_cb(obj, res, playlist):
@@ -242,14 +241,14 @@ class Playlists(GObject.GObject):
             creation_query = Query.create_playlist_with_tag(title, tag_text)
 
             # Start creating the playlist itself
-            self.tracker.update_blank_async(
+            self._tracker.update_blank_async(
                 creation_query, GLib.PRIORITY_LOW, None, playlist_created_cb,
                 playlist)
 
         # Start the playlist creation by creating the tag
-        self.tracker.update_blank_async(Query.create_tag(tag_text),
-                                        GLib.PRIORITY_LOW, None,
-                                        tag_created_cb, playlist)
+        self._tracker.update_blank_async(Query.create_tag(tag_text),
+                                         GLib.PRIORITY_LOW, None,
+                                         tag_created_cb, playlist)
 
     @log
     def update_static_playlist(self, playlist):
@@ -262,15 +261,15 @@ class Playlists(GObject.GObject):
     def clear_playlist(self, playlist):
         """Starts cleaning the playlist"""
         query = Query.clear_playlist_with_id(playlist.ID)
-        self.tracker.update_async(query, GLib.PRIORITY_LOW, None,
-                                  self._static_playlist_cleared_cb, playlist)
+        self._tracker.update_async(query, GLib.PRIORITY_LOW, None,
+                                   self._static_playlist_cleared_cb, playlist)
 
     @log
     def _static_playlist_cleared_cb(self, connection, res, playlist):
         """After clearing the playlist, start querying the playlist's songs"""
         # Get a list of matching songs
-        self.tracker.query_async(playlist.QUERY, None,
-                                 self._static_playlist_query_cb, playlist)
+        self._tracker.query_async(playlist.QUERY, None,
+                                  self._static_playlist_query_cb, playlist)
 
     @log
     def _static_playlist_query_cb(self, connection, res, playlist):
@@ -298,7 +297,7 @@ class Playlists(GObject.GObject):
 
                 cursor.next_async(None, callback, final_query)
             else:
-                self.tracker.update_blank_async(
+                self._tracker.update_blank_async(
                     final_query, GLib.PRIORITY_LOW, None,
                     self._static_playlist_update_finished, playlist)
 
@@ -344,12 +343,12 @@ class Playlists(GObject.GObject):
 
         def update_callback(conn, res, data):
             playlist_urn = conn.update_blank_finish(res)[0][0]['playlist']
-            self.tracker.query_async(
+            self._tracker.query_async(
                 Query.get_playlist_with_urn(playlist_urn),
                 None, query_callback, None
             )
 
-        self.tracker.update_blank_async(
+        self._tracker.update_blank_async(
             Query.create_playlist(title), GLib.PRIORITY_LOW,
             None, update_callback, None
         )
@@ -369,7 +368,7 @@ class Playlists(GObject.GObject):
             conn.update_finish(res)
             self.emit('playlist-renamed', item)
 
-        self.tracker.update_async(
+        self._tracker.update_async(
             Query.rename_playlist(item.get_id(), new_name), GLib.PRIORITY_LOW,
             None, update_callback, None)
 
@@ -379,7 +378,7 @@ class Playlists(GObject.GObject):
             conn.update_finish(res)
             self.emit('playlist-deleted', item)
 
-        self.tracker.update_async(
+        self._tracker.update_async(
             Query.delete_playlist(item.get_id()), GLib.PRIORITY_LOW,
             None, update_callback, None
         )
@@ -401,7 +400,7 @@ class Playlists(GObject.GObject):
 
         def update_callback(conn, res, data):
             entry_urn = conn.update_blank_finish(res)[0][0]['entry']
-            self.tracker.query_async(
+            self._tracker.query_async(
                 Query.get_playlist_song_with_urn(entry_urn),
                 None, query_callback, None
             )
@@ -410,7 +409,7 @@ class Playlists(GObject.GObject):
             uri = item.get_url()
             if not uri:
                 continue
-            self.tracker.update_blank_async(
+            self._tracker.update_blank_async(
                 Query.add_song_to_playlist(playlist.get_id(), uri),
                 GLib.PRIORITY_LOW,
                 None, update_callback, None
@@ -423,7 +422,7 @@ class Playlists(GObject.GObject):
             self.emit('song-removed-from-playlist', playlist, data)
 
         for item in items:
-            self.tracker.update_async(
+            self._tracker.update_async(
                 Query.remove_song_from_playlist(
                     playlist.get_id(), item.get_id()
                 ),
@@ -443,7 +442,7 @@ class Playlists(GObject.GObject):
             conn.update_finish(res)
 
         for item, new_position in zip(items, new_positions):
-            self.tracker.update_async(
+            self._tracker.update_async(
                 Query.change_song_position(
                     playlist.get_id(), item.get_id(), new_position),
                 GLib.PRIORITY_LOW, None, update_callback, item)


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