[tracker/sam/examples] examples/python: Update to Python 3 and add automated testing




commit 2c7c33868a0d91610c123945eb0b1e928ffd9ac8
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/meson.build    | 15 ++++++++++
 examples/python/query-async.py | 64 +++++++++++++++++++++++++++++++-----------
 examples/python/query-sync.py  | 45 +++++++++++++++++++++++++----
 4 files changed, 104 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/meson.build b/examples/python/meson.build
new file mode 100644
index 000000000..bca263900
--- /dev/null
+++ b/examples/python/meson.build
@@ -0,0 +1,15 @@
+python = find_program('python3')
+
+examples = [
+  'endpoint',
+  'query-async',
+  'query-sync',
+]
+
+foreach example_name: examples
+  file = meson.current_source_dir() / '@0@.py'.format(example_name)
+  test(example_name, python,
+    args: [file],
+    suite: 'examples',
+    env: 'TRACKER_EXAMPLES_AUTOMATED_TEST=1')
+endforeach
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]