Mercurial > code > home > repos > light9
annotate bin/rdfdb @ 830:9ba1c866bf4c
workaround for n3 file with rules in it
Ignore-this: 67f92a3ac346476a0a6f0182f965e231
author | Drew Perttula <drewp@bigasterisk.com> |
---|---|
date | Tue, 04 Jun 2013 23:16:51 +0000 |
parents | e53e78db7b17 |
children | bdfdfea84510 |
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 |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
112 from twisted.python.filepath import FilePath |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
113 from twisted.internet.inotify import humanReadableMask, IN_CREATE |
796 | 114 import sys, optparse, logging, json, os |
115 import cyclone.web, cyclone.httpclient, cyclone.websocket | |
116 sys.path.append(".") | |
808
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
117 from light9 import networking, showconfig, prof |
796 | 118 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
|
119 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
|
120 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
|
121 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
|
122 from light9.rdfdb import syncedgraph |
796 | 123 |
124 from twisted.internet.inotify import INotify | |
125 logging.basicConfig(level=logging.DEBUG) | |
126 log = logging.getLogger() | |
127 | |
128 try: | |
129 import sys | |
130 sys.path.append("../homeauto/lib") | |
131 from cycloneerr import PrettyErrorHandler | |
132 except ImportError: | |
133 class PrettyErrorHandler(object): | |
134 pass | |
135 | |
136 class Client(object): | |
797
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 one of our syncedgraph clients |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
139 """ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
140 def __init__(self, updateUri, label, db): |
796 | 141 self.db = db |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
142 self.label = label |
796 | 143 self.updateUri = updateUri |
144 self.sendAll() | |
145 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
146 def __repr__(self): |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
147 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
|
148 |
796 | 149 def sendAll(self): |
150 """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
|
151 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
|
152 (self.label, self.updateUri)) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
153 self.sendPatch(Patch( |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
154 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
|
155 delQuads=[])) |
821 | 156 |
796 | 157 def sendPatch(self, p): |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
158 return syncedgraph.sendPatch(self.updateUri, p) |
796 | 159 |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
160 class WatchedFiles(object): |
811 | 161 """ |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
162 find files, notice new files. |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
163 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
164 This object watches directories. Each GraphFile watches its own file. |
811 | 165 """ |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
166 def __init__(self, topDirsToWatch, patch, getSubgraph): |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
167 self.topDirsToWatch = topDirsToWatch |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
168 self.patch, self.getSubgraph = patch, getSubgraph |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
169 |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
170 # 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
|
171 # 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
|
172 # 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
|
173 # as the source code?! |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
174 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
|
175 |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
176 self.graphFiles = {} # context uri : GraphFile |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
177 |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
178 self.notifier = INotify() |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
179 self.notifier.startReading() |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
180 |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
181 self.findAndLoadFiles() |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
182 |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
183 def findAndLoadFiles(self): |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
184 self.initialLoad = True |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
185 try: |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
186 for topdir in self.topDirsToWatch: |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
187 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
|
188 for base in filenames: |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
189 self.watchFile(os.path.join(dirpath, base)) |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
190 self.notifier.watch(FilePath(dirpath), autoAdd=True, |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
191 callbacks=[self.dirChange]) |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
192 finally: |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
193 self.initialLoad = False |
821 | 194 |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
195 def dirChange(self, watch, path, mask): |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
196 if mask & IN_CREATE: |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
197 self.watchFile(path.path) |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
198 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
199 def watchFile(self, inFile): |
830
9ba1c866bf4c
workaround for n3 file with rules in it
Drew Perttula <drewp@bigasterisk.com>
parents:
829
diff
changeset
|
200 """consider adding a GraphFile to self.graphFiles""" |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
201 if not isinstance(inFile, FilePath): |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
202 inFile = FilePath(inFile) |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
203 if not inFile.isfile(): |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
204 return |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
205 if inFile.splitext()[1] not in ['.n3']: |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
206 return |
830
9ba1c866bf4c
workaround for n3 file with rules in it
Drew Perttula <drewp@bigasterisk.com>
parents:
829
diff
changeset
|
207 |
9ba1c866bf4c
workaround for n3 file with rules in it
Drew Perttula <drewp@bigasterisk.com>
parents:
829
diff
changeset
|
208 # an n3 file with rules makes it all the way past this reading |
9ba1c866bf4c
workaround for n3 file with rules in it
Drew Perttula <drewp@bigasterisk.com>
parents:
829
diff
changeset
|
209 # and the serialization. Then, on the receiving side, a |
9ba1c866bf4c
workaround for n3 file with rules in it
Drew Perttula <drewp@bigasterisk.com>
parents:
829
diff
changeset
|
210 # SyncedGraph calls graphFromNQuad on the incoming data and |
9ba1c866bf4c
workaround for n3 file with rules in it
Drew Perttula <drewp@bigasterisk.com>
parents:
829
diff
changeset
|
211 # has a parse error. I'm not sure where this should be fixed |
9ba1c866bf4c
workaround for n3 file with rules in it
Drew Perttula <drewp@bigasterisk.com>
parents:
829
diff
changeset
|
212 # yet. |
9ba1c866bf4c
workaround for n3 file with rules in it
Drew Perttula <drewp@bigasterisk.com>
parents:
829
diff
changeset
|
213 if '-rules' in inFile.path: |
9ba1c866bf4c
workaround for n3 file with rules in it
Drew Perttula <drewp@bigasterisk.com>
parents:
829
diff
changeset
|
214 return |
9ba1c866bf4c
workaround for n3 file with rules in it
Drew Perttula <drewp@bigasterisk.com>
parents:
829
diff
changeset
|
215 |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
216 ctx = self.uriFromFile(inFile) |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
217 gf = GraphFile(self.notifier, inFile.path, ctx, |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
218 self.patch, self.getSubgraph) |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
219 self.graphFiles[ctx] = gf |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
220 gf.reread() |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
221 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
222 def dirtyFiles(self, ctxs): |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
223 """mark dirty the files that we watch in these contexts. |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
224 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
225 the ctx might not be a file that we already read; it might be |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
226 for a new file we have to create, or it might be for a |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
227 transient context that we're not going to save |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
228 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
229 if it's a ctx with no file, error |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
230 """ |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
231 for ctx in ctxs: |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
232 g = self.getSubgraph(ctx) |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
233 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
234 if ctx not in self.graphFiles: |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
235 outFile = self.fileForUri(ctx) |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
236 self.graphFiles[ctx] = GraphFile(self.notifier, outFile, ctx, |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
237 self.patch, self.getSubgraph) |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
238 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
239 self.graphFiles[ctx].dirty(g) |
796 | 240 |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
241 def uriFromFile(self, filename): |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
242 if isinstance(filename, FilePath): |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
243 filename = filename.path |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
244 assert filename.endswith('.n3'), filename |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
245 return URIRef(self.topUri + filename[:-len('.n3')]) |
821 | 246 |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
247 def fileForUri(self, ctx): |
826
05aabe3d7b02
fix some minor issues with graph contexts
Drew Perttula <drewp@bigasterisk.com>
parents:
821
diff
changeset
|
248 assert isinstance(ctx, URIRef), ctx |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
249 if not ctx.startswith(self.topUri): |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
250 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
|
251 return ctx[len(self.topUri):] + ".n3" |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
252 |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
253 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
254 class Db(object): |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
255 """ |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
256 the master graph, all the connected clients, all the files we're watching |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
257 """ |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
258 def __init__(self, topDirsToWatch): |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
259 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
260 self.clients = [] |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
261 self.graph = ConjunctiveGraph() |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
262 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
263 self.watchedFiles = WatchedFiles(topDirsToWatch, |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
264 self.patch, self.getSubgraph) |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
265 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
266 self.summarizeToLog() |
821 | 267 |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
268 def patch(self, p, dueToFileChange=False): |
796 | 269 """ |
270 apply this patch to the master graph then notify everyone about it | |
811 | 271 |
272 dueToFileChange if this is a patch describing an edit we read | |
273 *from* the file (such that we shouldn't write it back to the file) | |
274 | |
275 if p has a senderUpdateUri attribute, we won't send this patch | |
276 back to the sender with that updateUri | |
796 | 277 """ |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
278 ctx = p.getContext() |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
279 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
|
280 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
|
281 |
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
|
282 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
|
283 senderUpdateUri = getattr(p, 'senderUpdateUri', None) |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
284 |
796 | 285 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
|
286 if c.updateUri == senderUpdateUri: |
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
287 # 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
|
288 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
|
289 d = c.sendPatch(p) |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
290 d.addErrback(self.clientErrored, c) |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
291 if not dueToFileChange: |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
292 self.watchedFiles.dirtyFiles([ctx]) |
796 | 293 sendToLiveClients(asJson=p.jsonRepr) |
294 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
295 def clientErrored(self, err, c): |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
296 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
|
297 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
|
298 self.clients.remove(c) |
821 | 299 self.sendClientsToAllLivePages() |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
300 |
796 | 301 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
|
302 log.info("contexts in graph (%s total stmts):" % len(self.graph)) |
796 | 303 for c in self.graph.contexts(): |
304 log.info(" %s: %s statements" % | |
305 (c.identifier, len(self.getSubgraph(c.identifier)))) | |
306 | |
307 def getSubgraph(self, uri): | |
811 | 308 """ |
309 this is meant to return a live view of the given subgraph, but | |
310 if i'm still working around an rdflib bug, it might return a | |
311 copy | |
312 | |
313 and it's returning triples, but I think quads would be better | |
314 """ | |
796 | 315 # this is returning an empty Graph :( |
316 #return self.graph.get_context(uri) | |
317 | |
318 g = Graph() | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
319 for s in self.graph.triples(ALLSTMTS, uri): |
796 | 320 g.add(s) |
321 return g | |
821 | 322 |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
323 def addClient(self, updateUri, label): |
796 | 324 [self.clients.remove(c) |
325 for c in self.clients if c.updateUri == updateUri] | |
326 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
327 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
|
328 self.clients.append(Client(updateUri, label, self)) |
796 | 329 self.sendClientsToAllLivePages() |
330 | |
331 def sendClientsToAllLivePages(self): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
332 sendToLiveClients({"clients":[ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
333 dict(updateUri=c.updateUri, label=c.label) |
821 | 334 for c in self.clients]}) |
796 | 335 |
336 class GraphResource(PrettyErrorHandler, cyclone.web.RequestHandler): | |
337 def get(self): | |
821 | 338 self.write(self.settings.db.graph.serialize(format='n3')) |
339 | |
796 | 340 class Patches(PrettyErrorHandler, cyclone.web.RequestHandler): |
341 def __init__(self, *args, **kw): | |
342 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
|
343 p = syncedgraph.makePatchEndpointPutMethod(self.settings.db.patch) |
796 | 344 self.put = lambda: p(self) |
345 | |
346 def get(self): | |
347 pass | |
348 | |
349 class GraphClients(PrettyErrorHandler, cyclone.web.RequestHandler): | |
350 def get(self): | |
351 pass | |
821 | 352 |
796 | 353 def post(self): |
354 upd = self.get_argument("clientUpdate") | |
355 try: | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
356 self.settings.db.addClient(upd, self.get_argument("label")) |
796 | 357 except: |
358 import traceback | |
359 traceback.print_exc() | |
360 raise | |
361 | |
362 liveClients = set() | |
363 def sendToLiveClients(d=None, asJson=None): | |
364 j = asJson or json.dumps(d) | |
365 for c in liveClients: | |
366 c.sendMessage(j) | |
367 | |
368 class Live(cyclone.websocket.WebSocketHandler): | |
821 | 369 |
796 | 370 def connectionMade(self, *args, **kwargs): |
811 | 371 log.info("websocket opened") |
796 | 372 liveClients.add(self) |
373 self.settings.db.sendClientsToAllLivePages() | |
374 | |
375 def connectionLost(self, reason): | |
811 | 376 log.info("websocket closed") |
796 | 377 liveClients.remove(self) |
378 | |
379 def messageReceived(self, message): | |
380 log.info("got message %s" % message) | |
381 self.sendMessage(message) | |
382 | |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
383 class NoExts(cyclone.web.StaticFileHandler): |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
384 # .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
|
385 def get(self, path, *args, **kw): |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
386 if path and '.' not in path: |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
387 path = path + ".xhtml" |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
388 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
|
389 |
796 | 390 if __name__ == "__main__": |
391 logging.basicConfig() | |
392 log = logging.getLogger() | |
393 | |
394 parser = optparse.OptionParser() | |
395 parser.add_option('--show', | |
396 help='show URI, like http://light9.bigasterisk.com/show/dance2008', | |
397 default=showconfig.showUri()) | |
398 parser.add_option("-v", "--verbose", action="store_true", | |
399 help="logging.DEBUG") | |
400 (options, args) = parser.parse_args() | |
401 | |
402 log.setLevel(logging.DEBUG if options.verbose else logging.INFO) | |
403 | |
404 if not options.show: | |
405 raise ValueError("missing --show http://...") | |
406 | |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
407 db = Db(topDirsToWatch=['show/dance2013']) |
821 | 408 |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
409 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
|
410 twlog.startLogging(sys.stdout) |
821 | 411 |
796 | 412 port = 8051 |
413 reactor.listenTCP(port, cyclone.web.Application(handlers=[ | |
414 (r'/live', Live), | |
415 (r'/graph', GraphResource), | |
416 (r'/patches', Patches), | |
417 (r'/graphClients', GraphClients), | |
418 | |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
419 (r'/(.*)', NoExts, |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
420 {"path" : "light9/rdfdb/web", |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
421 "default_filename" : "index.xhtml"}), |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
422 |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
423 ], debug=True, db=db)) |
796 | 424 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
|
425 prof.run(reactor.run, profile=False) |