Mercurial > code > home > repos > light9
changeset 1367:a287ed391ed5
rdfdb preserve n3 prefixes in the files you rewrite
Ignore-this: 5645efdb50760922a96f4f5163caccb7
author | Drew Perttula <drewp@bigasterisk.com> |
---|---|
date | Mon, 06 Jun 2016 07:46:58 +0000 |
parents | 106226e1eafc |
children | e32eee849766 |
files | light9/rdfdb/graphfile.py light9/rdfdb/graphfile_test.py makefile |
diffstat | 3 files changed, 45 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/light9/rdfdb/graphfile.py Mon Jun 06 07:07:23 2016 +0000 +++ b/light9/rdfdb/graphfile.py Mon Jun 06 07:46:58 2016 +0000 @@ -26,6 +26,8 @@ self.lastWriteTimestamp = 0 # mtime from the last time _we_ wrote + self.namespaces = {} + if not os.path.exists(path): # can't start notify until file exists try: @@ -126,6 +128,7 @@ return new.parse(location=self.path, format='n3') + self.namespaces.update(dict(new.namespaces())) except SyntaxError as e: print e traceback.print_exc() @@ -177,6 +180,8 @@ tmpOut = self.path + ".rdfdb-temp" f = open(tmpOut, 'w') t1 = time.time() + for p, n in self.namespaces.items(): + self.graphToWrite.bind(p, n) self.graphToWrite.serialize(destination=f, format='n3') serializeTime = time.time() - t1 f.close()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/light9/rdfdb/graphfile_test.py Mon Jun 06 07:46:58 2016 +0000 @@ -0,0 +1,39 @@ +import unittest +import mock +import tempfile +from rdflib import URIRef, Graph +from light9.rdfdb.graphfile import GraphFile + +class TestGraphFileOutput(unittest.TestCase): + def testMaintainsN3PrefixesFromInput(self): + tf = tempfile.NamedTemporaryFile(suffix='_test.n3') + tf.write(''' + @prefix : <http://example.com/> . + @prefix n: <http://example.com/n/> . + :foo n:bar :baz . + ''') + tf.flush() + + def getSubgraph(uri): + return Graph() + gf = GraphFile(mock.Mock(), tf.name, URIRef('uri'), mock.Mock(), getSubgraph) + gf.reread() + + newGraph = Graph() + newGraph.add((URIRef('http://example.com/boo'), + URIRef('http://example.com/n/two'), + URIRef('http://example.com/other/ns'))) + gf.dirty(newGraph) + gf.flush() + wroteContent = open(tf.name).read() + self.assertEqual('''@prefix : <http://example.com/> . +@prefix n: <http://example.com/n/> . +@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . +@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . +@prefix xml: <http://www.w3.org/XML/1998/namespace> . +@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . + +:boo n:two <http://example.com/other/ns> . + +''', wroteContent) +
--- a/makefile Mon Jun 06 07:07:23 2016 +0000 +++ b/makefile Mon Jun 06 07:46:58 2016 +0000 @@ -1,4 +1,4 @@ -NOSEARGS="--no-path-adjustment light9.rdfdb.rdflibpatch light9.rdfdb.patch light9.effecteval.test_effect light9.collector" +NOSEARGS="--no-path-adjustment light9.rdfdb.rdflibpatch light9.rdfdb.patch light9.effecteval.test_effect light9.collector light9.rdfdb.graphfile_test" tests: eval env/bin/nosetests -x $(NOSEARGS)