Mercurial > code > home > repos > light9
annotate bin/rdfdb @ 1336:2d77dc9436c3
music drives cursor
Ignore-this: ffa7a9a94084bb8feab6d04e888fb3f6
author | Drew Perttula <drewp@bigasterisk.com> |
---|---|
date | Sat, 04 Jun 2016 21:23:58 +0000 |
parents | d5c834766ce9 |
children | a214a9d6f2f0 |
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 |
1045
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
122 from light9.rdfdb.file_vs_uri import correctToTopdirPrefix, fileForUri, uriFromFile |
843
77b5dbcf688d
split syncedgraph into several layers
drewp@bigasterisk.com
parents:
833
diff
changeset
|
123 from light9.rdfdb.patchsender import sendPatch |
77b5dbcf688d
split syncedgraph into several layers
drewp@bigasterisk.com
parents:
833
diff
changeset
|
124 from light9.rdfdb.patchreceiver import makePatchEndpointPutMethod |
796 | 125 |
126 from twisted.internet.inotify import INotify | |
833 | 127 from run_local import log |
128 log.setLevel(logging.DEBUG) | |
796 | 129 |
1060
473db8bebb8f
install a copy of cycloneerr.py
Drew Perttula <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
130 from lib.cycloneerr import PrettyErrorHandler |
796 | 131 |
132 class Client(object): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
133 """ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
134 one of our syncedgraph clients |
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 def __init__(self, updateUri, label, db): |
796 | 137 self.db = db |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
138 self.label = label |
796 | 139 self.updateUri = updateUri |
140 self.sendAll() | |
141 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
142 def __repr__(self): |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
143 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
|
144 |
796 | 145 def sendAll(self): |
146 """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
|
147 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
|
148 (self.label, self.updateUri)) |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
149 self.sendPatch(Patch( |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
150 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
|
151 delQuads=[])) |
821 | 152 |
796 | 153 def sendPatch(self, p): |
843
77b5dbcf688d
split syncedgraph into several layers
drewp@bigasterisk.com
parents:
833
diff
changeset
|
154 return sendPatch(self.updateUri, p) |
796 | 155 |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
156 class WatchedFiles(object): |
811 | 157 """ |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
158 find files, notice new files. |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
159 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
160 This object watches directories. Each GraphFile watches its own file. |
811 | 161 """ |
1045
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
162 def __init__(self, dirUriMap, patch, getSubgraph): |
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
163 self.dirUriMap = dirUriMap # {abspath : uri prefix} |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
164 self.patch, self.getSubgraph = patch, getSubgraph |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
165 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
166 self.graphFiles = {} # context uri : GraphFile |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
167 |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
168 self.notifier = INotify() |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
169 self.notifier.startReading() |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
170 |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
171 self.findAndLoadFiles() |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
172 |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
173 def findAndLoadFiles(self): |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
174 self.initialLoad = True |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
175 try: |
1045
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
176 for topdir in self.dirUriMap: |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
177 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
|
178 for base in filenames: |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
179 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
|
180 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
|
181 callbacks=[self.dirChange]) |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
182 finally: |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
183 self.initialLoad = False |
821 | 184 |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
185 def dirChange(self, watch, path, mask): |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
186 if mask & IN_CREATE: |
924
dcab422615ca
working on local->global sub action. creation of new file in rdfdb is still a problem
drewp@bigasterisk.com
parents:
921
diff
changeset
|
187 log.debug("%s created; consider adding a watch", path) |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
188 self.watchFile(path.path) |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
189 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
190 def watchFile(self, inFile): |
857
1bda494a8c3a
attempted fix for FilePath making paths absolute (when they need to stay relative since they're used in uris)
drewp@bigasterisk.com
parents:
843
diff
changeset
|
191 """ |
1bda494a8c3a
attempted fix for FilePath making paths absolute (when they need to stay relative since they're used in uris)
drewp@bigasterisk.com
parents:
843
diff
changeset
|
192 consider adding a GraphFile to self.graphFiles |
1bda494a8c3a
attempted fix for FilePath making paths absolute (when they need to stay relative since they're used in uris)
drewp@bigasterisk.com
parents:
843
diff
changeset
|
193 |
1bda494a8c3a
attempted fix for FilePath making paths absolute (when they need to stay relative since they're used in uris)
drewp@bigasterisk.com
parents:
843
diff
changeset
|
194 inFile needs to be a relative path, not an absolute (e.g. in a |
1bda494a8c3a
attempted fix for FilePath making paths absolute (when they need to stay relative since they're used in uris)
drewp@bigasterisk.com
parents:
843
diff
changeset
|
195 FilePath) because we use its exact relative form in the |
1bda494a8c3a
attempted fix for FilePath making paths absolute (when they need to stay relative since they're used in uris)
drewp@bigasterisk.com
parents:
843
diff
changeset
|
196 context URI |
1bda494a8c3a
attempted fix for FilePath making paths absolute (when they need to stay relative since they're used in uris)
drewp@bigasterisk.com
parents:
843
diff
changeset
|
197 """ |
1bda494a8c3a
attempted fix for FilePath making paths absolute (when they need to stay relative since they're used in uris)
drewp@bigasterisk.com
parents:
843
diff
changeset
|
198 if not os.path.isfile(inFile): |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
199 return |
1045
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
200 |
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
201 inFile = correctToTopdirPrefix(self.dirUriMap, inFile) |
857
1bda494a8c3a
attempted fix for FilePath making paths absolute (when they need to stay relative since they're used in uris)
drewp@bigasterisk.com
parents:
843
diff
changeset
|
202 if os.path.splitext(inFile)[1] not in ['.n3']: |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
203 return |
830
9ba1c866bf4c
workaround for n3 file with rules in it
Drew Perttula <drewp@bigasterisk.com>
parents:
829
diff
changeset
|
204 |
9ba1c866bf4c
workaround for n3 file with rules in it
Drew Perttula <drewp@bigasterisk.com>
parents:
829
diff
changeset
|
205 # 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
|
206 # 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
|
207 # 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
|
208 # 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
|
209 # yet. |
857
1bda494a8c3a
attempted fix for FilePath making paths absolute (when they need to stay relative since they're used in uris)
drewp@bigasterisk.com
parents:
843
diff
changeset
|
210 if '-rules' in inFile: |
830
9ba1c866bf4c
workaround for n3 file with rules in it
Drew Perttula <drewp@bigasterisk.com>
parents:
829
diff
changeset
|
211 return |
904
86c6700d1d63
2013 code ignores 'config.n3' so 2012 code can use that instead
Drew Perttula <drewp@bigasterisk.com>
parents:
899
diff
changeset
|
212 |
86c6700d1d63
2013 code ignores 'config.n3' so 2012 code can use that instead
Drew Perttula <drewp@bigasterisk.com>
parents:
899
diff
changeset
|
213 # for legacy versions, compile all the config stuff you want |
86c6700d1d63
2013 code ignores 'config.n3' so 2012 code can use that instead
Drew Perttula <drewp@bigasterisk.com>
parents:
899
diff
changeset
|
214 # read into one file called config.n3. New versions won't read |
86c6700d1d63
2013 code ignores 'config.n3' so 2012 code can use that instead
Drew Perttula <drewp@bigasterisk.com>
parents:
899
diff
changeset
|
215 # it. |
86c6700d1d63
2013 code ignores 'config.n3' so 2012 code can use that instead
Drew Perttula <drewp@bigasterisk.com>
parents:
899
diff
changeset
|
216 if inFile.endswith("config.n3"): |
86c6700d1d63
2013 code ignores 'config.n3' so 2012 code can use that instead
Drew Perttula <drewp@bigasterisk.com>
parents:
899
diff
changeset
|
217 return |
830
9ba1c866bf4c
workaround for n3 file with rules in it
Drew Perttula <drewp@bigasterisk.com>
parents:
829
diff
changeset
|
218 |
1045
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
219 ctx = uriFromFile(self.dirUriMap, inFile) |
857
1bda494a8c3a
attempted fix for FilePath making paths absolute (when they need to stay relative since they're used in uris)
drewp@bigasterisk.com
parents:
843
diff
changeset
|
220 gf = GraphFile(self.notifier, inFile, ctx, |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
221 self.patch, self.getSubgraph) |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
222 self.graphFiles[ctx] = gf |
924
dcab422615ca
working on local->global sub action. creation of new file in rdfdb is still a problem
drewp@bigasterisk.com
parents:
921
diff
changeset
|
223 log.info("%s do initial read", inFile) |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
224 gf.reread() |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
225 |
924
dcab422615ca
working on local->global sub action. creation of new file in rdfdb is still a problem
drewp@bigasterisk.com
parents:
921
diff
changeset
|
226 def aboutToPatch(self, ctx): |
dcab422615ca
working on local->global sub action. creation of new file in rdfdb is still a problem
drewp@bigasterisk.com
parents:
921
diff
changeset
|
227 """ |
dcab422615ca
working on local->global sub action. creation of new file in rdfdb is still a problem
drewp@bigasterisk.com
parents:
921
diff
changeset
|
228 warn us that a patch is about to come to this context. it's more |
dcab422615ca
working on local->global sub action. creation of new file in rdfdb is still a problem
drewp@bigasterisk.com
parents:
921
diff
changeset
|
229 straightforward to create the new file now |
931 | 230 |
231 this is meant to make the file before we add triples, so we | |
232 wouldn't see the blank file and lose those triples. But it | |
233 didn't work, so there are other measures that make us not lose | |
234 the triples from a new file. Calling this before patching the | |
235 graph is still a reasonable thing to do, though. | |
924
dcab422615ca
working on local->global sub action. creation of new file in rdfdb is still a problem
drewp@bigasterisk.com
parents:
921
diff
changeset
|
236 """ |
dcab422615ca
working on local->global sub action. creation of new file in rdfdb is still a problem
drewp@bigasterisk.com
parents:
921
diff
changeset
|
237 g = self.getSubgraph(ctx) |
dcab422615ca
working on local->global sub action. creation of new file in rdfdb is still a problem
drewp@bigasterisk.com
parents:
921
diff
changeset
|
238 |
dcab422615ca
working on local->global sub action. creation of new file in rdfdb is still a problem
drewp@bigasterisk.com
parents:
921
diff
changeset
|
239 if ctx not in self.graphFiles: |
1045
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
240 outFile = fileForUri(self.dirUriMap, ctx) |
1282
2c0c3aec0d63
critical fix- put slash in the uri map prefix so it gets removed from uris, so filenames don't have // in them and fail to compare right to other versions
drewp@bigasterisk.com
parents:
1229
diff
changeset
|
241 assert '//' not in outFile, (outFile, self.dirUriMap, ctx) |
924
dcab422615ca
working on local->global sub action. creation of new file in rdfdb is still a problem
drewp@bigasterisk.com
parents:
921
diff
changeset
|
242 log.info("starting new file %r", outFile) |
dcab422615ca
working on local->global sub action. creation of new file in rdfdb is still a problem
drewp@bigasterisk.com
parents:
921
diff
changeset
|
243 self.graphFiles[ctx] = GraphFile(self.notifier, outFile, ctx, |
dcab422615ca
working on local->global sub action. creation of new file in rdfdb is still a problem
drewp@bigasterisk.com
parents:
921
diff
changeset
|
244 self.patch, self.getSubgraph) |
dcab422615ca
working on local->global sub action. creation of new file in rdfdb is still a problem
drewp@bigasterisk.com
parents:
921
diff
changeset
|
245 |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
246 def dirtyFiles(self, ctxs): |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
247 """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
|
248 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
249 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
|
250 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
|
251 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
|
252 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
253 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
|
254 """ |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
255 for ctx in ctxs: |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
256 g = self.getSubgraph(ctx) |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
257 self.graphFiles[ctx].dirty(g) |
796 | 258 |
829
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 class Db(object): |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
261 """ |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
262 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
|
263 """ |
1045
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
264 def __init__(self, dirUriMap): |
829
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.clients = [] |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
267 self.graph = ConjunctiveGraph() |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
268 |
1045
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
269 self.watchedFiles = WatchedFiles(dirUriMap, |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
270 self.patch, self.getSubgraph) |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
271 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
272 self.summarizeToLog() |
821 | 273 |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
274 def patch(self, p, dueToFileChange=False): |
796 | 275 """ |
276 apply this patch to the master graph then notify everyone about it | |
811 | 277 |
278 dueToFileChange if this is a patch describing an edit we read | |
279 *from* the file (such that we shouldn't write it back to the file) | |
280 | |
281 if p has a senderUpdateUri attribute, we won't send this patch | |
282 back to the sender with that updateUri | |
796 | 283 """ |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
284 ctx = p.getContext() |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
285 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
|
286 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
|
287 |
931 | 288 if hasattr(self, 'watchedFiles'): # not available during startup |
924
dcab422615ca
working on local->global sub action. creation of new file in rdfdb is still a problem
drewp@bigasterisk.com
parents:
921
diff
changeset
|
289 self.watchedFiles.aboutToPatch(ctx) |
dcab422615ca
working on local->global sub action. creation of new file in rdfdb is still a problem
drewp@bigasterisk.com
parents:
921
diff
changeset
|
290 |
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
|
291 patchQuads(self.graph, p.delQuads, p.addQuads, perfect=True) |
1083
7aca3145c96e
refactor rdfdb patch()
Drew Perttula <drewp@bigasterisk.com>
parents:
1060
diff
changeset
|
292 self._sendPatch(p) |
7aca3145c96e
refactor rdfdb patch()
Drew Perttula <drewp@bigasterisk.com>
parents:
1060
diff
changeset
|
293 if not dueToFileChange: |
7aca3145c96e
refactor rdfdb patch()
Drew Perttula <drewp@bigasterisk.com>
parents:
1060
diff
changeset
|
294 self.watchedFiles.dirtyFiles([ctx]) |
7aca3145c96e
refactor rdfdb patch()
Drew Perttula <drewp@bigasterisk.com>
parents:
1060
diff
changeset
|
295 sendToLiveClients(asJson=p.jsonRepr) |
7aca3145c96e
refactor rdfdb patch()
Drew Perttula <drewp@bigasterisk.com>
parents:
1060
diff
changeset
|
296 |
7aca3145c96e
refactor rdfdb patch()
Drew Perttula <drewp@bigasterisk.com>
parents:
1060
diff
changeset
|
297 def _sendPatch(self, p): |
808
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
298 senderUpdateUri = getattr(p, 'senderUpdateUri', None) |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
299 |
796 | 300 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
|
301 if c.updateUri == senderUpdateUri: |
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
302 # 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
|
303 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
|
304 d = c.sendPatch(p) |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
305 d.addErrback(self.clientErrored, c) |
1083
7aca3145c96e
refactor rdfdb patch()
Drew Perttula <drewp@bigasterisk.com>
parents:
1060
diff
changeset
|
306 |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
307 def clientErrored(self, err, c): |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
308 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
|
309 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
|
310 self.clients.remove(c) |
821 | 311 self.sendClientsToAllLivePages() |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
312 |
796 | 313 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
|
314 log.info("contexts in graph (%s total stmts):" % len(self.graph)) |
796 | 315 for c in self.graph.contexts(): |
316 log.info(" %s: %s statements" % | |
317 (c.identifier, len(self.getSubgraph(c.identifier)))) | |
318 | |
319 def getSubgraph(self, uri): | |
811 | 320 """ |
321 this is meant to return a live view of the given subgraph, but | |
322 if i'm still working around an rdflib bug, it might return a | |
323 copy | |
324 | |
325 and it's returning triples, but I think quads would be better | |
326 """ | |
796 | 327 # this is returning an empty Graph :( |
328 #return self.graph.get_context(uri) | |
329 | |
330 g = Graph() | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
331 for s in self.graph.triples(ALLSTMTS, uri): |
796 | 332 g.add(s) |
333 return g | |
821 | 334 |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
335 def addClient(self, updateUri, label): |
796 | 336 [self.clients.remove(c) |
337 for c in self.clients if c.updateUri == updateUri] | |
338 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
339 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
|
340 self.clients.append(Client(updateUri, label, self)) |
796 | 341 self.sendClientsToAllLivePages() |
342 | |
343 def sendClientsToAllLivePages(self): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
344 sendToLiveClients({"clients":[ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
345 dict(updateUri=c.updateUri, label=c.label) |
821 | 346 for c in self.clients]}) |
796 | 347 |
348 class GraphResource(PrettyErrorHandler, cyclone.web.RequestHandler): | |
349 def get(self): | |
905
741aa0f9be48
very simple con-neg on rdfdb /graph to support text/plain (ntriples)
Drew Perttula <drewp@bigasterisk.com>
parents:
904
diff
changeset
|
350 accept = self.request.headers.get('accept', '') |
741aa0f9be48
very simple con-neg on rdfdb /graph to support text/plain (ntriples)
Drew Perttula <drewp@bigasterisk.com>
parents:
904
diff
changeset
|
351 format = 'n3' |
741aa0f9be48
very simple con-neg on rdfdb /graph to support text/plain (ntriples)
Drew Perttula <drewp@bigasterisk.com>
parents:
904
diff
changeset
|
352 if accept == 'text/plain': |
741aa0f9be48
very simple con-neg on rdfdb /graph to support text/plain (ntriples)
Drew Perttula <drewp@bigasterisk.com>
parents:
904
diff
changeset
|
353 format = 'nt' |
1032
54027815c6cc
rdfdb can return 'application/n-quads' response
Drew Perttula <drewp@bigasterisk.com>
parents:
1017
diff
changeset
|
354 elif accept == 'application/n-quads': |
54027815c6cc
rdfdb can return 'application/n-quads' response
Drew Perttula <drewp@bigasterisk.com>
parents:
1017
diff
changeset
|
355 format = 'nquads' |
905
741aa0f9be48
very simple con-neg on rdfdb /graph to support text/plain (ntriples)
Drew Perttula <drewp@bigasterisk.com>
parents:
904
diff
changeset
|
356 self.write(self.settings.db.graph.serialize(format=format)) |
821 | 357 |
796 | 358 class Patches(PrettyErrorHandler, cyclone.web.RequestHandler): |
359 def __init__(self, *args, **kw): | |
360 cyclone.web.RequestHandler.__init__(self, *args, **kw) | |
843
77b5dbcf688d
split syncedgraph into several layers
drewp@bigasterisk.com
parents:
833
diff
changeset
|
361 p = makePatchEndpointPutMethod(self.settings.db.patch) |
796 | 362 self.put = lambda: p(self) |
363 | |
364 def get(self): | |
365 pass | |
366 | |
367 class GraphClients(PrettyErrorHandler, cyclone.web.RequestHandler): | |
368 def get(self): | |
369 pass | |
821 | 370 |
796 | 371 def post(self): |
372 upd = self.get_argument("clientUpdate") | |
373 try: | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
374 self.settings.db.addClient(upd, self.get_argument("label")) |
796 | 375 except: |
376 import traceback | |
377 traceback.print_exc() | |
378 raise | |
379 | |
380 liveClients = set() | |
381 def sendToLiveClients(d=None, asJson=None): | |
382 j = asJson or json.dumps(d) | |
383 for c in liveClients: | |
384 c.sendMessage(j) | |
385 | |
386 class Live(cyclone.websocket.WebSocketHandler): | |
821 | 387 |
796 | 388 def connectionMade(self, *args, **kwargs): |
811 | 389 log.info("websocket opened") |
796 | 390 liveClients.add(self) |
391 self.settings.db.sendClientsToAllLivePages() | |
392 | |
393 def connectionLost(self, reason): | |
811 | 394 log.info("websocket closed") |
796 | 395 liveClients.remove(self) |
396 | |
397 def messageReceived(self, message): | |
398 log.info("got message %s" % message) | |
399 self.sendMessage(message) | |
400 | |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
401 class NoExts(cyclone.web.StaticFileHandler): |
1309 | 402 # .html pages can be get() without .html on them |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
403 def get(self, path, *args, **kw): |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
404 if path and '.' not in path: |
1309 | 405 path = path + ".html" |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
406 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
|
407 |
796 | 408 if __name__ == "__main__": |
409 logging.basicConfig() | |
410 log = logging.getLogger() | |
411 | |
412 parser = optparse.OptionParser() | |
413 parser.add_option("-v", "--verbose", action="store_true", | |
414 help="logging.DEBUG") | |
415 (options, args) = parser.parse_args() | |
416 | |
417 log.setLevel(logging.DEBUG if options.verbose else logging.INFO) | |
418 | |
1045
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
419 db = Db(dirUriMap={os.environ['LIGHT9_SHOW'].rstrip('/') + '/': |
1282
2c0c3aec0d63
critical fix- put slash in the uri map prefix so it gets removed from uris, so filenames don't have // in them and fail to compare right to other versions
drewp@bigasterisk.com
parents:
1229
diff
changeset
|
420 showconfig.showUri() + '/'}) |
821 | 421 |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
422 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
|
423 twlog.startLogging(sys.stdout) |
821 | 424 |
1225
70f42f9d6e04
simplify homepageConfig. get homepage port from rdf. get rdfdb port from rdf
Drew Perttula <drewp@bigasterisk.com>
parents:
1212
diff
changeset
|
425 reactor.listenTCP(networking.rdfdb.port, cyclone.web.Application(handlers=[ |
796 | 426 (r'/live', Live), |
427 (r'/graph', GraphResource), | |
428 (r'/patches', Patches), | |
429 (r'/graphClients', GraphClients), | |
430 | |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
431 (r'/(.*)', NoExts, |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
432 {"path" : "light9/rdfdb/web", |
1309 | 433 "default_filename" : "index.html"}), |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
434 |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
435 ], debug=True, db=db)) |
1225
70f42f9d6e04
simplify homepageConfig. get homepage port from rdf. get rdfdb port from rdf
Drew Perttula <drewp@bigasterisk.com>
parents:
1212
diff
changeset
|
436 log.info("serving on %s" % networking.rdfdb.port) |
1178
a296d842d0da
better cmdline option error
Drew Perttula <drewp@bigasterisk.com>
parents:
1083
diff
changeset
|
437 prof.run(reactor.run, profile=None) |