File: query-async.py

package info (click to toggle)
localsearch 3.8.2-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,688 kB
  • sloc: ansic: 59,411; python: 3,774; xml: 261; perl: 106; sh: 62; makefile: 53
file content (58 lines) | stat: -rwxr-xr-x 1,900 bytes parent folder | download | duplicates (3)
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
#!/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.

"""Example script to query all documents from the filesystem index.

This uses an asynchronous query, which runs the query in a new thread and
notifies the main loop when results are ready.

"""

import gi
gi.require_version('Tracker', '3.0')
from gi.repository import Tracker, GLib


def results_ready_cb(obj, result, user_data):
    cursor = obj.query_finish(result)
    loop = user_data

    # This can also be done asynchronously using next_async().
    print("Documents:")
    while (cursor.next(None)):
        location = cursor.get_string(0)[0]
        title = cursor.get_string(1)[0]
        print(f"  * {location} (title: {title})")
    loop.quit ()


if __name__ == "__main__":
    loop = GLib.MainLoop ()
    cancellable = None

    miner_fs = Tracker.SparqlConnection.bus_new(
        "org.freedesktop.Tracker3.Miner.Files", None, cancellable)
    cursor = miner_fs.query_async(
        "SELECT nie:isStoredAs(?doc) ?title "
        "FROM tracker:Documents "
        "WHERE { ?doc a nfo:Document ; nie:title ?title . }",
        cancellable,
        results_ready_cb,
        loop)

    loop.run ()