PyKDE4: Queries with Nepomuk

In one of my previous blog posts I dealt with tagging files and resources with Nepomuk. But Nepomuk is not only about storing metadata, it is also about retrieving and interrogating data. Normally, this would mean querying the metadata database directly, using queries written in SPARQL. But this is not intuitive, can be inefficient (if you do things the wrong way) and error prone (oops, I messed up a parameter!). 

Fortunately, the Nepomuk developers have come up with a high level API to query already stored metadata, and today’s post will deal with querying tags in Nepomuk. As per the past tutorials, the full source code is available in the kdeexamples module.

Let’s start off with the basic imports:

import sys

import PyQt4.QtCore as QtCore

import PyKDE4.kdecore as kdecore
import PyKDE4.kdeui as kdeui
from PyKDE4.kio import KIO
from PyKDE4.nepomuk import Nepomuk
from PyKDE4.soprano import Soprano

Then let’s create a simple class that wil be used for the rest of this exercise:

class NepomukTagQueryExample(QtCore.QObject):

    def __init__(self, parent=None):

        super(NepomukTagQueryExample, self).__init__(parent)

__init__ is just used to construct the instance, nothing more. The bulk of the work is in the query_tag() function, which we’ll take a look at in parts.

    def query_tag(self, tag):

        """Query for a specific tag."""

        tag = Nepomuk.Tag(tag)

First of all we convert the tag we want to query into a proper Nepomuk.Tag() instance. Of course we should use an already existing tag: even if Nepomuk.Tag() automatically creates new tags, it makes little sense to query for a newly created tag, doesn’t it?

For our job, we need to use properties which define the terms of our query. As we’re looking for tags, we’ll use Soprano.Vocabulary.NAO.hasTag():

        soprano_term_uri = Soprano.Vocabulary.NAO.hasTag()
        nepomuk_property = Nepomuk.Types.Property(soprano_term_uri)

The first call generates an URI pointing to a specific RDF resource for this specific term, which is then wrapped as a Nepomuk.Types.Property in the second call. While the C++ API docs don’t show this, I found it to be necessary, or the Python interpreter would raise a TypeError. Notice that this is not the only term we can use: aside for tags, there are a lot of other URIs we can use for querying, listed in the Soprano API docs.

Once we have our property set up, it’s time to define which kind of query we’re going to use. In this case, since we want to check for the presence of tags, we use a Nepomuk.Query.ComparisonTerm, which is a query term used to match values of specific properties (in our case, tags):

        comparison_term = Nepomuk.Query.ComparisonTerm(nepomuk_property,
                Nepomuk.Query.ResourceTerm(tag))

Our tag is wrapped in a ResourceTerm, which is used exactly for the purpose. Now we make the proper query: in this specific case, we want to look up files tagged, so we use a FileQuery. We could also get other items, such as mails (in Akonadi): in that case we could use a a Nepomuk.Query.Query():

        query = Nepomuk.Query.FileQuery(comparison_term)

Lastly, we want to get some results out of this query. There are different methods, but for this tutorial we’ll use the tried-and-tested KIO technology:

        search_url = query.toSearchUrl()
        search_job = KIO.listDir(kdecore.KUrl(search_url))
        search_job.entries.connect(self.search_slot)
        search_job.result.connect(search_job.entries.disconnect)

First we convert the query to a nepomuksearch:// url, which then we pass to KIO.listDir, to list the entries. Unlike my previous post on KIO, this job emits entries() every time one is found, so we connect the signal to our search_slot method. We also connect the job’s result() signal in a way that it will disconnect the job once it’s over.

Finally, let’s take a look at the search_slot function:

    def search_slot(self, job, data):

        # We may get invalid entries, so skip those
        if not data:
            return

        for item in data:
            print item.stringValue(KIO.UDSEntry.UDS_DISPLAY_NAME)

