[tracker-miners/sam/examples: 2/2] examples: Add Python example of querying data from Tracker Miner FS




commit 63fc0e3f96c6e0d6ef4d709122d8e77dee10255c
Author: Sam Thursfield <sam afuera me uk>
Date:   Mon Nov 23 20:53:51 2020 +0100

    examples: Add Python example of querying data from Tracker Miner FS
    
    These were previously in tracker.git, but were moved here so they can
    be tested as part of CI. I've also updated them to Python 3.
    
    See https://gitlab.gnome.org/GNOME/tracker/-/merge_requests/341
    
    Fixes https://gitlab.gnome.org/GNOME/tracker/-/issues/273

 examples/meson.build           |  1 +
 examples/python/meson.build    | 26 +++++++++++++++++++
 examples/python/query-async.py | 58 ++++++++++++++++++++++++++++++++++++++++++
 examples/python/query-sync.py  | 45 ++++++++++++++++++++++++++++++++
 meson.build                    | 12 +++++----
 5 files changed, 137 insertions(+), 5 deletions(-)
---
diff --git a/examples/meson.build b/examples/meson.build
new file mode 100644
index 000000000..4d5f338fb
--- /dev/null
+++ b/examples/meson.build
@@ -0,0 +1 @@
+subdir('python')
diff --git a/examples/python/meson.build b/examples/python/meson.build
new file mode 100644
index 000000000..f45be68a6
--- /dev/null
+++ b/examples/python/meson.build
@@ -0,0 +1,26 @@
+python = find_program('python3')
+
+env = environment()
+
+# Needed to run the examples from the source tree, instead of
+# relying on host-installed Tracker Miners.
+env.prepend('LD_LIBRARY_PATH', tracker_sparql_uninstalled_dir)
+env.set('TEST_ONTOLOGIES_DIR', tracker_uninstalled_nepomuk_ontologies_dir)
+
+# Tell endpoint example to quit.
+env.set('TRACKER_EXAMPLES_AUTOMATED_TEST', '1')
+
+sandbox_python_args = [run_uninstalled, '--store-tmpdir']
+
+examples = [
+  'query-async',
+  'query-sync',
+]
+
+foreach example_name: examples
+  file = meson.current_source_dir() / '@0@.py'.format(example_name)
+  test(example_name, python,
+    args: sandbox_python_args + [file],
+    env: env,
+    suite: 'examples')
+endforeach
diff --git a/examples/python/query-async.py b/examples/python/query-async.py
new file mode 100755
index 000000000..e96933d83
--- /dev/null
+++ b/examples/python/query-async.py
@@ -0,0 +1,58 @@
+#!/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
+gi.require_version('Tracker', '3.0')
+from gi.repository import Tracker, GLib
+
+
+def results_ready_cb(obj, result, user_data):
+    cursor = obj.query_finish(result)
+    loop = user_data
+
+    # 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 = 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
new file mode 100755
index 000000000..f7b26e385
--- /dev/null
+++ b/examples/python/query-sync.py
@@ -0,0 +1,45 @@
+#!/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
+gi.require_version('Tracker', '3.0')
+from gi.repository import Tracker
+
+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)
+
+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})")
diff --git a/meson.build b/meson.build
index 1e0e72412..048871d5c 100644
--- a/meson.build
+++ b/meson.build
@@ -442,10 +442,6 @@ test_c_args = tracker_c_args + [
 
 subdir('tests')
 
-subdir('po')
-
-meson.add_install_script('meson_integration_commands.sh', glib_compile_schemas.path(), gsettings_schema_dir)
-
 run_uninstalled_conf = configuration_data()
 run_uninstalled_conf.set('tracker_sparql_uninstalled_dir', tracker_sparql_uninstalled_dir)
 run_uninstalled_conf.set('tracker_uninstalled_cli_dir', tracker_uninstalled_cli_dir)
@@ -458,11 +454,17 @@ run_uninstalled_conf.set('tracker_uninstalled_stop_words_dir', tracker_uninstall
 run_uninstalled_conf.set('tracker_uninstalled_testutils_dir', tracker_uninstalled_testutils_dir)
 run_uninstalled_conf.set('tracker_uninstalled_writeback_modules_dir', 
tracker_uninstalled_writeback_modules_dir)
 run_uninstalled_conf.set('uninstalled_gsettings_schema_dir', tracker_miners_uninstalled_gsettings_schema_dir)
-configure_file(
+run_uninstalled = configure_file(
   input: 'run-uninstalled.in',
   output: 'run-uninstalled',
   configuration: run_uninstalled_conf)
 
+subdir('examples')
+
+subdir('po')
+
+meson.add_install_script('meson_integration_commands.sh', glib_compile_schemas.path(), gsettings_schema_dir)
+
 summary = [
   '\nBuild Configuration:',
   '    Prefix:                                 ' + get_option('prefix'),


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