Mercurial > code > home > repos > homeauto
annotate lib/patchablegraph.py @ 224:596c645a1fc5
refactor /graph and /graph/events handlers to lib/
Ignore-this: fdd8f3d753f76b32929a6a318314d2b5
author | drewp@bigasterisk.com |
---|---|
date | Sun, 24 Jan 2016 22:53:29 -0800 |
parents | 9236b736bc34 |
children | c1b98006f56e |
rev | line source |
---|---|
224
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
1 """ |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
2 Design: |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
3 |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
4 1. Services each have (named) graphs, which they patch as things |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
5 change. PatchableGraph is an object for holding this graph. |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
6 2. You can http GET that graph, or ... |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
7 3. You can http GET/SSE that graph and hear about modifications to it |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
8 4. The client that got the graph holds and maintains a copy. The |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
9 client may merge together multiple graphs. |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
10 5. Client queries its graph with low-level APIs or client-side sparql. |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
11 6. When the graph changes, the client knows and can update itself at |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
12 low or high granularity. |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
13 """ |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
14 import sys, json |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
15 import cyclone.sse |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
16 sys.path.append("/my/proj/light9") |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
17 from light9.rdfdb.grapheditapi import GraphEditApi |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
18 from rdflib import ConjunctiveGraph |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
19 from light9.rdfdb.rdflibpatch import patchQuads |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
20 from rdflib_jsonld.serializer import from_rdf |
224
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
21 from cycloneerr import PrettyErrorHandler |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
22 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
23 def writeGraphResponse(req, graph, acceptHeader): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
24 if acceptHeader == 'application/nquads': |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
25 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
|
26 graph.serialize(req, format='nquads') |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
27 elif acceptHeader == 'application/ld+json': |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
28 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
|
29 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
|
30 else: |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
31 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
|
32 graph.serialize(req, format='trig') |
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 # forked from /my/proj/light9/light9/rdfdb/rdflibpatch.py |
224
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
35 def _graphFromQuads2(q): |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
36 g = ConjunctiveGraph() |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
37 #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
|
38 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
|
39 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
|
40 #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
|
41 return g |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
42 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
43 def patchAsJson(p): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
44 return json.dumps({'patch': { |
224
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
45 'adds': from_rdf(_graphFromQuads2(p.addQuads)), |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
46 'deletes': from_rdf(_graphFromQuads2(p.delQuads)), |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
47 }}) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
48 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
49 class PatchableGraph(GraphEditApi): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
50 """ |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
51 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
|
52 updates to all current listeners. |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
53 """ |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
54 def __init__(self): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
55 self._graph = ConjunctiveGraph() |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
56 self._observers = [] |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
57 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
58 def serialize(self, to, **kw): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
59 return self._graph.serialize(to, **kw) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
60 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
61 def patch(self, p): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
62 if p.isNoop(): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
63 return |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
64 patchQuads(self._graph, |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
65 deleteQuads=p.delQuads, |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
66 addQuads=p.addQuads, |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
67 perfect=False) # true? |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
68 for ob in self._observers: |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
69 ob(patchAsJson(p)) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
70 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
71 def addObserver(self, onPatch): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
72 self._observers.append(onPatch) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
73 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
74 def removeObserver(self, onPatch): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
75 try: |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
76 self._observers.remove(onPatch) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
77 except ValueError: |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
78 pass |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
79 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
80 |
224
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
81 class CycloneGraphHandler(PrettyErrorHandler, cyclone.web.RequestHandler): |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
82 def initialize(self, masterGraph): |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
83 self.masterGraph = masterGraph |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
84 |
224
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
85 def get(self): |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
86 writeGraphResponse(self, self.masterGraph, |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
87 self.request.headers.get('accept')) |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
88 |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
89 class CycloneGraphEventsHandler(cyclone.sse.SSEHandler): |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
90 """ |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
91 One session with one client. |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
92 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
93 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
|
94 in sync with ours. |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
95 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
96 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
|
97 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
|
98 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
|
99 """ |
224
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
100 def __init__(self, application, request, masterGraph): |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
101 cyclone.sse.SSEHandler.__init__(self, application, request) |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
102 self.masterGraph = masterGraph |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
103 |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
104 def bind(self): |
224
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
105 self.sendEvent( |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
106 message=self.masterGraph.serialize(None, format='json-ld', |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
107 indent=None), |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
108 event='fullGraph') |
596c645a1fc5
refactor /graph and /graph/events handlers to lib/
drewp@bigasterisk.com
parents:
223
diff
changeset
|
109 self.masterGraph.addObserver(self.onPatch) |
223
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
110 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
111 def onPatch(self, patchJson): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
112 self.sendEvent(message=patchJson, event='patch') |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
113 |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
114 def unbind(self): |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
115 self.settings.masterGraph.removeObserver(self.onPatch) |
9236b736bc34
add new jsonld/SSE support to environment service as a test
drewp@bigasterisk.com
parents:
diff
changeset
|
116 |