def test_create(self): title = 'mytitle' ourlist = { 'created': datetime.datetime.now().isoformat(), 'title': title, 'works': [] } ourlist = json.dumps(ourlist) ournewuser = '******' oururl = url(controller='collection', action='create') response = self.app.post(oururl, params=ourlist, status=[401, 302]) if response.status_int == 302: response = response.follow() assert 'Login' in response, response response = self.app.post(oururl, ourlist, extra_environ=dict(REMOTE_USER=ournewuser)) data = response.json collection_uri = data['uri'] self.graphs_to_destroy.append(collection_uri) assert data['uri'] graph = handler.get(collection_uri) assert graph, collection_uri pprint.pprint([(s, p, o) for s, p, o in graph]) match = list(graph.triples((URIRef(collection_uri), None, ournewuser))) assert match dctitle = URIRef('http://purl.org/dc/terms/title') title_triple = (URIRef(collection_uri), dctitle, title) assert title_triple in graph
def test_update(self): ouruser = '******' collection_uri = collection.create_collection(ouruser) collid = collection_uri.split('/')[-1] oururl = url(controller='collection', action='update', collection=collid) response = self.app.post(oururl, params={}, status=[401, 302]) title = 'mynewtitle' values = json.dumps({'title': title}) response = self.app.post(oururl, values, extra_environ=dict(REMOTE_USER=ouruser)) data = response.json assert data['status'] == 'ok', data graph = handler.get(collection_uri) assert graph, collection_uri pprint.pprint([(s, p, o) for s, p, o in graph]) match = list(graph.triples((URIRef(collection_uri), None, ouruser))) assert match dctitle = URIRef('http://purl.org/dc/terms/title') title_triple = (URIRef(collection_uri), dctitle, title) assert title_triple in graph
def rdf_data(): s = LicensesService2() g = Graph(identifier=CC[""]) g.parse("http://creativecommons.org/schema.rdf") yield g fp = pkg_resources.resource_stream("licenses", os.path.join("n3", "license.n3")) g = Graph(identifier=LICENSES["lens"]) g.parse(fp, format="n3") fp.close() yield g for ld in s.get_licenses(): ident = LICENSES[ld["id"]] g = Graph(identifier=ident) l = License(ident, graph=g) l.label = Literal(ld["title"]) l.prefLabel = Literal(ld["title"]) l.notation = Literal(ld["id"]) l.lens = LICENSES.lens if ld.get("url"): url = URIRef(ld["url"]) sa = Graph() try: sa.parse(url) except: pass try: sa.parse(url, format="rdfa") except: pass sa.remove((url, XHV.icon, None)) sa.remove((url, XHV.alternate, None)) sa.remove((url, XHV.stylesheet, None)) for ll in sa.distinct_objects(url, XHV.license): l.license = ll sa.remove((url, XHV.license, None)) if sa.bnc((url, None, None)): [g.add((ident, p, o)) for s, p, o in sa.bnc((url, None, None))] l.sameAs = url else: l.seeAlso = URIRef(ld["url"]) yield g
def work(self, marc): proc = self.process() proc.use(marc.identifier) work = Graph(identifier=URIRef(marc.identifier + "/work")) work.add((work.identifier, RDF["type"], OBP["Work"])) work += self.rewrite(marc, work, DC["title"]) work += self.rewrite(marc, work, DC["description"]) work += self.rewrite(marc, work, BIBO["lccn"]) work += self.rewrite(marc, work, OBP["scn"]) contributors = self.contributors(marc) for c in contributors: work.add((work.identifier, DC["contributor"], c.identifier)) subjects = self.subjects(marc) for s in subjects: work.add((work.identifier, DC["subject"], s.identifier)) if not s.exists((s.identifier, RDF["type"], FOAF["Person"])): work += s manif = self.manifestation(marc) work.add((work.identifier, OBP["hasManifestation"], manif.identifier)) proc.result(work) self.context.add(work)
def coerce_uri(x): if not isinstance(x, URIRef): if isinstance(x, Node): raise TypeError(x, type(x), "must be URIRef or a type other than Node") x = URIRef(x) return x
def manifestation(self, marc): proc = self.process() proc.use(marc.identifier) manif = Graph(identifier=URIRef(marc.identifier + "/manifestation")) manif.add((manif.identifier, RDF["type"], OBP["Manifestation"])) publisher = self.publisher(marc) manif.add((manif.identifier, DC["publisher"], publisher.identifier)) for _s, _p, o in marc.triples( (marc.identifier, DC["publisher"], None)): for s, p, loc in marc.triples((o, DC["spatial"], None)): manif.add((manif.identifier, DC["spatial"], loc)) manif += self.rewrite(marc, manif, BIBO["isbn"]) manif += self.rewrite(marc, manif, BIBO["isbn10"]) manif += self.rewrite(marc, manif, BIBO["isbn13"]) manif += self.rewrite(marc, manif, DC["date"]) manif += self.rewrite(marc, manif, DC["extent"]) manif += self.rewrite(marc, manif, OBP["dimensions"]) manif += self.rewrite(marc, manif, OBP["edition"]) manif += self.rewrite(marc, manif, OBP["lccall"]) manif += self.rewrite(marc, manif, OBP["nlmcall"]) manif += self.rewrite(marc, manif, OBP["nbn"]) manif += self.rewrite(marc, manif, OBP["physicalDetail"]) manif += self.rewrite(marc, manif, RDFS["seeAlso"]) proc.result(manif) self.context.add(manif) return manif
def clean_identifiers(self, record): identifiers = record.get("dc:identifier", []) links = [] rights = [] idents = [] for i in range(len(identifiers)): ident = identifiers[0] if ident.startswith("http://") and not ident.endswith("license"): links.append(URIRef(ident)) elif ident.endswith("license"): rights.append(URIRef(ident)) else: idents.append(ident) identifiers = identifiers[1:] record["dc:identifier"] = idents record["dc:rights"] = rights record["foaf:homepage"] = links
def rdf(self, *av, **kw): g = Graph(*av, **kw) g.add((g.identifier, RDF["type"], OBP["MarcRecord"])) def merge(d, s): for k, v in d.items(): ns, term = k.split(":") p = namespaces[ns][term] for o in v: if isinstance(o, dict): b = BNode() g.add((s, p, b)) merge(o, b) else: g.add((s, p, o)) ident = g.identifier merge(self, ident) for s, p, o in g.triples((ident, BIBO["isbn"], None)): g.add((ident, RDFS["seeAlso"], URIRef("urn:isbn:%s" % o))) g.add((ident, RDFS["seeAlso"], URIRef("http://purl.org/NET/book/isbn/%s#book" % o))) g.add( (ident, RDFS["seeAlso"], URIRef("http://www4.wiwiss.fu-berlin.de/bookmashup/books/%s" % o))) if len(o) == 10: g.add((ident, BIBO["isbn10"], o)) elif len(o) == 13: g.add((ident, BIBO["isbn13"], o)) for s, p, o in g.triples((ident, BIBO["issn"], None)): g.add((ident, RDFS["seeAlso"], URIRef("urn:issn:%s" % o))) for s, p, o in g.triples((ident, BIBO["lccn"], None)): g.add( (ident, RDFS["seeAlso"], URIRef(u"http://lccn.loc.gov/" + o))) self.nbn(g) self.scn(g) self.lccall(g) self.lccopy(g) self.isPartOf(g) return g
def _get_graph(self): uri = self._uri() content_type, format = self._accept(uri) if uri.endswith("bibtex"): content_type = "text/x-bibtex" format = "bibtex" uri_str, _ = uri.rsplit(".", 1) uri = URIRef(uri_str) graph = handler.get(uri) if len(graph) == 0: graph.rollback() cursor = handler.rdflib.store.cursor() cursor.execute("SET result_timeout = 10000") q = construct_graph % {"agent": uri.n3()} graph = handler.rdflib.store.sparql_query(q, cursor=cursor) graph = Graph(graph.store, identifier=graph.identifier) # ordf extensions cursor.close() if len(graph) == 0: abort(404, "No such graph: %s" % uri) if format == "html": c.graph = graph c.model = model.Entry.get_by_uri(uri) response.content_type = str(content_type) # should really iterate through the potential views if URIRef("http://purl.org/ontology/bibo/Book") in list( c.model.type): data = render("view_bibo_book.html") else: data = self._render_graph() elif format == "bibtex": b = Bibtex() b.load_from_graph(graph) data = b.to_bibtex() response.content_type = str(content_type) response.headers['Content-Location'] = "%s.bibtex" % b.uniquekey response.headers['Location'] = "%s.bibtex" % b.uniquekey else: data = graph.serialize(format=format) response.content_type = str(content_type) graph.rollback() # log.warn("XXX cursor: %s" % handler.rdflib.store._cursor) return data
def ddc(self): ddc = [] for s in self["marc:ddc"]: try: c, rest = s.split(".", 1) except: c = s ddc.append({ "dcam:member": [DC["DDC"]], "rdf:value": [s], "rdfs:seeAlso": [URIRef("http://dewey.info/class/%s/" % c)] }) return ddc
def publisher(self, marc): proc = self.process() proc.use(marc.identifier) publisher = Graph(identifier=URIRef(marc.identifier + "/publisher")) for s, p, o in marc.triples((marc.identifier, DC["publisher"], None)): publisher += marc.bnc((o, None, None)).replace( (o, None, None), (publisher.identifier, None, None)) publisher.remove((publisher.identifier, DC["spatial"], None)) proc.result(publisher) self.context.add(publisher) return publisher
def _get_graph(self): uri = self._uri() content_type, format = self._accept(uri) if uri.endswith("bibtex"): content_type = "text/x-bibtex" format = "bibtex" uri_str, _ = uri.rsplit(".", 1) uri = URIRef(uri_str) graph = handler.get(uri) if len(graph) == 0: graph.rollback() cursor = handler.rdflib.store.cursor() cursor.execute("SET result_timeout = 10000") q = construct_graph % {"agent": uri.n3()} graph = handler.rdflib.store.sparql_query(q, cursor=cursor) graph = Graph(graph.store, identifier=graph.identifier) # ordf extensions cursor.close() if len(graph) == 0: abort(404, "No such graph: %s" % uri) if format == "html": c.graph = graph data = self._render_graph() elif format == "bibtex": b = Bibtex() b.load_from_graph(graph) data = b.to_bibtex() response.content_type = str(content_type) response.headers['Content-Location'] = "%s.bibtex" % b.uniquekey response.headers['Location'] = "%s.bibtex" % b.uniquekey else: data = graph.serialize(format=format) response.content_type = str(content_type) graph.rollback() # log.warn("XXX cursor: %s" % handler.rdflib.store._cursor) return data
def nbn(self, g): """ Add urn:nbn: identifiers for national bibliographic numbers per: http://www.ietf.org/rfc/rfc3188.txt TODO: transform nbn prefixes to ISO country codes """ nbn = self["marc:nbn"] nbnc = self["marc:nbnc"] if nbn and nbnc: for n, c in zip(nbn, nbnc): g.add((g.identifier, RDFS["seeAlso"], URIRef("urn:nbn:%s-%s" % (c, n)))) b = BNode() g.add((g.identifier, OBP["nbn"], b)) g.add((b, DCAM["member"], NBN[c])) g.add((b, RDF["value"], n))
def contributors(self, marc): result = [] i = 0 for s, p, o in marc.triples( (marc.identifier, DC["contributor"], None)): proc = self.process() proc.use(marc.identifier) identifier = URIRef(marc.identifier + "/contributor/%d" % i) contributor = Graph(identifier=identifier) contributor += marc.bnc((o, None, None)).replace( (o, None, None), (identifier, None, None)) if not contributor.exists((identifier, RDF["type"], None)): contributor.add((identifier, RDF["type"], FOAF["Person"])) proc.result(contributor) self.context.add(contributor) result.append(contributor) i += 1 return result
def setUp(cls): from openbiblio import handler if cls.done: return ctx = handler.context(getuser(), "Initial Data") for graph in cls.data(): ## delete any stale history ctx.add(graph) ctx.commit() ctx = handler.context(getuser(), "Bibtex Graph data") ident = URIRef("http://bnb.bibliographica.org/entry/GB9361575") data = Graph(identifier=ident) data.parse(os.path.join(cls.testdata, "GB9361575.rdf")) ctx.add(data) ctx.commit() cls.done = True
def create_collection(user, object_dict={}): defaults = { 'uri': 'http://bibliographica.org/collection/' + str(uuid.uuid4()), 'title': 'Untitled', 'user': user, 'works': [] } values = dict(defaults) values.update(object_dict) uri = values['uri'] ident = URIRef(uri) data = Graph(identifier=ident) ourdata = collection_n3 % values for work in values['works']: membership = '<%s> rdfs:member <%s> .\n' % (work, ident) ourdata += membership data.parse(data=ourdata, format='n3') ctx = handler.context(user, "Creating collection: %s" % uri) ctx.add(data) ctx.commit() return uri
def subjects(self, marc): result = [] i = 0 for s, p, o in marc.triples((marc.identifier, DC["subject"], None)): if isinstance(o, Literal): subject = Graph() subject.add((subject.identifier, RDF["value"], o)) result.append(subject) elif marc.exists((o, RDF["type"], FOAF["Person"])): proc = self.process() proc.use(marc.identifier) identifier = URIRef(marc.identifier + "/subject/%d" % i) subject = Graph(identifier=identifier) subject += marc.bnc((o, None, None)).replace( (o, None, None), (identifier, None, None)) proc.result(subject) self.context.add(subject) i += 1 else: subject = Graph(identifier=o) subject += marc.bnc((o, None, None)) result.append(subject) return result
class TestAccount: openid = 'http://myopen.id' ident = URIRef(openid) name = 'Mr Jones and me' email = '*****@*****.**' @classmethod def setup_class(self): account = model.Account.create(self.openid, self.name, self.email) self.account_id = account.identifier current_user = '******' account.save(current_user, 'xyz') @classmethod def teardown_class(self): delete_all() def test_01_get_null(self): out = model.Account.get_by_openid(self.openid + 'madeup') assert out == None, out def test_02_get(self): acc = model.Account.get_by_uri(self.account_id) acc = model.Account.get_by_openid(self.openid) assert acc.identifier == self.account_id, acc.identifier out = [x for x in acc.owners][0] for (s, p, o) in out.graph.triples((None, None, None)): print s, p, o # print [(s,p,o) for (s,p,o) in out.graph.triples((None,None,None))] assert out.name[0] == self.name, out.name assert str(out.openid[0]) == self.openid, out.openid[0] def test_03_find(self): out = model.Account.find() assert len(out) == 1, out assert self.account_id == out[0].identifier, out
def data(cls): ident = URIRef("http://bibliographica.org/test") data = Graph(identifier=ident) data.parse(os.path.join(cls.testdata, "fixtures.rdf")) yield data
import wdmmgrdf.model as model def load(): '''Downloads the COFOG list, and loads it into the database with key names 'cofog1', 'cofog2' and 'cofog3'. ''' # Get the COFOG data package. pkgspec = 'file://%s' % os.path.join(config['getdata_cache'], 'cofog') pkg = datapkg.load_package(pkgspec) fileobj = pkg.stream('cofog.n3') load_file(fileobj) COFOG_IDENTIFIER = URIRef('http://cofog.eu/cofog/1999') def load_file(fileobj): '''Loads the specified COFOG-like file into the database with key names 'cofog1', 'cofog2' and 'cofog3'. ''' # TODO: replace with simple import of the cofog rdf data which already has # relevant structure from wdmmgrdf.model import handler ctx = handler.context(u'importer', u'loading cofog') g = Graph(identifier=COFOG_IDENTIFIER) g.parse(fileobj, format='n3') log.info('add %s' % g.identifier) ctx.add(g) log.info('commit changes')
def _idx(g): path = g.identifier.lstrip(self.options.base) return URIRef("%saggregate/%s" % (self.options.base, path))
def record_id(self): return URIRef(self.options.base + self.uuid() + "/%s" % self.recno)
def source(self): if self.options.source: return URIRef(self.options.source) else: return URIRef("file://" + os.path.abspath(self.filename))
def uri(self, s): return URIRef(s)