Changeset - a287ed391ed5
[Not reviewed]
default
0 2 1
Drew Perttula - 9 years ago 2016-06-06 07:46:58
drewp@bigasterisk.com
rdfdb preserve n3 prefixes in the files you rewrite
Ignore-this: 5645efdb50760922a96f4f5163caccb7
3 files changed with 45 insertions and 1 deletions:
0 comments (0 inline, 0 general)
light9/rdfdb/graphfile.py
Show inline comments
 
@@ -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()
light9/rdfdb/graphfile_test.py
Show inline comments
 
new file 100644
 
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)
 

	
makefile
Show inline comments
 
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)
0 comments (0 inline, 0 general)