Mercurial > code > home > repos > light9
annotate bin/rdfdb @ 801:caeaa88430b8
KC sends real levels
Ignore-this: 2c6dae9c9c706c7db02e361d1ff46035
author | drewp@bigasterisk.com |
---|---|
date | Mon, 16 Jul 2012 22:28:08 +0000 |
parents | fcf95ff23cc5 |
children | ce4fffe8e413 |
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 |
77 """ | |
78 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
|
79 import twisted.internet.error |
796 | 80 import sys, optparse, logging, json, os |
81 import cyclone.web, cyclone.httpclient, cyclone.websocket | |
82 sys.path.append(".") | |
83 from light9 import networking, showconfig | |
84 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
|
85 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
|
86 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
|
87 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
|
88 from light9.rdfdb import syncedgraph |
796 | 89 |
90 from twisted.internet.inotify import INotify | |
91 logging.basicConfig(level=logging.DEBUG) | |
92 log = logging.getLogger() | |
93 | |
94 try: | |
95 import sys | |
96 sys.path.append("../homeauto/lib") | |
97 from cycloneerr import PrettyErrorHandler | |
98 except ImportError: | |
99 class PrettyErrorHandler(object): | |
100 pass | |
101 | |
102 class Client(object): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
103 """ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
104 one of our syncedgraph clients |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
105 """ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
106 def __init__(self, updateUri, label, db): |
796 | 107 self.db = db |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
108 self.label = label |
796 | 109 self.updateUri = updateUri |
110 self.sendAll() | |
111 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
112 def __repr__(self): |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
113 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
|
114 |
796 | 115 def sendAll(self): |
116 """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
|
117 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
|
118 (self.label, self.updateUri)) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
119 self.sendPatch(Patch( |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
120 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
|
121 delQuads=[])) |
796 | 122 |
123 def sendPatch(self, p): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
124 return syncedgraph.sendPatch(self.updateUri, p) |
796 | 125 |
126 class Db(object): | |
127 def __init__(self): | |
128 self.clients = [] | |
129 self.graph = ConjunctiveGraph() | |
130 | |
131 notifier = INotify() | |
132 notifier.startReading() | |
133 | |
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
|
134 for inFile in [#"show/dance2012/config.n3", |
801 | 135 "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
|
136 "show/dance2012/subs/bcools", |
799
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
137 "show/dance2012/subs/bwarm", |
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
138 "show/dance2012/subs/house", |
fcf95ff23cc5
PersistentSubmaster split. keyboardcomposer now notices submaster changes
drewp@bigasterisk.com
parents:
798
diff
changeset
|
139 "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
|
140 ]: |
796 | 141 self.g = GraphFile(notifier, |
142 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
|
143 URIRef("http://example.com/file/%s" % |
796 | 144 os.path.basename(inFile)), |
145 self.patch, | |
146 self.getSubgraph) | |
147 | |
148 def patch(self, p): | |
149 """ | |
150 apply this patch to the master graph then notify everyone about it | |
151 """ | |
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
|
152 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
|
153 |
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
|
154 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
|
155 |
796 | 156 self.summarizeToLog() |
157 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
|
158 d = c.sendPatch(p) |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
159 d.addErrback(self.clientErrored, c) |
796 | 160 sendToLiveClients(asJson=p.jsonRepr) |
161 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
162 def clientErrored(self, err, c): |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
163 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
|
164 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
|
165 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
|
166 self.sendClientsToAllLivePages() |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
167 |
796 | 168 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
|
169 log.info("contexts in graph (%s total stmts):" % len(self.graph)) |
796 | 170 for c in self.graph.contexts(): |
171 log.info(" %s: %s statements" % | |
172 (c.identifier, len(self.getSubgraph(c.identifier)))) | |
173 | |
174 def getSubgraph(self, uri): | |
175 # this is returning an empty Graph :( | |
176 #return self.graph.get_context(uri) | |
177 | |
178 g = Graph() | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
179 for s in self.graph.triples(ALLSTMTS, uri): |
796 | 180 g.add(s) |
181 return g | |
182 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
183 def addClient(self, updateUri, label): |
796 | 184 [self.clients.remove(c) |
185 for c in self.clients if c.updateUri == updateUri] | |
186 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
187 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
|
188 self.clients.append(Client(updateUri, label, self)) |
796 | 189 self.sendClientsToAllLivePages() |
190 | |
191 def sendClientsToAllLivePages(self): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
192 sendToLiveClients({"clients":[ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
193 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
|
194 for c in self.clients]}) |
796 | 195 |
196 class Index(PrettyErrorHandler, cyclone.web.RequestHandler): | |
197 def get(self): | |
198 self.set_header("Content-Type", "application/xhtml+xml") | |
199 self.write(open("light9/rdfdb.xhtml").read()) | |
200 | |
201 class GraphResource(PrettyErrorHandler, cyclone.web.RequestHandler): | |
202 def get(self): | |
203 pass | |
204 | |
205 class Patches(PrettyErrorHandler, cyclone.web.RequestHandler): | |
206 def __init__(self, *args, **kw): | |
207 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
|
208 p = syncedgraph.makePatchEndpointPutMethod(self.settings.db.patch) |
796 | 209 self.put = lambda: p(self) |
210 | |
211 def get(self): | |
212 pass | |
213 | |
214 | |
215 class GraphClients(PrettyErrorHandler, cyclone.web.RequestHandler): | |
216 def get(self): | |
217 pass | |
218 | |
219 def post(self): | |
220 upd = self.get_argument("clientUpdate") | |
221 try: | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
222 self.settings.db.addClient(upd, self.get_argument("label")) |
796 | 223 except: |
224 import traceback | |
225 traceback.print_exc() | |
226 raise | |
227 | |
228 liveClients = set() | |
229 def sendToLiveClients(d=None, asJson=None): | |
230 j = asJson or json.dumps(d) | |
231 for c in liveClients: | |
232 c.sendMessage(j) | |
233 | |
234 class Live(cyclone.websocket.WebSocketHandler): | |
235 | |
236 def connectionMade(self, *args, **kwargs): | |
237 log.info("ws opened") | |
238 liveClients.add(self) | |
239 self.settings.db.sendClientsToAllLivePages() | |
240 | |
241 def connectionLost(self, reason): | |
242 log.info("ws closed") | |
243 liveClients.remove(self) | |
244 | |
245 def messageReceived(self, message): | |
246 log.info("got message %s" % message) | |
247 self.sendMessage(message) | |
248 | |
249 if __name__ == "__main__": | |
250 logging.basicConfig() | |
251 log = logging.getLogger() | |
252 | |
253 parser = optparse.OptionParser() | |
254 parser.add_option('--show', | |
255 help='show URI, like http://light9.bigasterisk.com/show/dance2008', | |
256 default=showconfig.showUri()) | |
257 parser.add_option("-v", "--verbose", action="store_true", | |
258 help="logging.DEBUG") | |
259 (options, args) = parser.parse_args() | |
260 | |
261 log.setLevel(logging.DEBUG if options.verbose else logging.INFO) | |
262 | |
263 if not options.show: | |
264 raise ValueError("missing --show http://...") | |
265 | |
266 db = Db() | |
267 | |
268 port = 8051 | |
269 reactor.listenTCP(port, cyclone.web.Application(handlers=[ | |
270 (r'/', Index), | |
271 (r'/live', Live), | |
272 (r'/graph', GraphResource), | |
273 (r'/patches', Patches), | |
274 (r'/graphClients', GraphClients), | |
275 | |
276 (r"/(jquery-1\.7\.2\.min\.js)", cyclone.web.StaticFileHandler, | |
277 dict(path='lib')), | |
278 | |
279 ], db=db)) | |
280 log.info("serving on %s" % port) | |
281 reactor.run() |