diff --git a/light9/rdfdb/graphfile.py b/light9/rdfdb/graphfile.py
--- a/light9/rdfdb/graphfile.py
+++ b/light9/rdfdb/graphfile.py
@@ -26,6 +26,8 @@ class GraphFile(object):
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 @@ class GraphFile(object):
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 @@ class GraphFile(object):
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 --git a/light9/rdfdb/graphfile_test.py b/light9/rdfdb/graphfile_test.py
new file mode 100644
--- /dev/null
+++ b/light9/rdfdb/graphfile_test.py
@@ -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 --git a/makefile b/makefile
--- a/makefile
+++ b/makefile
@@ -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)