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 test_load_from_uri(self): b = Bibtex() assert b.uri == None # make sure it's None g = handler.get("http://bnb.bibliographica.org/entry/GB9361575") b.load_from_graph(g) assert b.uri != None # make sure it's not None print b
def command(self): self.log = logging.getLogger("marc_loader") if len(self.args) != 1: self.log.error("please specify the location of the marc file") sys.exit(1) self.filename = self.args[0] self.log.info("loading records from %s" % (self.filename, )) skip = int(self.options.skip) count = int(self.options.count) self.recno = -1 self.total = 0 self.agent = Agent() self.agent.nick(getuser()) if self.options.aggregate: self.options.load = False self.options.evolve = False for record in marc.Parser(self.filename): self.recno += 1 if self.recno < skip: continue if (self.recno - skip) == count: break try: if self.options.load: marc_rdf = self.load(record) else: from openbiblio import handler marc_rdf = handler.get(self.record_id()) if self.options.evolve: self.evolve(marc_rdf) if self.options.aggregate: self.aggregate(marc_rdf) self.total += 1 except: self.log.error("Exception processing record %s" % self.recno) self.log.error("Record:\n%s" % pformat(record)) self.log.error("Traceback:\n%s" % format_exc()) self.report() sys.exit(1) self.report()
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 _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 aggregate(self, marc): from openbiblio import handler ctx = handler.context(getuser(), "Aggregation of %s" % marc.identifier) def _idx(g): path = g.identifier.lstrip(self.options.base) return URIRef("%saggregate/%s" % (self.options.base, path)) self.log.info("aggregate %s" % marc.identifier) q = """ SELECT DISTINCT ?x WHERE { ?x a obp:Work . ?x opmv:wasGeneratedBy _:proc . _:proc opmv:used %s } """ % (marc.identifier.n3(), ) for work in [handler.get(x) for x, in handler.query(q)]: work_agg = Aggregation(identifier=_idx(work)) work_agg.add((work_agg.identifier, ORDF["lens"], OBPL["work"])) for title in work.distinct_objects(work.identifier, DC["title"]): work_agg.add((work_agg.identifier, RDFS["label"], Literal(u"Work: %s" % (title, )))) work_agg.aggregate(work) contr_list = [] for contr in [ handler.get(x) for x in work.distinct_objects( work.identifier, DC["contributor"]) ]: work_agg.aggregate(contr) contr_agg = Aggregation(identifier=_idx(contr)) contr_agg.add( (contr_agg.identifier, ORDF["lens"], OBPL["contributor"])) for name in contr.distinct_objects(contr.identifier, FOAF["name"]): contr_agg.add((contr_agg.identifier, RDFS["label"], Literal(u"Person: %s" % (name, )))) contr_agg.aggregate(work) contr_agg.aggregate(contr) ctx.add(contr_agg) contr_list.append(contr) for manif in [ handler.get(x) for x in work.distinct_objects( work.identifier, OBP["hasManifestation"]) ]: work_agg.aggregate(manif) manif_agg = Aggregation(identifier=_idx(manif)) manif_agg.add((manif_agg.identifier, ORDF["lens"], OBPL["manifestation"])) for title in work.distinct_objects(work.identifier, DC["title"]): manif_agg.add((manif_agg.identifier, RDFS["label"], Literal(u"Manifestation: %s" % (title, )))) manif_agg.aggregate(work) manif_agg.aggregate(manif) for contr in contr_list: manif_agg.aggregate(contr) for pub in [ handler.get(x) for x in manif.distinct_objects( manif.identifier, DC["publisher"]) ]: manif_agg.aggregate(pub) pub_agg = Aggregation(identifier=_idx(pub)) pub_agg.add( (pub_agg.identifier, ORDF["lens"], OBPL["publisher"])) for name in pub.distinct_objects(pub.identifier, FOAF["name"]): pub_agg.add((pub_agg.identifier, RDFS["label"], Literal(u"Agent: %s" % (name, )))) pub_agg.aggregate(work) pub_agg.aggregate(manif) pub_agg.aggregate(pub) ctx.add(pub), ctx.add(pub_agg) ctx.add(manif), ctx.add(manif_agg) for contr in contr_list: ctx.add(contr) ctx.add(work), ctx.add(work_agg) ctx.commit()
def get_by_uri(cls, uri): uri = u(uri) graph = handler.get(uri) obj = cls(uri, graph=graph) return obj