ffmmx's Blog

Happy coding

[蛋疼][备忘] pynepomukindexer 简单封装,手动的文件索引.

其实没啥什么好说的,我kde环境里面的nepomukfilewatch 服务进程启动会crash,暂时没有找到错误的原因,这样会造成文件改动以后触发的somethingChange 信号无法被正确的捕捉,并且调用对应索引程序来重新丢该资源进行处理.所以只要弄个手动的,暂时应急一下.

pynepomukindexer.py

#!/usr/bin/env python2
#coding:utf8
#filename:pynepomukindexer.py
#description:一个支持多文件和文件夹递归的nepomukindexer的封装.

'''
一个支持多文件和文件夹递归的nepomukindexer文件索引的封装.
'''
import os
import sys
from PyKDE4.nepomuk import Nepomuk


class PyNepomukIndexer(object):
    def __init__(self,currentPath):
        self.currentPath = currentPath
        self.indexerCmd = 'nepomukindexer'

    def index(self,path):
        if os.path.isdir(path):
            for f in [ os.path.realpath(path)+'/'+tf for tf in os.listdir(path) if tf[0] != '.' ]:
                self.index(f)
        else:
            print path
            os.system(self.indexerCmd+" '"+path+"'")


if __name__ == '__main__':
    pyindexer = PyNepomukIndexer(os.path.realpath('.'))
    
    if len(sys.argv) == 1:
        pyindexer.index(os.path.realpath('.'))
    else:
        for file in sys.argv[1:]:
            pyindexer.index(file)

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

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

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

nepomuk资源创建和设置代码:

#!/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()