Mercurial > code > home > repos > light9
annotate bin/rdfdb @ 803:ce4fffe8e413
update SC to read rdf graph
Ignore-this: 7f6788bae887723c9ac12644c1a382da
author | drewp@bigasterisk.com |
---|---|
date | Wed, 18 Jul 2012 09:59:10 +0000 |
parents | caeaa88430b8 |
children | 6d8f0c088a26 |
rev | line source |
---|---|
796 | 1 #!bin/python |
2 """ | |
3 other tools POST themselves to here as subscribers to the graph. They | |
4 are providing a URL we can PUT to with graphs updates. | |
5 | |
6 we immediately PUT them back all the contents of the graph as a bunch | |
7 of adds. | |
8 | |
9 later we PUT them back with updates (add/del lists) when there are | |
10 changes. | |
11 | |
12 If we fail to reach a registered caller, we forget about it for future | |
13 calls. We can PUT empty diffs as a heartbeat to notice disappearing | |
14 callers faster. | |
15 | |
16 A caller can submit add/del changes that should be persisted and | |
17 broadcast. | |
18 | |
19 Global data undo should probably happen within this service. | |
20 | |
21 Maybe some subgraphs are for transient data (e.g. current timecode, | |
22 mouse position in curvecalc) that only some listeners want to hear about. | |
23 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
24 Deletes are graph-specific, so callers may be surprised to delete a |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
25 stmt from one graph but then find that statement is still true. |
796 | 26 |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
27 Alternate plan: would it help to insist that every patch is within |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
28 only one subgraph? I think it's ok for them to span multiple ones. |
796 | 29 |
30 Inserts can be made on any subgraphs, and each subgraph is saved in | |
31 its own file. The file might not be in a format that can express | |
32 graphs, so I'm just going to not store the subgraph URI in any file. | |
33 | |
34 I don't support wildcard deletes, and there are race conditions where a | |
35 s-p could end up with unexpected multiple objects. Every client needs | |
36 to be ready for this. | |
37 | |
38 We watch the files and push their own changes back to the clients. | |
39 | |
40 Persist our client list, to survive restarts. In another rdf file? A | |
41 random json one? memcache? Also hold the recent changes. We're not | |
42 logging everything forever, though, since the output files and a VCS | |
43 shall be used for that | |
44 | |
45 Bnodes: this rdfdb graph might be able to track bnodes correctly, and | |
46 they make for more compact n3 files. I'm not sure if it's going to be | |
47 hard to keep the client bnodes in sync though. File rereads would be | |
48 hard,if ever a bnode was used across graphs, so that probably should | |
49 not be allowed. | |
50 | |
51 Our API: | |
52 | |
53 GET / ui | |
54 GET /graph the whole graph (needed? just for ui browsing?) | |
55 PUT /patches clients submit changes | |
56 GET /patches (recent) patches from clients | |
57 POST /graphClients clientUpdate={uri} to subscribe | |
58 GET /graphClients current clients | |
59 | |
60 format: | |
61 json {"adds" : [[quads]...], | |
62 "deletes": [[quads]], | |
63 "from" : tooluri, | |
64 "created":tttt | |
65 } | |
66 maybe use some http://json-ld.org/ in there. | |
67 | |
68 Our web ui: | |
69 | |
70 registered clients | |
71 | |
72 recent edits, each one says what client it came from. You can reverse | |
799
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
73 them here. We should be able to take patches that are close in time |
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
74 and keep updating the same data (e.g. a stream of changes as the user |
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
75 drags a slider) and collapse them into a single edit for clarity. |
796 | 76 |
803 | 77 proposed rule feature: |
78 rdfdb should be able to watch a pair of (sourceFile, rulesFile) and | |
79 rerun the rules when either one changes. Should the sourceFile be able | |
80 to specify its own rules file? That would be easier configuration. | |
81 | |
796 | 82 """ |
83 from twisted.internet import reactor | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
84 import twisted.internet.error |
796 | 85 import sys, optparse, logging, json, os |
86 import cyclone.web, cyclone.httpclient, cyclone.websocket | |
87 sys.path.append(".") | |
88 from light9 import networking, showconfig | |
89 from rdflib import ConjunctiveGraph, URIRef, Graph | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
90 from light9.rdfdb.graphfile import GraphFile |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
91 from light9.rdfdb.patch import Patch, ALLSTMTS |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
92 from light9.rdfdb.rdflibpatch import patchQuads |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
93 from light9.rdfdb import syncedgraph |
796 | 94 |
95 from twisted.internet.inotify import INotify | |
96 logging.basicConfig(level=logging.DEBUG) | |
97 log = logging.getLogger() | |
98 | |
99 try: | |
100 import sys | |
101 sys.path.append("../homeauto/lib") | |
102 from cycloneerr import PrettyErrorHandler | |
103 except ImportError: | |
104 class PrettyErrorHandler(object): | |
105 pass | |
106 | |
107 class Client(object): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
108 """ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
109 one of our syncedgraph clients |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
110 """ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
111 def __init__(self, updateUri, label, db): |
796 | 112 self.db = db |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
113 self.label = label |
796 | 114 self.updateUri = updateUri |
115 self.sendAll() | |
116 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
117 def __repr__(self): |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
118 return "<%s client at %s>" % (self.label, self.updateUri) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
119 |
796 | 120 def sendAll(self): |
121 """send the client the whole graph contents""" | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
122 log.info("sending all graphs to %s at %s" % |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
123 (self.label, self.updateUri)) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
124 self.sendPatch(Patch( |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
125 addQuads=self.db.graph.quads(ALLSTMTS), |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
126 delQuads=[])) |
796 | 127 |
128 def sendPatch(self, p): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
129 return syncedgraph.sendPatch(self.updateUri, p) |
796 | 130 |
131 class Db(object): | |
132 def __init__(self): | |
133 self.clients = [] | |
134 self.graph = ConjunctiveGraph() | |
135 | |
136 notifier = INotify() | |
137 notifier.startReading() | |
138 | |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
139 for inFile in [#"show/dance2012/config.n3", |
801 | 140 "show/dance2012/patch.n3", |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
141 "show/dance2012/subs/bcools", |
799
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
142 "show/dance2012/subs/bwarm", |
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
143 "show/dance2012/subs/house", |
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
144 "demo.n3", |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
145 ]: |
796 | 146 self.g = GraphFile(notifier, |
147 inFile, | |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
148 URIRef("http://example.com/file/%s" % |
796 | 149 os.path.basename(inFile)), |
150 self.patch, | |
151 self.getSubgraph) | |
152 | |
153 def patch(self, p): | |
154 """ | |
155 apply this patch to the master graph then notify everyone about it | |
156 """ | |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
157 log.info("patching graph -%d +%d" % (len(p.delQuads), len(p.addQuads))) |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
158 |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
159 patchQuads(self.graph, p.delQuads, p.addQuads, perfect=True) |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
160 |
796 | 161 self.summarizeToLog() |
162 for c in self.clients: | |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
163 d = c.sendPatch(p) |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
164 d.addErrback(self.clientErrored, c) |
796 | 165 sendToLiveClients(asJson=p.jsonRepr) |
166 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
167 def clientErrored(self, err, c): |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
168 err.trap(twisted.internet.error.ConnectError) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
169 log.info("connection error- dropping client %r" % c) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
170 self.clients.remove(c) |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
171 self.sendClientsToAllLivePages() |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
172 |
796 | 173 def summarizeToLog(self): |
798
5c158d37f1ce
autoretry websocket. fix rdflib quad patching. only rerun handlers that asked for the affected subj-preds.
drewp@bigasterisk.com
parents:
797
diff
changeset
|
174 log.info("contexts in graph (%s total stmts):" % len(self.graph)) |
796 | 175 for c in self.graph.contexts(): |
176 log.info(" %s: %s statements" % | |
177 (c.identifier, len(self.getSubgraph(c.identifier)))) | |
178 | |
179 def getSubgraph(self, uri): | |
180 # this is returning an empty Graph :( | |
181 #return self.graph.get_context(uri) | |
182 | |
183 g = Graph() | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
184 for s in self.graph.triples(ALLSTMTS, uri): |
796 | 185 g.add(s) |
186 return g | |
187 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
188 def addClient(self, updateUri, label): |
796 | 189 [self.clients.remove(c) |
190 for c in self.clients if c.updateUri == updateUri] | |
191 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
192 log.info("new client %s at %s" % (label, updateUri)) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
193 self.clients.append(Client(updateUri, label, self)) |
796 | 194 self.sendClientsToAllLivePages() |
195 | |
196 def sendClientsToAllLivePages(self): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
197 sendToLiveClients({"clients":[ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
198 dict(updateUri=c.updateUri, label=c.label) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
199 for c in self.clients]}) |
796 | 200 |
201 class Index(PrettyErrorHandler, cyclone.web.RequestHandler): | |
202 def get(self): | |
203 self.set_header("Content-Type", "application/xhtml+xml") | |
204 self.write(open("light9/rdfdb.xhtml").read()) | |
205 | |
206 class GraphResource(PrettyErrorHandler, cyclone.web.RequestHandler): | |
207 def get(self): | |
208 pass | |
209 | |
210 class Patches(PrettyErrorHandler, cyclone.web.RequestHandler): | |
211 def __init__(self, *args, **kw): | |
212 cyclone.web.RequestHandler.__init__(self, *args, **kw) | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
213 p = syncedgraph.makePatchEndpointPutMethod(self.settings.db.patch) |
796 | 214 self.put = lambda: p(self) |
215 | |
216 def get(self): | |
217 pass | |
218 | |
219 | |
220 class GraphClients(PrettyErrorHandler, cyclone.web.RequestHandler): | |
221 def get(self): | |
222 pass | |
223 | |
224 def post(self): | |
225 upd = self.get_argument("clientUpdate") | |
226 try: | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
227 self.settings.db.addClient(upd, self.get_argument("label")) |
796 | 228 except: |
229 import traceback | |
230 traceback.print_exc() | |
231 raise | |
232 | |
233 liveClients = set() | |
234 def sendToLiveClients(d=None, asJson=None): | |
235 j = asJson or json.dumps(d) | |
236 for c in liveClients: | |
237 c.sendMessage(j) | |
238 | |
239 class Live(cyclone.websocket.WebSocketHandler): | |
240 | |
241 def connectionMade(self, *args, **kwargs): | |
242 log.info("ws opened") | |
243 liveClients.add(self) | |
244 self.settings.db.sendClientsToAllLivePages() | |
245 | |
246 def connectionLost(self, reason): | |
247 log.info("ws closed") | |
248 liveClients.remove(self) | |
249 | |
250 def messageReceived(self, message): | |
251 log.info("got message %s" % message) | |
252 self.sendMessage(message) | |
253 | |
254 if __name__ == "__main__": | |
255 logging.basicConfig() | |
256 log = logging.getLogger() | |
257 | |
258 parser = optparse.OptionParser() | |
259 parser.add_option('--show', | |
260 help='show URI, like http://light9.bigasterisk.com/show/dance2008', | |
261 default=showconfig.showUri()) | |
262 parser.add_option("-v", "--verbose", action="store_true", | |
263 help="logging.DEBUG") | |
264 (options, args) = parser.parse_args() | |
265 | |
266 log.setLevel(logging.DEBUG if options.verbose else logging.INFO) | |
267 | |
268 if not options.show: | |
269 raise ValueError("missing --show http://...") | |
270 | |
271 db = Db() | |
272 | |
273 port = 8051 | |
274 reactor.listenTCP(port, cyclone.web.Application(handlers=[ | |
275 (r'/', Index), | |
276 (r'/live', Live), | |
277 (r'/graph', GraphResource), | |
278 (r'/patches', Patches), | |
279 (r'/graphClients', GraphClients), | |
280 | |
281 (r"/(jquery-1\.7\.2\.min\.js)", cyclone.web.StaticFileHandler, | |
282 dict(path='lib')), | |
283 | |
284 ], db=db)) | |
285 log.info("serving on %s" % port) | |
286 reactor.run() |