changeset 1045:c1face79c0e1

fix rdfdb's filename<->uri mapping system Ignore-this: 2236b295035273370138943857894699
author Drew Perttula <drewp@bigasterisk.com>
date Thu, 29 May 2014 06:46:18 +0000
parents a2081b9adfe4
children 3b3d4e433db3
files bin/rdfdb light9/rdfdb/file_vs_uri.py
diffstat 2 files changed, 45 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/bin/rdfdb	Wed May 28 07:37:36 2014 +0000
+++ b/bin/rdfdb	Thu May 29 06:46:18 2014 +0000
@@ -119,7 +119,7 @@
 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 @@
 
     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 @@
     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 @@
         """
         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 @@
         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 @@
         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 @@
             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 @@
 
     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)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/light9/rdfdb/file_vs_uri.py	Thu May 29 06:46:18 2014 +0000
@@ -0,0 +1,33 @@
+"""
+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