Mercurial > code > home > repos > homeauto
annotate lib/patchablegraph.py @ 223:9236b736bc34
add new jsonld/SSE support to environment service as a test
Ignore-this: ae671e71966dbbb9d1f97e3596802d3d
author | drewp@bigasterisk.com |
---|---|
date | Sun, 24 Jan 2016 07:12:25 -0800 |
parents | |
children | 596c645a1fc5 |
rev | line source |
---|---|
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
1 import sys, json |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
2 import cyclone.sse |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
3 sys.path.append("/my/proj/light9") |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
4 from light9.rdfdb.grapheditapi import GraphEditApi |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
5 from rdflib import ConjunctiveGraph |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
6 from light9.rdfdb.rdflibpatch import patchQuads |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
7 from rdflib_jsonld.serializer import from_rdf |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
8 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
9 def writeGraphResponse(req, graph, acceptHeader): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
10 if acceptHeader == 'application/nquads': |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
11 req.set_header('Content-type', 'application/nquads') |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
12 graph.serialize(req, format='nquads') |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
13 elif acceptHeader == 'application/ld+json': |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
14 req.set_header('Content-type', 'application/ld+json') |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
15 graph.serialize(req, format='json-ld', indent=2) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
16 else: |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
17 req.set_header('Content-type', 'application/x-trig') |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
18 graph.serialize(req, format='trig') |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
19 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
20 # forked from /my/proj/light9/light9/rdfdb/rdflibpatch.py |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
21 def graphFromQuads2(q): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
22 g = ConjunctiveGraph() |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
23 #g.addN(q) # no effect on nquad output |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
24 for s,p,o,c in q: |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
25 g.get_context(c).add((s,p,o)) # kind of works with broken rdflib nquad serializer code |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
26 #g.store.add((s,p,o), c) # no effect on nquad output |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
27 return g |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
28 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
29 def patchAsJson(p): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
30 return json.dumps({'patch': { |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
31 'adds': from_rdf(graphFromQuads2(p.addQuads)), |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
32 'deletes': from_rdf(graphFromQuads2(p.delQuads)), |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
33 }}) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
34 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
35 class PatchableGraph(GraphEditApi): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
36 """ |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
37 Master graph that you modify with self.patch, and we get the |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
38 updates to all current listeners. |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
39 """ |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
40 def __init__(self): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
41 self._graph = ConjunctiveGraph() |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
42 self._observers = [] |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
43 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
44 def serialize(self, to, **kw): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
45 return self._graph.serialize(to, **kw) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
46 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
47 def patch(self, p): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
48 if p.isNoop(): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
49 return |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
50 patchQuads(self._graph, |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
51 deleteQuads=p.delQuads, |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
52 addQuads=p.addQuads, |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
53 perfect=False) # true? |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
54 for ob in self._observers: |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
55 ob(patchAsJson(p)) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
56 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
57 def addObserver(self, onPatch): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
58 self._observers.append(onPatch) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
59 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
60 def removeObserver(self, onPatch): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
61 try: |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
62 self._observers.remove(onPatch) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
63 except ValueError: |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
64 pass |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
65 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
66 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
67 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
68 class GraphEventsHandler(cyclone.sse.SSEHandler): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
69 """ |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
70 One session with one client. |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
71 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
72 returns current graph plus future patches to keep remote version |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
73 in sync with ours. |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
74 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
75 intsead of turning off buffering all over, it may work for this |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
76 response to send 'x-accel-buffering: no', per |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
77 http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffering |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
78 """ |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
79 def bind(self): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
80 mg = self.settings.masterGraph |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
81 # todo: needs to be on one line, or else fix cyclone to stripe headers |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
82 self.sendEvent(message=mg.serialize(None, format='json-ld', indent=None), event='fullGraph') |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
83 mg.addObserver(self.onPatch) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
84 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
85 def onPatch(self, patchJson): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
86 self.sendEvent(message=patchJson, event='patch') |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
87 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
88 def unbind(self): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
89 self.settings.masterGraph.removeObserver(self.onPatch) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
90 |