Mercurial > code > home > repos > homeauto
comparison lib/patchablegraph.py @ 1103:b84e956771fc
sse_collector now kind of gets concurrent requests right
Ignore-this: e1a104d9ae81473b86fc12fbb8ac097b
darcs-hash:1bc1655b532074d97d7b8b7dd65802a9c62b6ff9
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Fri, 19 Aug 2016 22:37:01 -0700 |
parents | ffe6a00c6cef |
children | a94f2a522d41 |
comparison
equal
deleted
inserted
replaced
1102:06a511a96f20 | 1103:b84e956771fc |
---|---|
24 import cyclone.sse | 24 import cyclone.sse |
25 sys.path.append("/my/proj/light9") | 25 sys.path.append("/my/proj/light9") |
26 from light9.rdfdb.grapheditapi import GraphEditApi | 26 from light9.rdfdb.grapheditapi import GraphEditApi |
27 from rdflib import ConjunctiveGraph | 27 from rdflib import ConjunctiveGraph |
28 from light9.rdfdb.rdflibpatch import patchQuads | 28 from light9.rdfdb.rdflibpatch import patchQuads |
29 from light9.rdfdb.patch import Patch | |
29 from rdflib_jsonld.serializer import from_rdf | 30 from rdflib_jsonld.serializer import from_rdf |
31 from rdflib.parser import StringInputSource | |
30 from cycloneerr import PrettyErrorHandler | 32 from cycloneerr import PrettyErrorHandler |
31 | 33 |
32 log = logging.getLogger('patchablegraph') | 34 log = logging.getLogger('patchablegraph') |
33 | 35 |
34 def writeGraphResponse(req, graph, acceptHeader): | 36 def writeGraphResponse(req, graph, acceptHeader): |
49 for s,p,o,c in q: | 51 for s,p,o,c in q: |
50 g.get_context(c).add((s,p,o)) # kind of works with broken rdflib nquad serializer code | 52 g.get_context(c).add((s,p,o)) # kind of works with broken rdflib nquad serializer code |
51 #g.store.add((s,p,o), c) # no effect on nquad output | 53 #g.store.add((s,p,o), c) # no effect on nquad output |
52 return g | 54 return g |
53 | 55 |
54 def patchAsJson(p): | 56 def jsonFromPatch(p): |
55 return json.dumps({'patch': { | 57 return json.dumps({'patch': { |
56 'adds': from_rdf(_graphFromQuads2(p.addQuads)), | 58 'adds': from_rdf(_graphFromQuads2(p.addQuads)), |
57 'deletes': from_rdf(_graphFromQuads2(p.delQuads)), | 59 'deletes': from_rdf(_graphFromQuads2(p.delQuads)), |
58 }}) | 60 }}) |
61 patchAsJson = jsonFromPatch # deprecated name | |
62 | |
63 | |
64 def patchFromJson(j): | |
65 body = json.loads(j)['patch'] | |
66 a = ConjunctiveGraph() | |
67 a.parse(StringInputSource(json.dumps(body['adds'])), format='json-ld') | |
68 d = ConjunctiveGraph() | |
69 d.parse(StringInputSource(json.dumps(body['deletes'])), format='json-ld') | |
70 return Patch(addGraph=a, delGraph=d) | |
59 | 71 |
60 def graphAsJson(g): | 72 def graphAsJson(g): |
61 # This is not the same as g.serialize(format='json-ld')! That | 73 # This is not the same as g.serialize(format='json-ld')! That |
62 # version omits literal datatypes. | 74 # version omits literal datatypes. |
63 return json.dumps(from_rdf(g)) | 75 return json.dumps(from_rdf(g)) |