00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 from Koo.Common import Common
00029 from Koo import Rpc
00030
00031
00032
00033
00034
00035
00036
00037 def addInformationToFile( fileName, model, ids, field = None ):
00038 if not Common.isKdeAvailable:
00039 return
00040 if not isinstance(ids, list):
00041 ids = [ids]
00042 field = False
00043 try:
00044
00045 ratings = Rpc.session.call( '/semantic', 'rating', model, ids, field, Rpc.session.context )
00046
00047 allTags = Rpc.session.call( '/semantic', 'tags', model, ids, field, Rpc.session.context )
00048
00049 allDescriptions = Rpc.session.call( '/semantic', 'description', model, ids, field, Rpc.session.context )
00050
00051 allContacts = Rpc.session.call( '/semantic', 'contacts', model, ids, field, Rpc.session.context )
00052 except:
00053 ratings = {}
00054 allTags = {}
00055 allDescriptions = {}
00056 allContacts = {}
00057
00058
00059 rating = 0
00060 if ratings:
00061 for x in ratings.values():
00062 rating += x
00063 rating = rating / len(ratings)
00064
00065 tags = []
00066 for x in allTags.values():
00067 tags += x
00068 tags = list( set( tags ) )
00069
00070 description = '\n--\n'.join( set(allDescriptions.values()) )
00071
00072 contacts = []
00073 for x in allContacts.values():
00074 contacts += x
00075 contacts = list( set( contacts ) )
00076
00077 from PyKDE4.nepomuk import Nepomuk
00078 from PyKDE4.soprano import Soprano
00079
00080 resource = Nepomuk.Resource( 'file://%s' % fileName, Soprano.Vocabulary.Xesam.File() )
00081 resource.setTags( [Nepomuk.Tag( tag ) for tag in tags] )
00082 resource.setRating( max(rating,0) )
00083 resource.setDescription( description )
00084 if not resource.isValid():
00085 return
00086
00087 manager = Nepomuk.ResourceManager.instance()
00088
00089
00090 client = Soprano.Client.DBusClient( 'org.kde.NepomukStorage' )
00091 models = client.allModels()
00092 if not models:
00093 return
00094 model = client.createModel( models[0] )
00095
00096 emails = [ '<mailto:%s>' % contact for contact in contacts ]
00097 emails = ', '.join( emails )
00098 if emails:
00099
00100
00101 iterator = model.executeQuery( "PREFIX nco: <http://www.semanticdesktop.org/ontologies/2007/03/22/nco#> SELECT ?contact WHERE { ?contact nco:hasEmailAddress %s. }" % emails, Soprano.Query.QueryLanguageSparql )
00102 contacts = []
00103
00104
00105 while iterator.next():
00106 x = iterator.binding('contact')
00107 if x.isResource():
00108 contacts.append( unicode( x.uri() ) )
00109
00110 for contact in contacts:
00111 resource.addIsRelated( Nepomuk.Resource( x.uri() ).pimoThing() )
00112