[tracker/wip/carlosg/doc-updates: 19/23] docs: Add python examples




commit a26d19c44f806cd7e2c84f0804b34241dda3107c
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon Mar 14 01:46:32 2022 +0100

    docs: Add python examples

 docs/reference/libtracker-sparql/examples.md       | 28 +++++++++++++++---
 .../examples/connection-example.py                 | 33 ++++++++++++++++++++++
 .../libtracker-sparql/examples/endpoint-example.py | 25 ++++++++++++++++
 .../libtracker-sparql/examples/notifier-example.py | 26 +++++++++++++++++
 .../examples/private-store-example.py              | 28 ++++++++++++++++++
 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,10 +34,15 @@ 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,10 +65,15 @@ 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,10 +87,15 @@ 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)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]