[tracker/sam/examples] 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] examples/python: Update to Python 3 and add automated testing
- Date: Sun, 22 Nov 2020 23:17:08 +0000 (UTC)
commit af9760e37d08353ee2d4f01cabdc10649305afb2
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/endpoint.py | 2 ++
examples/python/meson.build | 24 ++++++++++++++++
examples/python/query-async.py | 64 +++++++++++++++++++++++++++++++-----------
examples/python/query-sync.py | 45 +++++++++++++++++++++++++----
meson.build | 2 +-
utils/meson.build | 2 ++
7 files changed, 118 insertions(+), 22 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/endpoint.py b/examples/python/endpoint.py
old mode 100644
new mode 100755
index aaa811c80..503e61445
--- a/examples/python/endpoint.py
+++ b/examples/python/endpoint.py
@@ -41,6 +41,8 @@ def main():
# The database schemas.
ontology_path = Tracker.sparql_get_ontology_nepomuk()
+ if 'TEST_ONTOLOGIES_DIR' in os.environ:
+ ontology_path = Gio.File.new_for_path(os.environ['TEST_ONTOLOGIES_DIR'])
cancellable = None
diff --git a/examples/python/meson.build b/examples/python/meson.build
new file mode 100644
index 000000000..683804cf1
--- /dev/null
+++ b/examples/python/meson.build
@@ -0,0 +1,24 @@
+python = find_program('python3')
+
+env = environment()
+env.prepend('GI_TYPELIB_PATH', tracker_sparql_uninstalled_dir)
+env.prepend('LD_LIBRARY_PATH', tracker_sparql_uninstalled_dir)
+env.prepend('PYTHONPATH', tracker_uninstalled_testutils_dir)
+env.set('TRACKER_EXAMPLES_AUTOMATED_TEST', '1')
+env.set('TEST_ONTOLOGIES_DIR', tracker_uninstalled_nepomuk_ontologies_dir)
+
+sandbox_python_args = ['-m', 'trackertestutils', '--store-tmpdir', '--dbus-config',
meson.current_build_dir() / '..' / '..' / 'tests' / 'test-bus.conf']
+
+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: sandbox_python_args + [file],
+ env: env,
+ suite: 'examples')
+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})")
diff --git a/meson.build b/meson.build
index 3121bfcac..1a764782f 100644
--- a/meson.build
+++ b/meson.build
@@ -297,7 +297,6 @@ typelib_dir = gobject_introspection.get_pkgconfig_variable('typelibdir',
subdir('src')
subdir('docs')
-subdir('examples')
subdir('utils')
test_c_args = tracker_c_args + [
@@ -311,6 +310,7 @@ tracker_uninstalled_stop_words_dir = join_paths(meson.current_source_dir(), 'src
tracker_uninstalled_testutils_dir = join_paths(meson.current_source_dir(), 'utils')
subdir('tests')
+subdir('examples')
subdir('po')
diff --git a/utils/meson.build b/utils/meson.build
index c9431bb74..d571a582b 100644
--- a/utils/meson.build
+++ b/utils/meson.build
@@ -1,3 +1,5 @@
subdir('mtp')
subdir('tracker-resdump')
subdir('trackertestutils')
+
+utils_dir = meson.current_source_dir()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]