Mercurial > code > home > repos > homeauto
comparison lib/patchablegraph/patchablegraph.py @ 708:5b6022beb388
try to detect browser even better, instead of failing the whole request
Ignore-this: 2e228dcc5424533a30a796168f9a0d8
author | drewp@bigasterisk.com |
---|---|
date | Mon, 03 Feb 2020 22:07:20 -0800 |
parents | b41247c7b080 |
children | e404420a2343 |
comparison
equal
deleted
inserted
replaced
707:41af3ebccdf9 | 708:5b6022beb388 |
---|---|
33 from rdfdb.patch import Patch | 33 from rdfdb.patch import Patch |
34 from rdfdb.rdflibpatch import patchQuads, inGraph | 34 from rdfdb.rdflibpatch import patchQuads, inGraph |
35 | 35 |
36 log = logging.getLogger('patchablegraph') | 36 log = logging.getLogger('patchablegraph') |
37 | 37 |
38 def writeGraphResponse(req, graph, acceptHeader): | 38 def _writeGraphForBrowser(req, graph): |
39 # We think this is a browser, so respond with a live graph view | |
40 # (todo) | |
41 req.set_header('Content-type', 'text/plain') | |
42 lines = graph.serialize(None, format='nquads').splitlines() | |
43 lines.sort() | |
44 req.write(b'\n'.join(lines)) | |
45 | |
46 | |
47 def _writeGraphResponse(req, graph, acceptHeader: str): | |
39 if acceptHeader == 'application/nquads': | 48 if acceptHeader == 'application/nquads': |
40 req.set_header('Content-type', 'application/nquads') | 49 req.set_header('Content-type', 'application/nquads') |
41 graph.serialize(req, format='nquads') | 50 graph.serialize(req, format='nquads') |
42 elif acceptHeader == 'application/ld+json': | 51 elif acceptHeader == 'application/ld+json': |
43 req.set_header('Content-type', 'application/ld+json') | 52 req.set_header('Content-type', 'application/ld+json') |
44 graph.serialize(req, format='json-ld', indent=2) | 53 graph.serialize(req, format='json-ld', indent=2) |
45 else: | 54 else: |
46 print(f'acceptHeader {acceptHeader}') | |
47 if acceptHeader.startswith('text/html'): | 55 if acceptHeader.startswith('text/html'): |
48 # browser; should arrange to pick live view | 56 _writeGraphForBrowser(req, graph) |
49 req.set_header('Content-type', 'text/plain') | |
50 lines = graph.serialize(None, format='nquads').splitlines() | |
51 lines.sort() | |
52 req.write(b'\n'.join(lines)) | |
53 return | 57 return |
54 | |
55 req.set_header('Content-type', 'application/x-trig') | 58 req.set_header('Content-type', 'application/x-trig') |
56 graph.serialize(req, format='trig') | 59 graph.serialize(req, format='trig') |
57 | 60 |
58 # forked from /my/proj/light9/light9/rdfdb/rdflibpatch.py | 61 # forked from /my/proj/light9/light9/rdfdb/rdflibpatch.py |
59 def _graphFromQuads2(q): | 62 def _graphFromQuads2(q): |
147 def initialize(self, masterGraph): | 150 def initialize(self, masterGraph): |
148 self.masterGraph = masterGraph | 151 self.masterGraph = masterGraph |
149 | 152 |
150 def get(self): | 153 def get(self): |
151 with self.masterGraph._sendSimpleGraph.time(): | 154 with self.masterGraph._sendSimpleGraph.time(): |
152 writeGraphResponse(self, self.masterGraph, | 155 _writeGraphResponse(self, self.masterGraph, |
153 self.request.headers.get('accept')) | 156 self.request.headers.get('accept', '')) |
154 | 157 |
155 | 158 |
156 class CycloneGraphEventsHandler(cyclone.sse.SSEHandler): | 159 class CycloneGraphEventsHandler(cyclone.sse.SSEHandler): |
157 """ | 160 """ |
158 One session with one client. | 161 One session with one client. |