Mercurial > code > home > repos > light9
annotate bin/rdfdb @ 1447:8a9a9b58a4e2
live page now knows that collector needs all settings on each request
Ignore-this: 79ad8d1e001b98b6fce6d14f75f92745
author | drewp@bigasterisk.com |
---|---|
date | Sat, 11 Jun 2016 21:40:37 +0000 |
parents | 6044e527d795 |
children | 3ca103d97f98 |
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 """ |
1347
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
110 from twisted.internet import reactor, defer |
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 |
1347
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
113 from twisted.python.failure import Failure |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
114 from twisted.internet.inotify import humanReadableMask, IN_CREATE |
796 | 115 import sys, optparse, logging, json, os |
116 import cyclone.web, cyclone.httpclient, cyclone.websocket | |
117 sys.path.append(".") | |
808
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
118 from light9 import networking, showconfig, prof |
796 | 119 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
|
120 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
|
121 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
|
122 from light9.rdfdb.rdflibpatch import patchQuads |
1045
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
123 from light9.rdfdb.file_vs_uri import correctToTopdirPrefix, fileForUri, uriFromFile |
843
77b5dbcf688d
split syncedgraph into several layers
drewp@bigasterisk.com
parents:
833
diff
changeset
|
124 from light9.rdfdb.patchsender import sendPatch |
77b5dbcf688d
split syncedgraph into several layers
drewp@bigasterisk.com
parents:
833
diff
changeset
|
125 from light9.rdfdb.patchreceiver import makePatchEndpointPutMethod |
796 | 126 |
127 from twisted.internet.inotify import INotify | |
833 | 128 from run_local import log |
129 log.setLevel(logging.DEBUG) | |
796 | 130 |
1060
473db8bebb8f
install a copy of cycloneerr.py
Drew Perttula <drewp@bigasterisk.com>
parents:
1045
diff
changeset
|
131 from lib.cycloneerr import PrettyErrorHandler |
796 | 132 |
1347
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
133 class WebsocketDisconnect(ValueError): |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
134 pass |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
135 |
1345
13f758eda3b4
refactor rdfdb: sendGraphToClient
Drew Perttula <drewp@bigasterisk.com>
parents:
1344
diff
changeset
|
136 def sendGraphToClient(graph, client): |
13f758eda3b4
refactor rdfdb: sendGraphToClient
Drew Perttula <drewp@bigasterisk.com>
parents:
1344
diff
changeset
|
137 """send the client the whole graph contents""" |
1347
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
138 log.info("sending all graphs to %r" % client) |
1345
13f758eda3b4
refactor rdfdb: sendGraphToClient
Drew Perttula <drewp@bigasterisk.com>
parents:
1344
diff
changeset
|
139 client.sendPatch(Patch( |
13f758eda3b4
refactor rdfdb: sendGraphToClient
Drew Perttula <drewp@bigasterisk.com>
parents:
1344
diff
changeset
|
140 addQuads=graph.quads(ALLSTMTS), |
13f758eda3b4
refactor rdfdb: sendGraphToClient
Drew Perttula <drewp@bigasterisk.com>
parents:
1344
diff
changeset
|
141 delQuads=[])) |
13f758eda3b4
refactor rdfdb: sendGraphToClient
Drew Perttula <drewp@bigasterisk.com>
parents:
1344
diff
changeset
|
142 |
13f758eda3b4
refactor rdfdb: sendGraphToClient
Drew Perttula <drewp@bigasterisk.com>
parents:
1344
diff
changeset
|
143 |
796 | 144 class Client(object): |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
145 """ |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
146 one of our syncedgraph clients |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
147 """ |
1343
a214a9d6f2f0
refactor rdfdb: Client doesn't need Db at construction
Drew Perttula <drewp@bigasterisk.com>
parents:
1309
diff
changeset
|
148 def __init__(self, updateUri, label): |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
149 self.label = label |
1347
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
150 # todo: updateUri is used publicly to compare clients. Replace |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
151 # it with Client.__eq__ so WsClient doesn't have to fake an |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
152 # updateUri. |
796 | 153 self.updateUri = updateUri |
154 | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
155 def __repr__(self): |
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
156 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
|
157 |
796 | 158 def sendPatch(self, p): |
1347
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
159 """ |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
160 returns deferred. error will be interpreted as the client being |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
161 broken. |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
162 """ |
843
77b5dbcf688d
split syncedgraph into several layers
drewp@bigasterisk.com
parents:
833
diff
changeset
|
163 return sendPatch(self.updateUri, p) |
1347
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
164 |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
165 class WsClient(object): |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
166 def __init__(self, connectionId, sendMessage): |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
167 self.updateUri = connectionId |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
168 self.sendMessage = sendMessage |
796 | 169 |
1347
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
170 def __repr__(self): |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
171 return "<WsClient %s>" % self.updateUri |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
172 |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
173 def sendPatch(self, p): |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
174 self.sendMessage(p.makeJsonRepr()) |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
175 return defer.succeed(None) |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
176 |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
177 class WatchedFiles(object): |
811 | 178 """ |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
179 find files, notice new files. |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
180 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
181 This object watches directories. Each GraphFile watches its own file. |
811 | 182 """ |
1045
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
183 def __init__(self, dirUriMap, patch, getSubgraph): |
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
184 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
|
185 self.patch, self.getSubgraph = patch, getSubgraph |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
186 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
187 self.graphFiles = {} # context uri : GraphFile |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
188 |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
189 self.notifier = INotify() |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
190 self.notifier.startReading() |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
191 |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
192 self.findAndLoadFiles() |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
193 |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
194 def findAndLoadFiles(self): |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
195 self.initialLoad = True |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
196 try: |
1045
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
197 for topdir in self.dirUriMap: |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
198 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
|
199 for base in filenames: |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
200 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
|
201 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
|
202 callbacks=[self.dirChange]) |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
203 finally: |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
204 self.initialLoad = False |
821 | 205 |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
206 def dirChange(self, watch, path, mask): |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
207 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
|
208 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
|
209 self.watchFile(path.path) |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
210 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
211 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
|
212 """ |
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
|
213 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
|
214 |
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
|
215 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
|
216 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
|
217 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
|
218 """ |
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
|
219 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
|
220 return |
1045
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
221 |
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
222 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
|
223 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
|
224 return |
830
9ba1c866bf4c
workaround for n3 file with rules in it
Drew Perttula <drewp@bigasterisk.com>
parents:
829
diff
changeset
|
225 |
9ba1c866bf4c
workaround for n3 file with rules in it
Drew Perttula <drewp@bigasterisk.com>
parents:
829
diff
changeset
|
226 # 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
|
227 # 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
|
228 # 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
|
229 # 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
|
230 # 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
|
231 if '-rules' in inFile: |
830
9ba1c866bf4c
workaround for n3 file with rules in it
Drew Perttula <drewp@bigasterisk.com>
parents:
829
diff
changeset
|
232 return |
904
86c6700d1d63
2013 code ignores 'config.n3' so 2012 code can use that instead
Drew Perttula <drewp@bigasterisk.com>
parents:
899
diff
changeset
|
233 |
86c6700d1d63
2013 code ignores 'config.n3' so 2012 code can use that instead
Drew Perttula <drewp@bigasterisk.com>
parents:
899
diff
changeset
|
234 # 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
|
235 # 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
|
236 # it. |
86c6700d1d63
2013 code ignores 'config.n3' so 2012 code can use that instead
Drew Perttula <drewp@bigasterisk.com>
parents:
899
diff
changeset
|
237 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
|
238 return |
830
9ba1c866bf4c
workaround for n3 file with rules in it
Drew Perttula <drewp@bigasterisk.com>
parents:
829
diff
changeset
|
239 |
1045
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
240 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
|
241 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
|
242 self.patch, self.getSubgraph) |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
243 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
|
244 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
|
245 gf.reread() |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
246 |
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
|
247 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
|
248 """ |
dcab422615ca
working on local->global sub action. creation of new file in rdfdb is still a problem
drewp@bigasterisk.com
parents:
921
diff
changeset
|
249 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
|
250 straightforward to create the new file now |
931 | 251 |
252 this is meant to make the file before we add triples, so we | |
253 wouldn't see the blank file and lose those triples. But it | |
254 didn't work, so there are other measures that make us not lose | |
255 the triples from a new file. Calling this before patching the | |
256 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
|
257 """ |
dcab422615ca
working on local->global sub action. creation of new file in rdfdb is still a problem
drewp@bigasterisk.com
parents:
921
diff
changeset
|
258 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
|
259 |
dcab422615ca
working on local->global sub action. creation of new file in rdfdb is still a problem
drewp@bigasterisk.com
parents:
921
diff
changeset
|
260 if ctx not in self.graphFiles: |
1045
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
261 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
|
262 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
|
263 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
|
264 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
|
265 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
|
266 |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
267 def dirtyFiles(self, ctxs): |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
268 """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
|
269 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
270 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
|
271 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
|
272 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
|
273 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
274 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
|
275 """ |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
276 for ctx in ctxs: |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
277 g = self.getSubgraph(ctx) |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
278 self.graphFiles[ctx].dirty(g) |
796 | 279 |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
280 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
281 class Db(object): |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
282 """ |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
283 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
|
284 """ |
1045
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
285 def __init__(self, dirUriMap): |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
286 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
287 self.clients = [] |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
288 self.graph = ConjunctiveGraph() |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
289 |
1045
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
290 self.watchedFiles = WatchedFiles(dirUriMap, |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
291 self.patch, self.getSubgraph) |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
292 |
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
293 self.summarizeToLog() |
821 | 294 |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
295 def patch(self, p, dueToFileChange=False): |
796 | 296 """ |
297 apply this patch to the master graph then notify everyone about it | |
811 | 298 |
299 dueToFileChange if this is a patch describing an edit we read | |
300 *from* the file (such that we shouldn't write it back to the file) | |
301 | |
302 if p has a senderUpdateUri attribute, we won't send this patch | |
303 back to the sender with that updateUri | |
796 | 304 """ |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
305 ctx = p.getContext() |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
306 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
|
307 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
|
308 |
931 | 309 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
|
310 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
|
311 |
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
|
312 patchQuads(self.graph, p.delQuads, p.addQuads, perfect=True) |
1083
7aca3145c96e
refactor rdfdb patch()
Drew Perttula <drewp@bigasterisk.com>
parents:
1060
diff
changeset
|
313 self._sendPatch(p) |
7aca3145c96e
refactor rdfdb patch()
Drew Perttula <drewp@bigasterisk.com>
parents:
1060
diff
changeset
|
314 if not dueToFileChange: |
7aca3145c96e
refactor rdfdb patch()
Drew Perttula <drewp@bigasterisk.com>
parents:
1060
diff
changeset
|
315 self.watchedFiles.dirtyFiles([ctx]) |
7aca3145c96e
refactor rdfdb patch()
Drew Perttula <drewp@bigasterisk.com>
parents:
1060
diff
changeset
|
316 sendToLiveClients(asJson=p.jsonRepr) |
7aca3145c96e
refactor rdfdb patch()
Drew Perttula <drewp@bigasterisk.com>
parents:
1060
diff
changeset
|
317 |
7aca3145c96e
refactor rdfdb patch()
Drew Perttula <drewp@bigasterisk.com>
parents:
1060
diff
changeset
|
318 def _sendPatch(self, p): |
808
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
319 senderUpdateUri = getattr(p, 'senderUpdateUri', None) |
829
e53e78db7b17
refactor file watching. notice new files and dirs.
Drew Perttula <drewp@bigasterisk.com>
parents:
826
diff
changeset
|
320 |
796 | 321 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
|
322 if c.updateUri == senderUpdateUri: |
a631e075a5bf
KC big rewrites, now multiple KC instances can sync with rdfdb
drewp@bigasterisk.com
parents:
806
diff
changeset
|
323 # 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
|
324 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
|
325 d = c.sendPatch(p) |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
326 d.addErrback(self.clientErrored, c) |
1083
7aca3145c96e
refactor rdfdb patch()
Drew Perttula <drewp@bigasterisk.com>
parents:
1060
diff
changeset
|
327 |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
328 def clientErrored(self, err, c): |
1347
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
329 err.trap(twisted.internet.error.ConnectError, WebsocketDisconnect) |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
330 log.info("%r %r - dropping client", c, err.getErrorMessage()) |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
331 self.clients.remove(c) |
821 | 332 self.sendClientsToAllLivePages() |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
333 |
796 | 334 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
|
335 log.info("contexts in graph (%s total stmts):" % len(self.graph)) |
796 | 336 for c in self.graph.contexts(): |
337 log.info(" %s: %s statements" % | |
338 (c.identifier, len(self.getSubgraph(c.identifier)))) | |
339 | |
340 def getSubgraph(self, uri): | |
811 | 341 """ |
342 this is meant to return a live view of the given subgraph, but | |
343 if i'm still working around an rdflib bug, it might return a | |
344 copy | |
345 | |
346 and it's returning triples, but I think quads would be better | |
347 """ | |
796 | 348 # this is returning an empty Graph :( |
349 #return self.graph.get_context(uri) | |
350 | |
351 g = Graph() | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
352 for s in self.graph.triples(ALLSTMTS, uri): |
796 | 353 g.add(s) |
354 return g | |
821 | 355 |
1344
01f7ece0cb13
refactor rdfdb: Db doesn't make Client objects
Drew Perttula <drewp@bigasterisk.com>
parents:
1343
diff
changeset
|
356 def addClient(self, newClient): |
796 | 357 [self.clients.remove(c) |
1344
01f7ece0cb13
refactor rdfdb: Db doesn't make Client objects
Drew Perttula <drewp@bigasterisk.com>
parents:
1343
diff
changeset
|
358 for c in self.clients if c.updateUri == newClient.updateUri] |
796 | 359 |
1347
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
360 log.info("new client %r" % newClient) |
1345
13f758eda3b4
refactor rdfdb: sendGraphToClient
Drew Perttula <drewp@bigasterisk.com>
parents:
1344
diff
changeset
|
361 sendGraphToClient(self.graph, newClient) |
1344
01f7ece0cb13
refactor rdfdb: Db doesn't make Client objects
Drew Perttula <drewp@bigasterisk.com>
parents:
1343
diff
changeset
|
362 self.clients.append(newClient) |
796 | 363 self.sendClientsToAllLivePages() |
364 | |
365 def sendClientsToAllLivePages(self): | |
797
904913de4599
deletes are now quads. refactor files. named clients. auto client port
drewp@bigasterisk.com
parents:
796
diff
changeset
|
366 sendToLiveClients({"clients":[ |
1347
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
367 dict(updateUri=c.updateUri, label=repr(c)) |
821 | 368 for c in self.clients]}) |
796 | 369 |
370 class GraphResource(PrettyErrorHandler, cyclone.web.RequestHandler): | |
371 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
|
372 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
|
373 format = 'n3' |
741aa0f9be48
very simple con-neg on rdfdb /graph to support text/plain (ntriples)
Drew Perttula <drewp@bigasterisk.com>
parents:
904
diff
changeset
|
374 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
|
375 format = 'nt' |
1032
54027815c6cc
rdfdb can return 'application/n-quads' response
Drew Perttula <drewp@bigasterisk.com>
parents:
1017
diff
changeset
|
376 elif accept == 'application/n-quads': |
54027815c6cc
rdfdb can return 'application/n-quads' response
Drew Perttula <drewp@bigasterisk.com>
parents:
1017
diff
changeset
|
377 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
|
378 self.write(self.settings.db.graph.serialize(format=format)) |
821 | 379 |
796 | 380 class Patches(PrettyErrorHandler, cyclone.web.RequestHandler): |
381 def __init__(self, *args, **kw): | |
382 cyclone.web.RequestHandler.__init__(self, *args, **kw) | |
843
77b5dbcf688d
split syncedgraph into several layers
drewp@bigasterisk.com
parents:
833
diff
changeset
|
383 p = makePatchEndpointPutMethod(self.settings.db.patch) |
796 | 384 self.put = lambda: p(self) |
385 | |
386 def get(self): | |
387 pass | |
388 | |
389 class GraphClients(PrettyErrorHandler, cyclone.web.RequestHandler): | |
390 def get(self): | |
391 pass | |
821 | 392 |
796 | 393 def post(self): |
394 upd = self.get_argument("clientUpdate") | |
395 try: | |
1344
01f7ece0cb13
refactor rdfdb: Db doesn't make Client objects
Drew Perttula <drewp@bigasterisk.com>
parents:
1343
diff
changeset
|
396 self.settings.db.addClient(Client(upd, self.get_argument("label"))) |
796 | 397 except: |
398 import traceback | |
399 traceback.print_exc() | |
400 raise | |
1347
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
401 |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
402 _wsClientSerial = 0 |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
403 class WebsocketClient(cyclone.websocket.WebSocketHandler): |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
404 |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
405 def connectionMade(self, *args, **kwargs): |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
406 global _wsClientSerial |
1348 | 407 connectionId = 'connection-%s' % _wsClientSerial |
1347
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
408 _wsClientSerial += 1 |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
409 |
1348 | 410 self.wsClient = WsClient(connectionId, self.sendMessage) |
411 log.info("new ws client %r", self.wsClient) | |
1347
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
412 self.settings.db.addClient(self.wsClient) |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
413 |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
414 def connectionLost(self, reason): |
1348 | 415 log.info("bye ws client %r", self.wsClient) |
1347
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
416 self.settings.db.clientErrored( |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
417 Failure(WebsocketDisconnect(reason)), self.wsClient) |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
418 |
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
419 def messageReceived(self, message): |
1351
6044e527d795
ping loop (and some lines from the next patch). My setup seemed to be dropping WS after like 30 sec.
Drew Perttula <drewp@bigasterisk.com>
parents:
1350
diff
changeset
|
420 if message == 'PING': |
6044e527d795
ping loop (and some lines from the next patch). My setup seemed to be dropping WS after like 30 sec.
Drew Perttula <drewp@bigasterisk.com>
parents:
1350
diff
changeset
|
421 self.sendMessage('PONG') |
6044e527d795
ping loop (and some lines from the next patch). My setup seemed to be dropping WS after like 30 sec.
Drew Perttula <drewp@bigasterisk.com>
parents:
1350
diff
changeset
|
422 return |
1350
36f58b2aa8ef
browser syncedgraph sends patches back to server
Drew Perttula <drewp@bigasterisk.com>
parents:
1348
diff
changeset
|
423 log.info("got message from %r: %s", self.wsClient, message) |
36f58b2aa8ef
browser syncedgraph sends patches back to server
Drew Perttula <drewp@bigasterisk.com>
parents:
1348
diff
changeset
|
424 p = Patch(jsonRepr=message) |
36f58b2aa8ef
browser syncedgraph sends patches back to server
Drew Perttula <drewp@bigasterisk.com>
parents:
1348
diff
changeset
|
425 p.senderUpdateUri = self.wsClient.updateUri |
36f58b2aa8ef
browser syncedgraph sends patches back to server
Drew Perttula <drewp@bigasterisk.com>
parents:
1348
diff
changeset
|
426 self.settings.db.patch(p) |
796 | 427 |
428 liveClients = set() | |
429 def sendToLiveClients(d=None, asJson=None): | |
430 j = asJson or json.dumps(d) | |
431 for c in liveClients: | |
432 c.sendMessage(j) | |
433 | |
434 class Live(cyclone.websocket.WebSocketHandler): | |
821 | 435 |
796 | 436 def connectionMade(self, *args, **kwargs): |
811 | 437 log.info("websocket opened") |
796 | 438 liveClients.add(self) |
439 self.settings.db.sendClientsToAllLivePages() | |
440 | |
441 def connectionLost(self, reason): | |
811 | 442 log.info("websocket closed") |
796 | 443 liveClients.remove(self) |
444 | |
445 def messageReceived(self, message): | |
446 log.info("got message %s" % message) | |
447 self.sendMessage(message) | |
448 | |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
449 class NoExts(cyclone.web.StaticFileHandler): |
1309 | 450 # .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
|
451 def get(self, path, *args, **kw): |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
452 if path and '.' not in path: |
1309 | 453 path = path + ".html" |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
454 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
|
455 |
796 | 456 if __name__ == "__main__": |
457 logging.basicConfig() | |
458 log = logging.getLogger() | |
459 | |
460 parser = optparse.OptionParser() | |
461 parser.add_option("-v", "--verbose", action="store_true", | |
462 help="logging.DEBUG") | |
463 (options, args) = parser.parse_args() | |
464 | |
465 log.setLevel(logging.DEBUG if options.verbose else logging.INFO) | |
466 | |
1045
c1face79c0e1
fix rdfdb's filename<->uri mapping system
Drew Perttula <drewp@bigasterisk.com>
parents:
1041
diff
changeset
|
467 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
|
468 showconfig.showUri() + '/'}) |
821 | 469 |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
470 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
|
471 twlog.startLogging(sys.stdout) |
821 | 472 |
1225
70f42f9d6e04
simplify homepageConfig. get homepage port from rdf. get rdfdb port from rdf
Drew Perttula <drewp@bigasterisk.com>
parents:
1212
diff
changeset
|
473 reactor.listenTCP(networking.rdfdb.port, cyclone.web.Application(handlers=[ |
796 | 474 (r'/live', Live), |
475 (r'/graph', GraphResource), | |
476 (r'/patches', Patches), | |
477 (r'/graphClients', GraphClients), | |
1347
5c54a1f94050
browser SyncedGraph client connects and receives patches
Drew Perttula <drewp@bigasterisk.com>
parents:
1345
diff
changeset
|
478 (r'/syncedGraph', WebsocketClient), |
796 | 479 |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
480 (r'/(.*)', NoExts, |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
481 {"path" : "light9/rdfdb/web", |
1309 | 482 "default_filename" : "index.html"}), |
814
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
483 |
1ae8e6b287e3
improvements to file watching. outline of how resync will work
drewp@bigasterisk.com
parents:
811
diff
changeset
|
484 ], 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
|
485 log.info("serving on %s" % networking.rdfdb.port) |
1178
a296d842d0da
better cmdline option error
Drew Perttula <drewp@bigasterisk.com>
parents:
1083
diff
changeset
|
486 prof.run(reactor.run, profile=None) |