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
 
@@ -17,24 +17,26 @@ class GraphFile(object):
 
        """
 
        uri is the context for the triples in this file. We assume
 
        sometimes that we're the only ones with triples in this
 
        context.
 
        
 
        this does not include an initial reread() call
 
        """
 
        self.path, self.uri = path, uri
 
        self.patch, self.getSubgraph = patch, getSubgraph
 

	
 
        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:
 
                os.makedirs(os.path.dirname(path))
 
            except OSError:
 
                pass
 
            f = open(path, "w")
 
            f.write("#new\n")
 
            f.close()
 
            iolog.info("%s created", path)
 
            # this was supposed to cut out some extra reads but it
 
            # didn't work:
 
@@ -117,24 +119,25 @@ class GraphFile(object):
 
        new = Graph()
 
        try:
 
            contents = open(self.path).read()
 
            if contents.startswith("#new"):
 
                log.debug("%s ignoring empty contents of my new file", self.path)
 
                # this is a new file we're starting, and we should not
 
                # patch our graph as if it had just been cleared. We
 
                # shouldn't even be here reading this, but
 
                # lastWriteTimestamp didn't work.
 
                return
 

	
 
            new.parse(location=self.path, format='n3')
 
            self.namespaces.update(dict(new.namespaces()))
 
        except SyntaxError as e:
 
            print e
 
            traceback.print_exc()
 
            log.error("%s syntax error", self.path)
 
            # todo: likely bug- if a file has this error upon first
 
            # read, I think we don't retry it right.
 
            return
 
        except IOError as e:
 
            log.error("%s rereading %s: %r", self.path, self.uri, e)
 
            return
 

	
 
        old = inContext(old, self.uri)
 
@@ -168,24 +171,26 @@ class GraphFile(object):
 
        self.graphToWrite = graph
 
        if self.writeCall:
 
            self.writeCall.reset(self.flushDelay)
 
        else:
 
            self.writeCall = reactor.callLater(self.flushDelay, self.flush)
 

	
 
    def flush(self):
 
        self.writeCall = None
 

	
 
        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()
 
        self.lastWriteTimestamp = os.path.getmtime(tmpOut)
 
        os.rename(tmpOut, self.path)
 
        iolog.info("%s rewrote in %.1f ms",
 
                   self.path, serializeTime * 1000)
 
        
 
    def __repr__(self):
 
        return "%s(path=%r, uri=%r, ...)" % (
 
            self.__class__.__name__, self.path, self.uri)
 
        
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)
 

	
 
tests_watch:
 
	eval env/bin/nosetests --with-watcher $(NOSEARGS)
 

	
 

	
 
tests_coverage:
 
	eval env/bin/nosetests --with-coverage --cover-erase --cover-html --cover-html-dir=/tmp/light9-cov/  --cover-package=light9 --cover-branches $(NOSEARGS)
 

	
 
test_js_init:
0 comments (0 inline, 0 general)