ffmmx's Blog

Happy coding

[笔记]pykde的nepomuk 资源操作以及标签和评分建立.

pykde 允许对nepomuk的操作是十分有限的(实际上通过dbus的方式来走还要好些), 下面是一个用pykde 创建一个NEPOMUK资源的非常简单的例子.我在pykde的api中没有找到创建索引的相关的函数,以至于即便是有了对应的资源,可以在dolphin里面完全展示,但是通过nepomuk.query去搜索还是找不到资源信息.

如果有知道的朋友,希望分享一下.谢谢.

nepomuk资源创建和设置代码:

nepomuk资源创建和设置
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
#!/usr/bin/env python2
# coding:utf8
# filename:nepomuk_test.py
# descption:a example for nepomuk
 
 
import sys
from PyQt4 import QtCore
from PyKDE4 import kdecore
from PyKDE4 import kdeui
from PyKDE4.nepomuk import Nepomuk
 
 
dummy_file = open('fummy.txt','w')
dummy_file.write('测试\n')
dummy_file.close()
 
result = Nepomuk.ResourceManager.instance().init() # create resoucemanager and connect to nepomuk resource database ,return 0 is ok,not is error.
if result != 0:
    sys.exit(result)
 
file_info = QtCore.QFileInfo("fummy.txt")
absolute_path = file_info.absoluteFilePath()
resource = Nepomuk.Resource(kdecore.KUrl(absolute_path)) # create or load a resource
 
tagText = "tagtest"
tag = Nepomuk.Tag(tagText)
tag.setLabel(tagText)
 
resource.addTag(tag) # add tag
resource.setDescription("This is an example comment.") # set description
resource.setRating(4); # set rating
 
print "resource.exists() == ",resource.exists()
print "resource.isVaid() == ",resource.isValid()
print "resource.identifiers().length == ", len(resource.identifiers())
print "resource.type() == ",resource.type()
print "resource.types() == ",resource.types()
print "resource.resourceUri() == ",resource.resourceUri()
print "resource.resourceType () == ",resource.resourceType()
print "resource.identifierUri() == ",resource.identifierUri()