---
author: einar
comments: true
date: 2010-10-26 19:37:16+00:00
layout: post
slug: pykde4-tag-and-annotate-files-using-nepomuk
title: 'PyKDE4: Tag and annotate files using Nepomuk'
wordpress_id: 803
categories:
- KDE
- Linux
tags:
- KDE
- python
- semantic desktop
---

Some time has passed since I last blogged... this was not only due to lack of time but also due to motivation (writing long texts can be discouraging at times). In any case, I'd like to rectify for that. In this post, I'll talk about Nepomuk, and in particular how to use it to tag and annotate arbitrary files using its API in PyKDE4.

Before starting, let me say that creating this tutorial was only possible thanks to the help of Sebastian Trueg, who helped me by pointing out some mistakes I was doing.

The example here is not showing the extra methods to set up a KApplication, etc.:  the full code for this tutorial[ is available in the kdeexamples module](http://websvn.kde.org/trunk/KDE/kdeexamples/bindings/python/nepomuk/). 

Let's start with the basics.


{% highlight python %}import sys
from PyQt4 import QtCore
from PyKDE4 import kdecore
from PyKDE4 import kdeui
from PyKDE4.nepomuk import Nepomuk{% endhighlight %}


This will import all the bits needed to test our experiment. As a second step, we'll create a dummy empty file.


{% highlight python %}dummy_file = open("dummy.txt", "w")
dummy_file.write("Some text\n")
dummy_file.close(){% endhighlight %}


Or, if we have Python 2.6+ (as pointed out in the comments):


{% highlight python %}
with open("dummy.txt", "w") as handle:
    handle.write("Some text\n")
{% endhighlight %}


Now that we have our file, it's time to do something productive with it. But first and foremost, we have to ensure that Nepomuk is running. To do so, we make a simple check (EDIT: fixed the syntax):


{% highlight python %}result = Nepomuk.ResourceManager.instance().init()
if result != 0:
     return{% endhighlight %}


Neomuk.instance().init() must return 0 if Nepomuk is properly set up. Once this is taken care of, we can manipulate the semantic information of our file. Thus, Nepomuk needs to be made aware of it: this is done by creating a _resource_ that points to the actual file:


{% highlight python %}file_info = QtCore.QFileInfo("dummy.txt")
absolute_path = file_info.absoluteFilePath()
resource = Nepomuk.Resource(kdecore.KUrl(absolute_path){% endhighlight %}


Notice that we **must** use an absolute file path, or the resource will be not created properly and although no errors will happen when tagging, changes will not be made. Let's now create a tag, which is done by simply constructing a Nepomuk.Tag instance:


{% highlight python %}tag = Nepomuk.Tag("test_example")
tag.setLabel("test_example"){% endhighlight %}


In the first line we create the tag, then we associate it with a label, so that it will be displayed in applications such as Dolphin. The nice thing is that if the Tag already exists, it will be recycled: no duplicates will occur. A simple call to addTag to the resource we created earlier will now tag it:


{% highlight python %}resource.addTag(tag){% endhighlight %}


We can also add comments that can show up in Dolphin as well by using the setDescription method:


{% highlight python %}resource.setDescription("This is an example comment."){% endhighlight %}


What if we want to remove tags and descriptions? To wipe them all, we can use the remove() method of the Resource, otherwise we can strip elements by using removeProperty along with the tagUri() or descriptionUri() methods of the resource:


{% highlight python %}resource.remove() # strip everything
resource.removeProperty(resource.descriptionUri()) # remove comment
resource.removeProperty(resource.tagUri()) # remove tags
{% endhighlight %}


That's it. As you can see, adding semantic information from PyKDE4 isn't that hard. Sooner or later I'll try my hand at queries and report back my findings.
