def _setup_local_db(self):
        # Open a local Tracker database.
        try:
            self._local_db = Tracker.SparqlConnection.new(
                Tracker.SparqlConnectionFlags.NONE,
                Gio.File.new_for_path(self.cache_directory()),
                Tracker.sparql_get_ontology_nepomuk(), None)
        except GLib.Error as error:
            self._log.warning("Error: {}, {}".format(error.domain,
                                                     error.message))
            self.notify("tracker-available")
            return

        # Version checks against the local version of Tracker can be done
        # here, set `self._tracker_available = TrackerState.OUTDATED` if the
        # checks fail.
        self._local_db_available = TrackerState.AVAILABLE
        self.notify("tracker-available")
Beispiel #2
0
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()
#!/usr/bin/python3

import gi, sys
from gi.repository import GLib, Gio, Tracker

try:
    connection = Tracker.SparqlConnection.new(
        Tracker.SparqlConnectionFlags.NONE,
        None,  # Database location, None creates it in-memory
        Tracker.sparql_get_ontology_nepomuk(),  # Ontology location
        None)

    # Create a resource containing RDF data
    resource = Tracker.Resource.new(None)
    resource.set_uri('rdf:type', 'nmm:MusicPiece')

    # Create a batch, and add the resource to it
    batch = connection.create_batch()
    batch.add_resource(None, resource)

    # Execute the batch to insert the data
    batch.execute()

    connection.close()

except Exception as e:
    print('Error: {0}'.format(e))
    sys.exit(-1)