Entries are emitted as UDSEntries: to get something at least understandable, we turn them into the file name, which is obtained by the stringValue() call using KIO.UDSEntry.UDS_DISPLAY_NAME.

That’s it. As you can see, it was pretty easy. Of course there’s more than that. For further reading, take a look at Nepomuk’s Query API docs, and Query Examples. Bear in mind however that to the best of my knowledge, the “fancy operators” mentioned there will not work with Python.

Happy Nepomuk querying!

Access multiple Google Calendars from KOrganizer

Recently, a question came up on the KDE Community Forums regarding the use of multiple Google Calendars with KOrganizer. The preferred access up to now has been with googledata Akonadi resource, however that doesn’t support more than one calendar, and (at least from my unscientific observation) seems to be rather unmaintained these days. 

Luckily, not all’s lost. Akonadi recently gained the opportunity of accessing CalDAV resources, and Google Calendar also offers a CalDAV interface, hence this is possible. 
This post will briefly describe how (thanks go to PIMster krop, which casually mentioned the possibility on IRC and prompted me to investigate).

Continue reading

Taking video snapshots quickly: KDE VLC Snapper

Some of the oldest readers of this blog are well aware of a certain hobby of mine. Over the years I’ve always wanted to write more about that, including the stuff I’m viewing nowadays, but I found a hassle to collect snapshots from videos / DVDs, selecting them, and so on. 

Recently I learnt that VLC has some rather complete Python bindings, and I thought, why not make the process automated? Yesterday I had some free time on my hands and a quick session of hacking brought some results already.
As the stuff is somewhat past prototypal stage, I thought I would push somewhere for others to use.  Lo and behold, here I present you KDE VLC Snapper.
As you can see, it’s a minimal dialog: just select your source video file (any file supported by VLC will do), the number of screencaps, the destination directory, and the program will do the rest. Currently it works somewhat OK (see caveats below) and is good enough for my use cases.

How do I get it?

Just clone this repository:
git clone http://git.gitorious.org/kde-vlc-snapper/kde-vlc-snapper.git

followed by

sudo python setup.py install

You can then invoke the program with

kdevlcsnapper
Requirements include PyKDE4 (tested on KDE Dev Platform 4.6), numpy (just for its “linspace” function, alternatives are welcome) and VLC installed (you don’t need the bindings, however: I provide a local copy).
What about bugs? Well, currently there are two issues that I’m unsure on how to fix: the first is a crash on exit, the second is that certain media files make VLC crash in the background when called from the bindings.
In any case, if you try it out, let me know what you think in the comments!

Improvements to the Git hooks

As you may already know, recently the KDE sysadmins completely overhauled the commit hooks used with the Git infrastructure. Written in Python, they have already brought significant improvements to the current workflows. These hooks include keywords that when specified trigger particular actions: the most used are  to CC specific email addresses (CCMAIL), to CC bug reports (CCBUG) or to close bug reports (BUG).

With the adoption of Review Board to facilitate code reviews, there were also requests for a REVIEW keyword that could close the review requests without asking the submitters to do so manually (which is slow and not always effective). Since the hooks for Git were written in Python, I thought I could give a hand there.

I looked into the Review Board API, which is a simple REST API: tasks are performed with HTTP GET, POST, or PUT. As I didn’t want to dive too much into the technicalities, I decided to use a wrapper that would make things easier: python-rest-client. Once that was in place, it was just a matter of adding some sugar to handle replies, errors and logging. All in 78 lines of code.

Now that the “field tests” passed with flying colors, I’m happy to announce that such a hook exists and is operational for KDE’s Git infrastructure. By using the REVIEW keyword at the start of a line, followed by a number, the hook will notify the Review Board instance and close the request. It will also publish a comment stating the commit’s SHA1 and the person who did it.

You can take a look at the finished results in this review request.

Credits for this also go to Ben “bcooksley” Cooksley for helping with testing and fixes, and Eike “Sho” Hein for helpful suggestions.