summaryrefslogtreecommitdiff
path: root/examples/python/endpoint.py
blob: 503e61445896278150bc540efcd651ccd34544c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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()