comparison bin/rdfdb @ 1581:30c79f1dc4f8

fix suggestPrefixes to send suggestions to rdfdb Ignore-this: 637142dcc1de97c9046d75d1396a9446
author Drew Perttula <drewp@bigasterisk.com>
date Tue, 30 May 2017 07:36:42 +0000
parents 1ba1d46a70a2
children 2fc0e726a3c3
comparison
equal deleted inserted replaced
1580:1db548d395fb 1581:30c79f1dc4f8
178 """ 178 """
179 find files, notice new files. 179 find files, notice new files.
180 180
181 This object watches directories. Each GraphFile watches its own file. 181 This object watches directories. Each GraphFile watches its own file.
182 """ 182 """
183 def __init__(self, dirUriMap, patch, getSubgraph, addlPrefixes=None): 183 def __init__(self, dirUriMap, patch, getSubgraph, addlPrefixes):
184 self.dirUriMap = dirUriMap # {abspath : uri prefix} 184 self.dirUriMap = dirUriMap # {abspath : uri prefix}
185 self.patch, self.getSubgraph = patch, getSubgraph 185 self.patch, self.getSubgraph = patch, getSubgraph
186 self.addlPrefixes = addlPrefixes 186 self.addlPrefixes = addlPrefixes
187 187
188 self.graphFiles = {} # context uri : GraphFile 188 self.graphFiles = {} # context uri : GraphFile
237 # it. 237 # it.
238 if inFile.endswith("config.n3"): 238 if inFile.endswith("config.n3"):
239 return 239 return
240 240
241 ctx = uriFromFile(self.dirUriMap, inFile) 241 ctx = uriFromFile(self.dirUriMap, inFile)
242 gf = GraphFile(self.notifier, inFile, ctx, 242 gf = self._addGraphFile(ctx, inFile)
243 self.patch, self.getSubgraph,
244 addlPrefixes=self.addlPrefixes)
245 self.graphFiles[ctx] = gf
246 log.info("%s do initial read", inFile) 243 log.info("%s do initial read", inFile)
247 gf.reread() 244 gf.reread()
248 245
249 def aboutToPatch(self, ctx): 246 def aboutToPatch(self, ctx):
250 """ 247 """
261 258
262 if ctx not in self.graphFiles: 259 if ctx not in self.graphFiles:
263 outFile = fileForUri(self.dirUriMap, ctx) 260 outFile = fileForUri(self.dirUriMap, ctx)
264 assert '//' not in outFile, (outFile, self.dirUriMap, ctx) 261 assert '//' not in outFile, (outFile, self.dirUriMap, ctx)
265 log.info("starting new file %r", outFile) 262 log.info("starting new file %r", outFile)
266 self.graphFiles[ctx] = GraphFile(self.notifier, outFile, ctx, 263 self._addGraphFile(ctx, outFile)
267 self.patch, self.getSubgraph, 264
268 addlPrefixes=self.addlPrefixes) 265 def _addGraphFile(self, ctx, path):
269 266 self.addlPrefixes.setdefault(ctx, {})
267 self.addlPrefixes.setdefault(None, {})
268 gf = GraphFile(self.notifier, path, ctx,
269 self.patch, self.getSubgraph,
270 globalPrefixes=self.addlPrefixes[None],
271 ctxPrefixes=self.addlPrefixes[ctx])
272 self.graphFiles[ctx] = gf
273 return gf
274
275
270 def dirtyFiles(self, ctxs): 276 def dirtyFiles(self, ctxs):
271 """mark dirty the files that we watch in these contexts. 277 """mark dirty the files that we watch in these contexts.
272 278
273 the ctx might not be a file that we already read; it might be 279 the ctx might not be a file that we already read; it might be
274 for a new file we have to create, or it might be for a 280 for a new file we have to create, or it might be for a
283 289
284 class Db(object): 290 class Db(object):
285 """ 291 """
286 the master graph, all the connected clients, all the files we're watching 292 the master graph, all the connected clients, all the files we're watching
287 """ 293 """
288 def __init__(self, dirUriMap, addlPrefixes=None): 294 def __init__(self, dirUriMap, addlPrefixes):
289 295
290 self.clients = [] 296 self.clients = []
291 self.graph = ConjunctiveGraph() 297 self.graph = ConjunctiveGraph()
292 298
293 self.watchedFiles = WatchedFiles(dirUriMap, 299 self.watchedFiles = WatchedFiles(dirUriMap,
294 self.patch, self.getSubgraph, addlPrefixes) 300 self.patch, self.getSubgraph,
301 addlPrefixes)
295 302
296 self.summarizeToLog() 303 self.summarizeToLog()
297 304
298 def patch(self, p, dueToFileChange=False): 305 def patch(self, p, dueToFileChange=False):
299 """ 306 """
401 except: 408 except:
402 import traceback 409 import traceback
403 traceback.print_exc() 410 traceback.print_exc()
404 raise 411 raise
405 412
413 class Prefixes(PrettyErrorHandler, cyclone.web.RequestHandler):
414 def post(self):
415 suggestion = json.loads(self.request.body)
416 addlPrefixes = self.settings.db.watchedFiles.addlPrefixes
417 addlPrefixes.setdefault(URIRef(suggestion['ctx']), {}).update(suggestion['prefixes'])
418
406 _wsClientSerial = 0 419 _wsClientSerial = 0
407 class WebsocketClient(cyclone.websocket.WebSocketHandler): 420 class WebsocketClient(cyclone.websocket.WebSocketHandler):
408 421
409 def connectionMade(self, *args, **kwargs): 422 def connectionMade(self, *args, **kwargs):
410 global _wsClientSerial 423 global _wsClientSerial
455 def get(self, path, *args, **kw): 468 def get(self, path, *args, **kw):
456 if path and '.' not in path: 469 if path and '.' not in path:
457 path = path + ".html" 470 path = path + ".html"
458 cyclone.web.StaticFileHandler.get(self, path, *args, **kw) 471 cyclone.web.StaticFileHandler.get(self, path, *args, **kw)
459 472
473
474
460 if __name__ == "__main__": 475 if __name__ == "__main__":
461 logging.basicConfig() 476 logging.basicConfig()
462 log = logging.getLogger() 477 log = logging.getLogger()
463 478
464 parser = optparse.OptionParser() 479 parser = optparse.OptionParser()
468 483
469 log.setLevel(logging.DEBUG if options.verbose else logging.INFO) 484 log.setLevel(logging.DEBUG if options.verbose else logging.INFO)
470 485
471 db = Db(dirUriMap={os.environ['LIGHT9_SHOW'].rstrip('/') + '/': 486 db = Db(dirUriMap={os.environ['LIGHT9_SHOW'].rstrip('/') + '/':
472 showconfig.showUri() + '/'}, 487 showconfig.showUri() + '/'},
473 addlPrefixes={'show': URIRef(showconfig.showUri() + '/')}) 488 addlPrefixes={None: {
489 'show': showconfig.showUri() + '/',
490 '': 'http://light9.bigasterisk.com/',
491 'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
492 'rdfs': 'http://www.w3.org/2000/01/rdf-schema#',
493 'xsd': 'http://www.w3.org/2001/XMLSchema#',
494 'effect': 'http://light9.bigasterisk.com/effect/',
495 'dev': 'http://light9.bigasterisk.com/device/',
496 }})
474 497
475 from twisted.python import log as twlog 498 from twisted.python import log as twlog
476 twlog.startLogging(sys.stdout) 499 twlog.startLogging(sys.stdout)
477 500
478 reactor.listenTCP(networking.rdfdb.port, cyclone.web.Application(handlers=[ 501 reactor.listenTCP(networking.rdfdb.port, cyclone.web.Application(handlers=[
479 (r'/live', Live), 502 (r'/live', Live),
480 (r'/graph', GraphResource), 503 (r'/graph', GraphResource),
481 (r'/patches', Patches), 504 (r'/patches', Patches),
482 (r'/graphClients', GraphClients), 505 (r'/graphClients', GraphClients),
483 (r'/syncedGraph', WebsocketClient), 506 (r'/syncedGraph', WebsocketClient),
507 (r'/prefixes', Prefixes),
484 508
485 (r'/(.*)', NoExts, 509 (r'/(.*)', NoExts,
486 {"path" : "light9/rdfdb/web", 510 {"path" : "light9/rdfdb/web",
487 "default_filename" : "index.html"}), 511 "default_filename" : "index.html"}),
488 512