summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorCarlos Garnacho <carlosg@gnome.org>2022-03-14 01:46:32 +0100
committerCarlos Garnacho <carlosg@gnome.org>2022-04-03 18:47:17 +0000
commita26d19c44f806cd7e2c84f0804b34241dda3107c (patch)
treedbe45fc67f475068e9660d8f8177d326a7aab3dc /docs
parent1e9f6fee869030474323b78eeccb3b04227f26a8 (diff)
downloadtracker-a26d19c44f806cd7e2c84f0804b34241dda3107c.tar.gz
docs: Add python examples
Diffstat (limited to 'docs')
-rw-r--r--docs/reference/libtracker-sparql/examples.md28
-rwxr-xr-xdocs/reference/libtracker-sparql/examples/connection-example.py33
-rwxr-xr-xdocs/reference/libtracker-sparql/examples/endpoint-example.py25
-rwxr-xr-xdocs/reference/libtracker-sparql/examples/notifier-example.py26
-rwxr-xr-xdocs/reference/libtracker-sparql/examples/private-store-example.py28
5 files changed, 136 insertions, 4 deletions
diff --git a/docs/reference/libtracker-sparql/examples.md b/docs/reference/libtracker-sparql/examples.md
index d544838e5..710ad8702 100644
--- a/docs/reference/libtracker-sparql/examples.md
+++ b/docs/reference/libtracker-sparql/examples.md
@@ -34,11 +34,16 @@ main loop is not blocked while these operations are executed.
Once you end up with the query, remember to call [](tracker_sparql_cursor_close).
The same applies to [](tracker_sparql_connection_close) when no longer needed.
-<div class="gi-lang-c gi-lang-python gi-lang-javascript">
+<div class="gi-lang-c gi-lang-javascript">
{{ examples/connection-example.c }}
</div>
+<div class="gi-lang-python">
+
+{{ examples/connection-example.py }}
+
+</div>
## Creating a private database
@@ -60,11 +65,16 @@ main loop is not blocked while these operations are executed.
Once you no longer need the connection, remember to call
[](tracker_sparql_connection_close) on the [](TrackerSparqlConnection).
-<div class="gi-lang-c gi-lang-python gi-lang-javascript">
+<div class="gi-lang-c gi-lang-javascript">
{{ examples/private-store-example.c }}
</div>
+<div class="gi-lang-python">
+
+{{ examples/private-store-example.py }}
+
+</div>
## Creating a SPARQL endpoint
@@ -77,11 +87,16 @@ concretely the creation of a D-Bus endpoint, that other applications
may query e.g. through a connection created with
[](tracker_sparql_connection_bus_new).
-<div class="gi-lang-c gi-lang-python gi-lang-javascript">
+<div class="gi-lang-c gi-lang-javascript">
{{ examples/endpoint-example.c }}
</div>
+<div class="gi-lang-python">
+
+{{ examples/endpoint-example.py }}
+
+</div>
## Receiving notification on changes
@@ -94,8 +109,13 @@ on changes of certain RDF classes (Those with the
This example demonstrates the use of [](TrackerNotifier) to receive
notifications on database updates.
-<div class="gi-lang-c gi-lang-python gi-lang-javascript">
+<div class="gi-lang-c gi-lang-javascript">
{{ examples/notifier-example.c }}
</div>
+<div class="gi-lang-python">
+
+{{ examples/notifier-example.py }}
+
+</div>
diff --git a/docs/reference/libtracker-sparql/examples/connection-example.py b/docs/reference/libtracker-sparql/examples/connection-example.py
new file mode 100755
index 000000000..d8def7f77
--- /dev/null
+++ b/docs/reference/libtracker-sparql/examples/connection-example.py
@@ -0,0 +1,33 @@
+#!/usr/bin/python3
+
+import gi, sys
+from gi.repository import GLib, Gio, Tracker
+
+try:
+ connection = Tracker.SparqlConnection.bus_new(
+ 'org.freedesktop.Tracker3.Miner.Files',
+ None, None)
+
+ stmt = connection.query_statement (
+ 'SELECT DISTINCT nie:url(?u) WHERE { ' +
+ ' ?u a nfo:FileDataObject ; ' +
+ ' nfo:fileName ~name ' +
+ '}', None)
+
+ stmt.bind_string('name', sys.argv[1])
+
+ cursor = stmt.execute()
+ i = 0;
+
+ while cursor.next():
+ i += 1
+ print('Result {0}: {1}'.format(i, cursor.get_string(0)[0]))
+
+ print('A total of {0} results were found\n'.format(i))
+
+ cursor.close()
+ connection.close()
+
+except Exception as e:
+ print('Error: {0}'.format(e))
+ sys.exit(-1)
diff --git a/docs/reference/libtracker-sparql/examples/endpoint-example.py b/docs/reference/libtracker-sparql/examples/endpoint-example.py
new file mode 100755
index 000000000..654482511
--- /dev/null
+++ b/docs/reference/libtracker-sparql/examples/endpoint-example.py
@@ -0,0 +1,25 @@
+#!/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)
+
+ bus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
+
+ endpoint = Tracker.EndpointDBus.new(
+ connection, bus, None, None)
+
+ loop = GLib.MainLoop.new(None, False)
+ loop.run()
+
+ connection.close()
+
+except Exception as e:
+ print('Error: {0}'.format(e))
+ sys.exit(-1)
diff --git a/docs/reference/libtracker-sparql/examples/notifier-example.py b/docs/reference/libtracker-sparql/examples/notifier-example.py
new file mode 100755
index 000000000..5cc9cbeea
--- /dev/null
+++ b/docs/reference/libtracker-sparql/examples/notifier-example.py
@@ -0,0 +1,26 @@
+#!/usr/bin/python3
+
+import gi, sys
+from gi.repository import GLib, Gio, Tracker
+
+def callback(service, graph, events):
+ for event in events:
+ print('Event {0} on {1}\n'.format(
+ event.get_event_type(), event.get_urn()))
+
+try:
+ connection = Tracker.SparqlConnection.bus_new(
+ 'org.freedesktop.Tracker3.Miner.Files',
+ None, None)
+
+ notifier = connection.create_notifier()
+ notifier.connect('events', callback)
+
+ loop = GLib.MainLoop.new(None, False)
+ loop.run()
+
+ connection.close()
+
+except Exception as e:
+ print('Error: {0}'.format(e))
+ sys.exit(-1)
diff --git a/docs/reference/libtracker-sparql/examples/private-store-example.py b/docs/reference/libtracker-sparql/examples/private-store-example.py
new file mode 100755
index 000000000..61c1adbe5
--- /dev/null
+++ b/docs/reference/libtracker-sparql/examples/private-store-example.py
@@ -0,0 +1,28 @@
+#!/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)