[tracker/sam/examples: 5/5] examples/python: Update to Python 3 and add automated testing
- From: Sam Thursfield <sthursfield src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tracker/sam/examples: 5/5] examples/python: Update to Python 3 and add automated testing
- Date: Sun, 22 Nov 2020 21:17:33 +0000 (UTC)
commit 69fc357159c30d3f7ed88a9f7b7ebe2ead263bb1
Author: Sam Thursfield <sam afuera me uk>
Date: Sun Nov 22 22:17:08 2020 +0100
examples/python: Update to Python 3 and add automated testing
examples/meson.build | 1 +
examples/python/query-async.py | 64 +++++++++++++++++++++++++++++++-----------
examples/python/query-sync.py | 45 +++++++++++++++++++++++++----
3 files changed, 89 insertions(+), 21 deletions(-)
---
diff --git a/examples/meson.build b/examples/meson.build
index f788edc7d..9cfe5f8b4 100644
--- a/examples/meson.build
+++ b/examples/meson.build
@@ -1 +1,2 @@
subdir('libtracker-sparql')
+subdir('python')
diff --git a/examples/python/query-async.py b/examples/python/query-async.py
index 1077052dc..918c900fa 100755
--- a/examples/python/query-async.py
+++ b/examples/python/query-async.py
@@ -1,25 +1,57 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
+# Copyright (C) 2020, Sam Thursfield <sam afuera me uk>
+#
+# This program 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.
+#
+# This program 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 this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
+
+"""Example script to query all documents from the filesystem index.
+
+This uses an asynchronous query, which runs the query in a new thread and
+notifies the main loop when results are ready.
+
+"""
+
import gi
-from gi.repository import Tracker, GObject
+from gi.repository import Tracker, GLib
-def results_ready_cb (obj, result, user_data):
- cursor = obj.query_finish (result)
- # This can also be done asynchronously
- while (cursor.next (None)):
- print cursor.get_string (0)
+def results_ready_cb(obj, result, user_data):
+ cursor = obj.query_finish(result)
+ loop = user_data
- user_data.quit ()
+ # This can also be done asynchronously using next_async().
+ print("Documents:")
+ while (cursor.next(None)):
+ location = cursor.get_string(0)[0]
+ title = cursor.get_string(1)[0]
+ print(f" * {location} (title: {title})")
+ loop.quit ()
if __name__ == "__main__":
- loop = GObject.MainLoop ()
-
- # The connection can be requested asynchronously
- conn = Tracker.SparqlConnection.get (None)
- conn.query_async ("SELECT nie:url(?u) WHERE { ?u a nfo:FileDataObject }",
- None,
- results_ready_cb,
- loop)
+ loop = GLib.MainLoop ()
+ cancellable = None
+
+ miner_fs = Tracker.SparqlConnection.bus_new(
+ "org.freedesktop.Tracker3.Miner.Files", None, cancellable)
+ cursor = miner_fs.query_async(
+ "SELECT nie:isStoredAs(?doc) ?title "
+ "FROM tracker:Documents "
+ "WHERE { ?doc a nfo:Document ; nie:title ?title . }",
+ cancellable,
+ results_ready_cb,
+ loop)
loop.run ()
diff --git a/examples/python/query-sync.py b/examples/python/query-sync.py
index cf8bfd6f6..3c3d46ad8 100755
--- a/examples/python/query-sync.py
+++ b/examples/python/query-sync.py
@@ -1,9 +1,44 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
+# Copyright (C) 2020, Sam Thursfield <sam afuera me uk>
+#
+# This program 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.
+#
+# This program 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 this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
+
+"""Example script to query all music albums from the filesystem index.
+
+This uses a synchronous query, which blocks until results are ready. This can
+be useful in commandline applications, automated tests, and multithreaded
+apps.
+
+"""
+
import gi
from gi.repository import Tracker
-conn = Tracker.SparqlConnection.get (None)
-cursor = conn.query ("SELECT ?u WHERE { ?u a nie:InformationElement. }", None)
+cancellable = None
+
+miner_fs = Tracker.SparqlConnection.bus_new(
+ "org.freedesktop.Tracker3.Miner.Files", None, cancellable)
+cursor = miner_fs.query(
+ "SELECT ?album ?title "
+ "FROM tracker:Audio "
+ "WHERE { ?album a nmm:MusicAlbum ; nie:title ?title . }",
+ cancellable)
-while (cursor.next (None)):
- print cursor.get_string (0)
+print("Music albums:")
+while (cursor.next(None)):
+ album_id = cursor.get_string(0)[0]
+ album_title = cursor.get_string(1)[0]
+ print(f" * {album_title} (ID: {album_id})")
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]