Mercurial > code > home > repos > light9
annotate bin/rdfdb @ 815:d7f1f868eb6c
toplevel window pos is saved in the graph. Patch conflicts no longer break as hard, but they don't exactly reset themselves right yet eiher
Ignore-this: 56f96fd0b1a8602abc4e41851685794c
author | drewp@bigasterisk.com |
---|---|
date | Sat, 20 Oct 2012 21:52:10 +0000 |
parents | 1ae8e6b287e3 |
children | 295b867fd810 |
rev | line source |
---|---|
796 | 1 #!bin/python |
2 """ | |
3 other tools POST themselves to here as subscribers to the graph. They | |
811 | 4 are providing a URL we can PUT to with graph updates. |
796 | 5 |
6 we immediately PUT them back all the contents of the graph as a bunch | |
7 of adds. | |
8 | |
811 | 9 later we PUT them back with patches (del/add lists) when there are |
796 | 10 changes. |
11 | |
12 If we fail to reach a registered caller, we forget about it for future | |
811 | 13 calls. We could PUT empty diffs as a heartbeat to notice disappearing |
796 | 14 callers faster. |
15 | |
811 | 16 A caller can submit a patch which we'll persist and broadcast to every |
17 other client. | |
796 | 18 |
815
d7f1f868eb6c
toplevel window pos is saved in the graph. Patch conflicts no longer break as hard, but they don't exactly reset themselves right yet eiher
drewp@bigasterisk.com
parents:
814
diff
changeset
|
19 Global data undo should probably happen within this service. Some |
d7f1f868eb6c
toplevel window pos is saved in the graph. Patch conflicts no longer break as hard, but they don't exactly reset themselves right yet eiher
drewp@bigasterisk.com
parents:
814
diff
changeset
|
20 operations should not support undo, such as updating the default |
d7f1f868eb6c
toplevel window pos is saved in the graph. Patch conflicts no longer break as hard, but they don't exactly reset themselves right yet eiher
drewp@bigasterisk.com
parents:
814
diff
changeset
|
21 position of a window. How will we separate those? A blacklist of |
d7f1f868eb6c
toplevel window pos is saved in the graph. Patch conflicts no longer break as hard, but they don't exactly reset themselves right yet eiher
drewp@bigasterisk.com
parents:
814
diff
changeset
|
22 subj+pred pairs that don't save undo? Or just save the updates like |
d7f1f868eb6c
toplevel window pos is saved in the graph. Patch conflicts no longer break as hard, but they don't exactly reset themselves right yet eiher
drewp@bigasterisk.com
parents:
814
diff
changeset
|
23 everything else, but when you press undo, there's a way to tell which |
d7f1f868eb6c
toplevel window pos is saved in the graph. Patch conflicts no longer break as hard, but they don't exactly reset themselves right yet eiher
drewp@bigasterisk.com
parents:
814
diff
changeset
|
24 updates *should* be part of your app's undo system? |
796 | 25 |
26 Maybe some subgraphs are for transient data (e.g. current timecode, | |
27 mouse position in curvecalc) that only some listeners want to hear about. | |
28 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
29 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
|
30 stmt from one graph but then find that statement is still true. |
796 | 31 |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
32 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
|
33 only one subgraph? I think it's ok for them to span multiple ones. |
796 | 34 |
35 Inserts can be made on any subgraphs, and each subgraph is saved in | |
36 its own file. The file might not be in a format that can express | |
37 graphs, so I'm just going to not store the subgraph URI in any file. | |
38 | |
39 I don't support wildcard deletes, and there are race conditions where a | |
40 s-p could end up with unexpected multiple objects. Every client needs | |
41 to be ready for this. | |
42 | |
43 We watch the files and push their own changes back to the clients. | |
44 | |
45 Persist our client list, to survive restarts. In another rdf file? A | |
46 random json one? memcache? Also hold the recent changes. We're not | |
47 logging everything forever, though, since the output files and a VCS | |
48 shall be used for that | |
49 | |
50 Bnodes: this rdfdb graph might be able to track bnodes correctly, and | |
51 they make for more compact n3 files. I'm not sure if it's going to be | |
52 hard to keep the client bnodes in sync though. File rereads would be | |
811 | 53 hard, if ever a bnode was used across graphs, so that probably should |
796 | 54 not be allowed. |
55 | |
56 Our API: | |
57 | |
58 GET / ui | |
811 | 59 GET /graph the whole graph, or a query from it (needed? just for ui browsing?) |
796 | 60 PUT /patches clients submit changes |
61 GET /patches (recent) patches from clients | |
62 POST /graphClients clientUpdate={uri} to subscribe | |
63 GET /graphClients current clients | |
64 | |
65 format: | |
66 json {"adds" : [[quads]...], | |
67 "deletes": [[quads]], | |
811 | 68 "senderUpdateUri" : tooluri, |
69 "created":tttt // maybe to help resolve some conflicts | |
796 | 70 } |
71 maybe use some http://json-ld.org/ in there. | |
72 | |
806 | 73 proposed rule feature: |
74 rdfdb should be able to watch a pair of (sourceFile, rulesFile) and | |
75 rerun the rules when either one changes. Should the sourceFile be able | |
76 to specify its own rules file? That would be easier | |
77 configuration. How do edits work? Not allowed? Patch the source only? | |
78 Also see the source graph loaded into a different ctx, and you can | |
79 edit that one and see the results in the output context? | |
80 | |
796 | 81 Our web ui: |
82 | |
811 | 83 sections |
84 | |
85 registered clients | |
796 | 86 |
811 | 87 recent patches, each one says what client it came from. You can reverse |
88 them here. We should be able to take patches that are close in time | |
89 and keep updating the same data (e.g. a stream of changes as the user | |
90 drags a slider) and collapse them into a single edit for clarity. | |
91 | |
92 Ways to display patches, using labels and creator/subj icons | |
93 where possible: | |
796 | 94 |
811 | 95 <creator> set <subj>'s <p> to <o> |
96 <creator> changed <subj>'s <pred> from <o1> to <o2> | |
97 <creator> added <o> to <s> <p> | |
98 | |
99 raw messages for debugging this client | |
806 | 100 |
811 | 101 ctx urls take you to-> |
102 files, who's dirty, have we seen external changes, notice big | |
103 files that are taking a long time to save | |
806 | 104 |
811 | 105 graph contents. plain rdf browser like an outliner or |
106 something. clicking any resource from the other displays takes you | |
107 to this, focused on that resource | |
803 | 108 |
796 | 109 """ |
110 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
|
111 import twisted.internet.error |
796 | 112 import sys, optparse, logging, json, os |
113 import cyclone.web, cyclone.httpclient, cyclone.websocket | |
114 sys.path.append(".") | |
808
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
115 from light9 import networking, showconfig, prof |
796 | 116 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
|
117 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
|
118 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
|
119 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
|
120 from light9.rdfdb import syncedgraph |
796 | 121 |
122 from twisted.internet.inotify import INotify | |
123 logging.basicConfig(level=logging.DEBUG) | |
124 log = logging.getLogger() | |
125 | |
126 try: | |
127 import sys | |
128 sys.path.append("../homeauto/lib") | |
129 from cycloneerr import PrettyErrorHandler | |
130 except ImportError: | |
131 class PrettyErrorHandler(object): | |
132 pass | |
133 | |
134 class Client(object): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
135 """ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
136 one of our syncedgraph clients |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
137 """ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
138 def __init__(self, updateUri, label, db): |
796 | 139 self.db = db |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
140 self.label = label |
796 | 141 self.updateUri = updateUri |
142 self.sendAll() | |
143 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
144 def __repr__(self): |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
145 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
|
146 |
796 | 147 def sendAll(self): |
148 """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
|
149 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
|
150 (self.label, self.updateUri)) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
151 self.sendPatch(Patch( |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
152 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
|
153 delQuads=[])) |
796 | 154 |
155 def sendPatch(self, p): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
156 return syncedgraph.sendPatch(self.updateUri, p) |
796 | 157 |
158 class Db(object): | |
811 | 159 """ |
160 the master graph, all the connected clients, all the files we're watching | |
161 """ | |
796 | 162 def __init__(self): |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
163 # files from cwd become uris starting with this. *should* be |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
164 # building uris from the show uri in $LIGHT9_SHOW/URI |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
165 # instead. Who wants to keep their data in the same dir tree |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
166 # as the source code?! |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
167 self.topUri = URIRef("http://light9.bigasterisk.com/") |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
168 |
796 | 169 self.clients = [] |
170 self.graph = ConjunctiveGraph() | |
171 | |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
172 self.notifier = INotify() |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
173 self.notifier.startReading() |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
174 self.graphFiles = {} # context uri : GraphFile |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
175 |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
176 self.findAndLoadFiles() |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
177 |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
178 def findAndLoadFiles(self): |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
179 self.initialLoad = True |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
180 try: |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
181 dirs = [ |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
182 "show/dance2012/sessions", |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
183 "show/dance2012/subs", |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
184 "show/dance2012/subterms", |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
185 ] |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
186 |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
187 for topdir in dirs: |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
188 for dirpath, dirnames, filenames in os.walk(topdir): |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
189 for base in filenames: |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
190 self.watchFile(os.path.join(dirpath, base)) |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
191 # todo: also notice new files in this dir |
796 | 192 |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
193 self.watchFile("show/dance2012/config.n3") |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
194 self.watchFile("show/dance2012/patch.n3") |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
195 finally: |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
196 self.initialLoad = False |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
197 |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
198 self.summarizeToLog() |
796 | 199 |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
200 def uriFromFile(self, filename): |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
201 if filename.endswith('.n3'): |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
202 # some legacy files don't end with n3. when we write them |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
203 # back this might not go so well |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
204 filename = filename[:-len('.n3')] |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
205 return URIRef(self.topUri + filename) |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
206 |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
207 def fileForUri(self, ctx): |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
208 if not ctx.startswith(self.topUri): |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
209 raise ValueError("don't know what filename to use for %s" % ctx) |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
210 return ctx[len(self.topUri):] + ".n3" |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
211 |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
212 def watchFile(self, inFile): |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
213 ctx = self.uriFromFile(inFile) |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
214 gf = GraphFile(self.notifier, inFile, ctx, self.patch, self.getSubgraph) |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
215 self.graphFiles[ctx] = gf |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
216 gf.reread() |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
217 |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
218 def patch(self, p, dueToFileChange=False): |
796 | 219 """ |
220 apply this patch to the master graph then notify everyone about it | |
811 | 221 |
222 dueToFileChange if this is a patch describing an edit we read | |
223 *from* the file (such that we shouldn't write it back to the file) | |
224 | |
225 if p has a senderUpdateUri attribute, we won't send this patch | |
226 back to the sender with that updateUri | |
796 | 227 """ |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
228 ctx = p.getContext() |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
229 log.info("patching graph %s -%d +%d" % ( |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
230 ctx, 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
|
231 |
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
|
232 patchQuads(self.graph, p.delQuads, p.addQuads, perfect=True) |
808
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
233 senderUpdateUri = getattr(p, 'senderUpdateUri', None) |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
234 #if not self.initialLoad: |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
235 # self.summarizeToLog() |
796 | 236 for c in self.clients: |
808
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
237 if c.updateUri == senderUpdateUri: |
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
238 # this client has self-applied the patch already |
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
239 continue |
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
|
240 d = c.sendPatch(p) |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
241 d.addErrback(self.clientErrored, c) |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
242 if not dueToFileChange: |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
243 self.dirtyFiles([ctx]) |
796 | 244 sendToLiveClients(asJson=p.jsonRepr) |
245 | |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
246 def dirtyFiles(self, ctxs): |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
247 """mark dirty the files that we watch in these contexts. |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
248 |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
249 the ctx might not be a file that we already read; it might be |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
250 for a new file we have to create, or it might be for a |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
251 transient context that we're not going to save |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
252 |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
253 if it's a ctx with no file, error |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
254 """ |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
255 for ctx in ctxs: |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
256 g = self.getSubgraph(ctx) |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
257 |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
258 if ctx not in self.graphFiles: |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
259 outFile = self.fileForUri(ctx) |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
260 self.graphFiles[ctx] = GraphFile(self.notifier, outFile, ctx, |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
261 self.patch, self.getSubgraph) |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
262 |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
263 self.graphFiles[ctx].dirty(g) |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
264 |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
265 def clientErrored(self, err, c): |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
266 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
|
267 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
|
268 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
|
269 self.sendClientsToAllLivePages() |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
270 |
796 | 271 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
|
272 log.info("contexts in graph (%s total stmts):" % len(self.graph)) |
796 | 273 for c in self.graph.contexts(): |
274 log.info(" %s: %s statements" % | |
275 (c.identifier, len(self.getSubgraph(c.identifier)))) | |
276 | |
277 def getSubgraph(self, uri): | |
811 | 278 """ |
279 this is meant to return a live view of the given subgraph, but | |
280 if i'm still working around an rdflib bug, it might return a | |
281 copy | |
282 | |
283 and it's returning triples, but I think quads would be better | |
284 """ | |
796 | 285 # this is returning an empty Graph :( |
286 #return self.graph.get_context(uri) | |
287 | |
288 g = Graph() | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
289 for s in self.graph.triples(ALLSTMTS, uri): |
796 | 290 g.add(s) |
291 return g | |
292 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
293 def addClient(self, updateUri, label): |
796 | 294 [self.clients.remove(c) |
295 for c in self.clients if c.updateUri == updateUri] | |
296 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
297 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
|
298 self.clients.append(Client(updateUri, label, self)) |
796 | 299 self.sendClientsToAllLivePages() |
300 | |
301 def sendClientsToAllLivePages(self): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
302 sendToLiveClients({"clients":[ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
303 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
|
304 for c in self.clients]}) |
796 | 305 |
306 class GraphResource(PrettyErrorHandler, cyclone.web.RequestHandler): | |
307 def get(self): | |
308 pass | |
309 | |
310 class Patches(PrettyErrorHandler, cyclone.web.RequestHandler): | |
311 def __init__(self, *args, **kw): | |
312 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
|
313 p = syncedgraph.makePatchEndpointPutMethod(self.settings.db.patch) |
796 | 314 self.put = lambda: p(self) |
315 | |
316 def get(self): | |
317 pass | |
318 | |
319 class GraphClients(PrettyErrorHandler, cyclone.web.RequestHandler): | |
320 def get(self): | |
321 pass | |
322 | |
323 def post(self): | |
324 upd = self.get_argument("clientUpdate") | |
325 try: | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
326 self.settings.db.addClient(upd, self.get_argument("label")) |
796 | 327 except: |
328 import traceback | |
329 traceback.print_exc() | |
330 raise | |
331 | |
332 liveClients = set() | |
333 def sendToLiveClients(d=None, asJson=None): | |
334 j = asJson or json.dumps(d) | |
335 for c in liveClients: | |
336 c.sendMessage(j) | |
337 | |
338 class Live(cyclone.websocket.WebSocketHandler): | |
339 | |
340 def connectionMade(self, *args, **kwargs): | |
811 | 341 log.info("websocket opened") |
796 | 342 liveClients.add(self) |
343 self.settings.db.sendClientsToAllLivePages() | |
344 | |
345 def connectionLost(self, reason): | |
811 | 346 log.info("websocket closed") |
796 | 347 liveClients.remove(self) |
348 | |
349 def messageReceived(self, message): | |
350 log.info("got message %s" % message) | |
351 self.sendMessage(message) | |
352 | |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
353 class NoExts(cyclone.web.StaticFileHandler): |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
354 # .xhtml pages can be get() without .xhtml on them |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
355 def get(self, path, *args, **kw): |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
356 if path and '.' not in path: |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
357 path = path + ".xhtml" |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
358 cyclone.web.StaticFileHandler.get(self, path, *args, **kw) |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
359 |
796 | 360 if __name__ == "__main__": |
361 logging.basicConfig() | |
362 log = logging.getLogger() | |
363 | |
364 parser = optparse.OptionParser() | |
365 parser.add_option('--show', | |
366 help='show URI, like http://light9.bigasterisk.com/show/dance2008', | |
367 default=showconfig.showUri()) | |
368 parser.add_option("-v", "--verbose", action="store_true", | |
369 help="logging.DEBUG") | |
370 (options, args) = parser.parse_args() | |
371 | |
372 log.setLevel(logging.DEBUG if options.verbose else logging.INFO) | |
373 | |
374 if not options.show: | |
375 raise ValueError("missing --show http://...") | |
376 | |
377 db = Db() | |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
378 |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
379 from twisted.python import log as twlog |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
380 twlog.startLogging(sys.stdout) |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
381 |
796 | 382 port = 8051 |
383 reactor.listenTCP(port, cyclone.web.Application(handlers=[ | |
384 (r'/live', Live), | |
385 (r'/graph', GraphResource), | |
386 (r'/patches', Patches), | |
387 (r'/graphClients', GraphClients), | |
388 | |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
389 (r'/(.*)', NoExts, |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
390 {"path" : "light9/rdfdb/web", |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
391 "default_filename" : "index.xhtml"}), |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
392 |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
393 ], debug=True, db=db)) |
796 | 394 log.info("serving on %s" % port) |
808
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
395 prof.run(reactor.run, profile=False) |