[gnome-music/wip/jfelder/tracker-not-found-view: 6/7] tracker: Simplify TrackerWrapper instantiation



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

    tracker: Simplify TrackerWrapper instantiation
    
    Remove all the singleton logic and move it to its own file.
    Instantiate TrackerWrapper in Grilo class and then pass it to
    Playlists class.

 gnomemusic/__init__.py  | 29 -----------------------------
 gnomemusic/grilo.py     | 10 ++++++----
 gnomemusic/playlists.py | 43 +++++++++++++++++++++----------------------
 gnomemusic/query.py     |  3 +++
 gnomemusic/tracker.py   | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 77 insertions(+), 55 deletions(-)
---
diff --git a/gnomemusic/__init__.py b/gnomemusic/__init__.py
index 452bfdbd..d25af2f6 100644
--- a/gnomemusic/__init__.py
+++ b/gnomemusic/__init__.py
@@ -29,10 +29,6 @@ from itertools import chain
 from time import time
 import logging
 
-import gi
-gi.require_version('Tracker', '2.0')
-from gi.repository import Tracker
-
 logger = logging.getLogger(__name__)
 tabbing = 0
 
@@ -75,28 +71,3 @@ def log(fn):
         return retval
 
     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
-
-    def __init__(self):
-        if not TrackerWrapper._instance:
-            TrackerWrapper._instance = TrackerWrapper.__TrackerWrapper()
-
-    def __getattr__(self, name):
-        return getattr(self._instance, name)
diff --git a/gnomemusic/grilo.py b/gnomemusic/grilo.py
index 65d0373f..2d58425e 100644
--- a/gnomemusic/grilo.py
+++ b/gnomemusic/grilo.py
@@ -29,7 +29,8 @@ import gi
 gi.require_version('Grl', '0.3')
 from gi.repository import GLib, GObject
 from gnomemusic.query import Query
-from gnomemusic import log, TrackerWrapper
+from gnomemusic import log
+from gnomemusic.tracker import TrackerWrapper
 import logging
 import os
 os.environ['GRL_PLUGIN_RANKS'] = ("grl-local-metadata:5,"
@@ -123,7 +124,8 @@ class Grilo(GObject.GObject):
 
         self.registry = Grl.Registry.get_default()
 
-        self.sparqltracker = TrackerWrapper().tracker
+        tracker_wrapper = TrackerWrapper()
+        self.tracker_sparql = tracker_wrapper.props.tracker
 
         self._find_sources()
 
@@ -505,8 +507,8 @@ class Grilo(GObject.GObject):
 
         # TODO: currently just checks tracker, should work with any
         # queryable supported Grilo source.
-        self.sparqltracker.query_async(Query.all_songs_count(), None,
-                                       songs_query_cb, None)
+        self.tracker_sparql.query_async(Query.all_songs_count(), None,
+                                        songs_query_cb, None)
 
 
 grilo = Grilo()
diff --git a/gnomemusic/playlists.py b/gnomemusic/playlists.py
index da427a6b..e4f768c1 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.tracker_sparql
         # 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)
diff --git a/gnomemusic/query.py b/gnomemusic/query.py
index ac7707f8..755511c6 100644
--- a/gnomemusic/query.py
+++ b/gnomemusic/query.py
@@ -25,6 +25,9 @@
 # code, but you are not obligated to do so.  If you do not wish to do so,
 # delete this exception statement from your version.
 
+import gi
+gi.require_version('Tracker', '2.0')
+
 from gi.repository import GLib, Tracker
 from gnomemusic import log
 import os
diff --git a/gnomemusic/tracker.py b/gnomemusic/tracker.py
new file mode 100644
index 00000000..2283ff15
--- /dev/null
+++ b/gnomemusic/tracker.py
@@ -0,0 +1,47 @@
+# Copyright 2019 The GNOME Music developers
+# GNOME Music is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# GNOME Music is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with GNOME Music; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# The GNOME Music authors hereby grant permission for non-GPL compatible
+# GStreamer plugins to be used and distributed together with GStreamer
+# and GNOME Music.  This permission is above and beyond the permissions
+# granted by the GPL license by which GNOME Music is covered.  If you
+# modify this code, you may extend this exception to your version of the
+# code, but you are not obligated to do so.  If you do not wish to do so,
+# delete this exception statement from your version.
+
+import logging
+
+from gi.repository import GObject, Tracker
+
+logger = logging.getLogger(__name__)
+
+
+class TrackerWrapper(GObject.GObject):
+    """Create a connection to an instance of Tracker"""
+
+    def __init__(self):
+        super().__init__()
+        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


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