[tracker/sam/examples: 8/9] examples: Add endpoint example




commit f0933246c384046de8e089e8bf097aa2ba94f16f
Author: Sam Thursfield <sam afuera me uk>
Date:   Sun Nov 22 22:16:58 2020 +0100

    examples: Add endpoint example

 examples/meson.build        |  1 +
 examples/python/endpoint.py | 79 +++++++++++++++++++++++++++++++++++++++++++++
 examples/python/meson.build | 24 ++++++++++++++
 meson.build                 |  2 +-
 utils/meson.build           |  2 ++
 5 files changed, 107 insertions(+), 1 deletion(-)
---
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
new file mode 100755
index 000000000..503e61445
--- /dev/null
+++ b/examples/python/endpoint.py
@@ -0,0 +1,79 @@
+#!/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.
+
+
+"""Demonstrates publishing a database over DBus."""
+
+import gi
+gi.require_version('Tracker', '3.0')
+from gi.repository import Gio
+from gi.repository import GLib
+from gi.repository import Tracker
+
+import logging
+import os
+import sys
+import tempfile
+
+
+def main():
+    logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
+
+    tmpdir = tempfile.mkdtemp(prefix='tracker-test-')
+
+    # Where the database is stored.
+    store_path = Gio.File.new_for_path(tmpdir)
+
+    # 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
+
+    # Create a new, empty database.
+    conn = Tracker.SparqlConnection.new(
+        Tracker.SparqlConnectionFlags.NONE,
+        store_path,
+        ontology_path,
+        cancellable)
+
+    bus = Gio.bus_get_sync(Gio.BusType.SESSION, cancellable)
+    unique_name = bus.get_unique_name()
+
+    # Publish our endpoint on DBus.
+    endpoint = Tracker.EndpointDBus.new(conn, bus, None, cancellable)
+
+    print(f"Exposing a Tracker endpoint on bus name {unique_name}")
+    print()
+    print(f"Try connecting over D-Bus using `tracker3 sparql`:")
+    print()
+    print(f"   tracker3 sparql --dbus-service={unique_name} -q ...")
+
+    loop = GLib.MainLoop.new(None, False)
+
+    if os.environ.get('TRACKER_EXAMPLES_AUTOMATED_TEST'):
+        GLib.timeout_add(10, lambda *args: loop.quit(), None)
+    else:
+        print()
+        print(f"Press CTRL+C to quit.")
+
+    loop.run()
+
+
+main()
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/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]