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



commit 07f5f46c2caf3908418f1da38a675b7bed44b891
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 | 87 +++++++++++++++++++++++--------------------------
 gnomemusic/query.py     |  3 ++
 gnomemusic/tracker.py   | 47 ++++++++++++++++++++++++++
 5 files changed, 96 insertions(+), 80 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..3081857d 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,16 @@ 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 +298,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,15 +344,13 @@ class Playlists(GObject.GObject):
 
         def update_callback(conn, res, data):
             playlist_urn = conn.update_blank_finish(res)[0][0]['playlist']
-            self.tracker.query_async(
-                Query.get_playlist_with_urn(playlist_urn),
-                None, query_callback, None
-            )
+            self._tracker.query_async(
+                Query.get_playlist_with_urn(playlist_urn), None,
+                query_callback, None)
 
-        self.tracker.update_blank_async(
-            Query.create_playlist(title), GLib.PRIORITY_LOW,
-            None, update_callback, None
-        )
+        self._tracker.update_blank_async(
+            Query.create_playlist(title), GLib.PRIORITY_LOW, None,
+            update_callback, None)
 
     @log
     def rename(self, item, new_name):
@@ -369,7 +367,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,10 +377,9 @@ 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
-        )
+            None, update_callback, None)
 
     @log
     def add_to_playlist(self, playlist, items):
@@ -396,25 +393,22 @@ class Playlists(GObject.GObject):
                 return
             entry_id = cursor.get_integer(0)
             grilo.get_playlist_song_with_id(
-                playlist.get_id(), entry_id, get_callback
-            )
+                playlist_id, entry_id, get_callback)
 
         def update_callback(conn, res, data):
             entry_urn = conn.update_blank_finish(res)[0][0]['entry']
-            self.tracker.query_async(
-                Query.get_playlist_song_with_urn(entry_urn),
-                None, query_callback, None
-            )
+            self._tracker.query_async(
+                Query.get_playlist_song_with_urn(entry_urn), None,
+                query_callback, None)
 
+        playlist_id = playlist.get_id()
         for item in items:
             uri = item.get_url()
             if not uri:
                 continue
-            self.tracker.update_blank_async(
-                Query.add_song_to_playlist(playlist.get_id(), uri),
-                GLib.PRIORITY_LOW,
-                None, update_callback, None
-            )
+            self._tracker.update_blank_async(
+                Query.add_song_to_playlist(playlist_id, uri),
+                GLib.PRIORITY_LOW, None, update_callback, None)
 
     @log
     def remove_from_playlist(self, playlist, items):
@@ -422,14 +416,12 @@ class Playlists(GObject.GObject):
             conn.update_finish(res)
             self.emit('song-removed-from-playlist', playlist, data)
 
+        playlist_id = playlist.get_id()
         for item in items:
-            self.tracker.update_async(
-                Query.remove_song_from_playlist(
-                    playlist.get_id(), item.get_id()
-                ),
-                GLib.PRIORITY_LOW,
-                None, update_callback, item
-            )
+            item_id = item.get_id()
+            self._tracker.update_async(
+                Query.remove_song_from_playlist(playlist_id, item_id),
+                GLib.PRIORITY_LOW, None, update_callback, item)
 
     @log
     def reorder_playlist(self, playlist, items, new_positions):
@@ -442,10 +434,11 @@ class Playlists(GObject.GObject):
         def update_callback(conn, res, data):
             conn.update_finish(res)
 
+        playlist_id = playlist.get_id()
         for item, new_position in zip(items, new_positions):
-            self.tracker.update_async(
-                Query.change_song_position(
-                    playlist.get_id(), item.get_id(), new_position),
+            item_id = item.get_id()
+            self._tracker.update_async(
+                Query.change_song_position(playlist_id, item_id, new_position),
                 GLib.PRIORITY_LOW, None, update_callback, item)
 
     @log
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]