def add_file_to_vocab_status(vocabprefix, properties, addHasFormat=True): vocabdir = os.path.join(ag.vocabulariesdir, vocabprefix) vocabstatusfile = os.path.join(vocabdir, "status.rdf") vocaburi = "http://vocab.ox.ac.uk/%s"%vocabprefix if not 'name' in properties or not properties['name'] or not 'path' in properties or not properties['path']: return False if not 'uri' in properties or not properties['uri']: properties['uri'] = URIRef("http://vocab.ox.ac.uk/%s/%s"%(vocabprefix, properties['name'])) if not 'format' in properties or not properties['format']: # get mimetype of file mt = None if os.path.isfile(vocabfile): if check_rdf(vocabfile): properties['format'] = 'application/rdf+xml' else: mt1 = mimetypes.guess_type(vocabfile) if mt1[0]: properties['format'] = mt1[0] else: properties['format'] = get_file_mimetype(vocabfile) graph = Graph() if os.path.isfile(vocabstatusfile): graph.parse(vocabstatusfile) else: return False for prefix, url in namespaces.iteritems(): graph.bind(prefix, URIRef(url)) if addHasFormat: graph.add((URIRef(vocaburi), namespaces['dcterms']['hasFormat'], URIRef(properties['uri']))) if properties['format']: graph.add((URIRef(properties['uri']), namespaces['dcterms']['format'], Literal(properties['format']))) if os.path.isfile(properties['path']): graph.add((URIRef(properties['uri']), namespaces['nfo']['fileUrl'], Literal('file://%s'%properties['path']))) graph.add((URIRef(properties['uri']), namespaces['nfo']['fileName'], Literal(properties['name']))) rdf_str = None rdf_str = graph.serialize() f = codecs.open(vocabstatusfile, 'w', 'utf-8') f.write(rdf_str) f.close() return True
def create_vocab_statusfile(userid, vocabprefix, vocabfile, baseuri, update=False, using_uuid=False, refvocab=False): vocab_uri = URIRef("http://vocab.ox.ac.uk/%s"%vocabprefix) vocabdir = os.path.join(ag.vocabulariesdir, str(vocabprefix)) vocabstatusfile = os.path.join(vocabdir, "status.rdf") vocab_file_name = os.path.basename(vocabfile) vocabfile_uri = URIRef("http://vocab.ox.ac.uk/%s/%s"%(vocabprefix, vocab_file_name)) #Add vocab in mediator file graph = Graph() mediatorfile = os.path.join(ag.mediatorsdir, '%s.rdf'%userid) graph.parse(mediatorfile) user_uri = [] for uri in graph.subjects(namespaces['foaf']['account'], Literal(userid)): if not uri in user_uri: user_uri.append(uri) user_uri = URIRef(user_uri[0]) graph.add((vocab_uri, namespaces['dcterms']['mediator'], URIRef(user_uri))) rdf_str = None rdf_str = graph.serialize() f = codecs.open(mediatorfile, 'w', 'utf-8') f.write(rdf_str) f.close() #Add vocab in vocab status file graph = Graph() if update and os.path.isfile(vocabstatusfile): graph.parse(vocabstatusfile) for prefix, url in namespaces.iteritems(): graph.bind(prefix, URIRef(url)) graph.add((vocab_uri, namespaces['dcterms']['mediator'], URIRef(user_uri))) graph.add((user_uri, namespaces['foaf']['account'], Literal(userid))) graph.add((vocab_uri, namespaces['dcterms']['hasFormat'], URIRef(vocabfile_uri))) graph.add((vocab_uri, namespaces['vann']['preferredNamespaceUri'], URIRef(baseuri))) graph.add((vocab_uri, namespaces['vann']['preferredNamespacePrefix'], Literal(vocabprefix))) graph.add((vocab_uri, namespaces['skos']['editorialNote'], Literal(vocab_editorial_descriptions[0]))) if refvocab: add_ref_vocab(vocabprefix, refvocab) graph.add((vocab_uri, namespaces['dcterms']['isVersionOf'], URIRef(refvocab))) # get mimetype of file if os.path.isfile(vocabfile): graph.add((vocabfile_uri, namespaces['nfo']['fileUrl'], Literal('file://%s'%vocabfile))) graph.add((vocabfile_uri, namespaces['nfo']['fileName'], Literal(vocab_file_name))) mt = None if check_rdf(vocabfile): mt = 'application/rdf+xml' graph.add((vocabfile_uri, namespaces['dcterms']['conformsTo'], Literal(mt))) graph.add((vocabfile_uri, namespaces['skos']['editorialNote'], Literal(vocab_editorial_descriptions[3]))) elif check_n3(vocabfile): mt = 'text/rdf+nt' root, ext = os.path.splitext(vocabfile) if ext == '.rdf': rdffile = "%s_2.rdf"%root else: rdffile = "%s.rdf"%root converttordf = convert_n3_rdf(vocabfile, rdffile) if converttordf and os.path.isfile(rdffile): rdf_file_name = os.path.basename(rdffile) rdffile_uri = URIRef("http://vocab.ox.ac.uk/%s/%s"%(vocabprefix, rdf_file_name)) graph.add((vocab_uri, namespaces['dcterms']['hasFormat'], URIRef(rdffile_uri))) graph.add((rdffile_uri, namespaces['nfo']['fileUrl'], Literal('file://%s'%rdffile))) graph.add((rdffile_uri, namespaces['nfo']['fileName'], Literal(rdf_file_name))) graph.add((rdffile_uri, namespaces['dcterms']['conformsTo'], Literal('application/rdf+xml'))) graph.add((rdffile_uri, namespaces['skos']['editorialNote'], Literal(vocab_editorial_descriptions[3]))) graph.add((rdffile_uri, namespaces['dcterms']['format'], Literal('application/rdf+xml'))) else: mt1 = mimetypes.guess_type(vocabfile) mt2 = get_file_mimetype(vocabfile) if mt1[0]: mt = mt1[0] else: mt = mt2 if str(mt) == 'application/rdf+xml': graph.add((vocabfile_uri, namespaces['skos']['editorialNote'], Literal(vocab_editorial_descriptions[2]))) else: graph.add((vocab_uri, namespaces['skos']['editorialNote'], Literal(vocab_editorial_descriptions[1]))) if mt: graph.add((vocabfile_uri, namespaces['dcterms']['format'], Literal(mt))) rdf_str = None rdf_str = graph.serialize() f = codecs.open(vocabstatusfile, 'w', 'utf-8') f.write(rdf_str) f.close() return True