Changeset - c1face79c0e1
[Not reviewed]
default
0 1 1
Drew Perttula - 11 years ago 2014-05-29 06:46:18
drewp@bigasterisk.com
fix rdfdb's filename<->uri mapping system
Ignore-this: 2236b295035273370138943857894699
2 files changed with 45 insertions and 37 deletions:
0 comments (0 inline, 0 general)
bin/rdfdb
Show inline comments
 
@@ -119,7 +119,7 @@ from rdflib import ConjunctiveGraph, URI
 
from light9.rdfdb.graphfile import GraphFile
 
from light9.rdfdb.patch import Patch, ALLSTMTS
 
from light9.rdfdb.rdflibpatch import patchQuads
 

	
 
from light9.rdfdb.file_vs_uri import correctToTopdirPrefix, fileForUri, uriFromFile
 
from light9.rdfdb.patchsender import sendPatch
 
from light9.rdfdb.patchreceiver import makePatchEndpointPutMethod
 

	
 
@@ -165,16 +165,10 @@ class WatchedFiles(object):
 

	
 
    This object watches directories. Each GraphFile watches its own file.
 
    """
 
    def __init__(self, topDirsToWatch, patch, getSubgraph):
 
        self.topDirsToWatch = topDirsToWatch
 
    def __init__(self, dirUriMap, patch, getSubgraph):
 
        self.dirUriMap = dirUriMap # {abspath : uri prefix}
 
        self.patch, self.getSubgraph = patch, getSubgraph
 
        
 
        # files from cwd become uris starting with this. *should* be
 
        # building uris from the show uri in $LIGHT9_SHOW/URI
 
        # instead. Who wants to keep their data in the same dir tree
 
        # as the source code?!
 
        self.topUri = URIRef("http://light9.bigasterisk.com/")
 

	
 
        self.graphFiles = {} # context uri : GraphFile
 
        
 
        self.notifier = INotify()
 
@@ -185,7 +179,7 @@ class WatchedFiles(object):
 
    def findAndLoadFiles(self):
 
        self.initialLoad = True
 
        try:
 
            for topdir in self.topDirsToWatch:
 
            for topdir in self.dirUriMap:
 
                for dirpath, dirnames, filenames in os.walk(topdir):
 
                    for base in filenames:
 
                        self.watchFile(os.path.join(dirpath, base))
 
@@ -209,15 +203,8 @@ class WatchedFiles(object):
 
        """
 
        if not os.path.isfile(inFile):
 
            return
 
        if not any(inFile.startswith(prefix) for prefix in self.topDirsToWatch):
 
            for prefix in self.topDirsToWatch:
 
                prefixAbs = os.path.abspath(prefix)
 
                if inFile.startswith(prefixAbs):
 
                    inFile = prefix + inFile[len(prefixAbs):]
 
                    break
 
            else:
 
                raise ValueError("can't correct %s to start with one of %s" %
 
                                 (inFile, self.topDirsToWatch))
 

	
 
        inFile = correctToTopdirPrefix(self.dirUriMap, inFile)
 
        if os.path.splitext(inFile)[1] not in ['.n3']:
 
            return
 

	
 
@@ -235,7 +222,7 @@ class WatchedFiles(object):
 
        if inFile.endswith("config.n3"):
 
            return
 
            
 
        ctx = self.uriFromFile(inFile)
 
        ctx = uriFromFile(self.dirUriMap, inFile)
 
        gf = GraphFile(self.notifier, inFile, ctx,
 
                       self.patch, self.getSubgraph)
 
        self.graphFiles[ctx] = gf
 
@@ -256,7 +243,7 @@ class WatchedFiles(object):
 
        g = self.getSubgraph(ctx)
 

	
 
        if ctx not in self.graphFiles:
 
            outFile = self.fileForUri(ctx)
 
            outFile = fileForUri(self.dirUriMap, ctx)
 
            log.info("starting new file %r", outFile)
 
            self.graphFiles[ctx] = GraphFile(self.notifier, outFile, ctx,
 
                                             self.patch, self.getSubgraph)
 
@@ -274,30 +261,17 @@ class WatchedFiles(object):
 
            g = self.getSubgraph(ctx)
 
            self.graphFiles[ctx].dirty(g)
 

	
 
    def uriFromFile(self, filename):
 
        assert filename.endswith('.n3'), filename
 
        if not any(filename.startswith(t) for t in self.topDirsToWatch):
 
            raise ValueError("filename %s doesn't start with any of %s" %
 
                             (filename, self.topDirsToWatch))
 
        return URIRef(self.topUri + filename[:-len('.n3')])
 

	
 
    def fileForUri(self, ctx):
 
        assert isinstance(ctx, URIRef), ctx
 
        if not ctx.startswith(self.topUri):
 
            raise ValueError("don't know what filename to use for %s" % ctx)
 
        return ctx[len(self.topUri):] + ".n3"
 

	
 
        
 
class Db(object):
 
    """
 
    the master graph, all the connected clients, all the files we're watching
 
    """
 
    def __init__(self, topDirsToWatch):
 
    def __init__(self, dirUriMap):
 
      
 
        self.clients = []
 
        self.graph = ConjunctiveGraph()
 

	
 
        self.watchedFiles = WatchedFiles(topDirsToWatch,
 
        self.watchedFiles = WatchedFiles(dirUriMap,
 
                                         self.patch, self.getSubgraph)
 
        
 
        self.summarizeToLog()
 
@@ -444,7 +418,8 @@ if __name__ == "__main__":
 

	
 
    log.setLevel(logging.DEBUG if options.verbose else logging.INFO)
 

	
 
    db = Db(topDirsToWatch=[os.environ['LIGHT9_SHOW']])
 
    db = Db(dirUriMap={os.environ['LIGHT9_SHOW'].rstrip('/') + '/':
 
                       URIRef('http://light9.bigasterisk.com/show/dance2014/')})
 

	
 
    from twisted.python import log as twlog
 
    twlog.startLogging(sys.stdout)
light9/rdfdb/file_vs_uri.py
Show inline comments
 
new file 100644
 
"""
 
note that if there are ambiguities in dirUriMap, you'll get
 
undefined results that depend on python's items() order.
 
"""
 
import os
 
from rdflib import URIRef
 

	
 
def uriFromFile(dirUriMap, filename):
 
    assert filename.endswith('.n3'), filename
 
    for d, prefix in dirUriMap.items():
 
        if filename.startswith(d):
 
            return URIRef(prefix + filename[len(d):-len('.n3')])
 
    raise ValueError("filename %s doesn't start with any of %s" %
 
                     (filename, dirUriMap.keys()))
 

	
 
def fileForUri(dirUriMap, ctx):
 
    assert isinstance(ctx, URIRef), ctx
 
    for d, prefix in dirUriMap.items():
 
        if ctx.startswith(prefix):
 
            return d + ctx[len(prefix):] + '.n3'
 
    raise ValueError("don't know what filename to use for %s" % ctx)
 

	
 
def correctToTopdirPrefix(dirUriMap, inFile):
 
    if not any(inFile.startswith(prefix) for prefix in dirUriMap):
 
        for prefix in dirUriMap:
 
            prefixAbs = os.path.abspath(prefix)
 
            if inFile.startswith(prefixAbs):
 
                inFile = prefix + inFile[len(prefixAbs):]
 
                break
 
        else:
 
            raise ValueError("can't correct %s to start with one of %s" %
 
                             (inFile, dirUriMap.keys()))
 
    return inFile
0 comments (0 inline, 0 general)