# HG changeset patch # User Drew Perttula # Date 1465199218 0 # Node ID a287ed391ed5e5efeefbaf3bc8de1caad2f9c6c0 # Parent 106226e1eafc22bc5f78fa9dc3d3bfd81dd4ee5a rdfdb preserve n3 prefixes in the files you rewrite Ignore-this: 5645efdb50760922a96f4f5163caccb7 diff -r 106226e1eafc -r a287ed391ed5 light9/rdfdb/graphfile.py --- 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() diff -r 106226e1eafc -r a287ed391ed5 light9/rdfdb/graphfile_test.py --- /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 : . + @prefix 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 : . +@prefix n: . +@prefix rdf: . +@prefix rdfs: . +@prefix xml: . +@prefix xsd: . + +:boo n:two . + +''', wroteContent) + diff -r 106226e1eafc -r a287ed391ed5 makefile --- 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)