Mercurial > code > home > repos > homeauto
annotate service/store/store.py @ 653:281be4651a2f
graph path now has 'store' in it
Ignore-this: f20acfa66fa56e5b4e076e76cad140f6
author | drewp@bigasterisk.com |
---|---|
date | Wed, 25 Sep 2019 17:29:47 -0700 |
parents | 92cb3245fdd1 |
children | a93fbf0d0daa |
rev | line source |
---|---|
411 | 1 """ |
2 persistent store of rdf statements, meant for stmts from users. | |
3 | |
4 API is not typical rdf: putting statments replaces existing (s,o) | |
5 matches so there can be only one object at a time. Putting the special | |
6 object :unset clears the statement. | |
7 """ | |
8 | |
9 import sys, logging | |
10 from docopt import docopt | |
11 from patchablegraph import PatchableGraph, CycloneGraphHandler, CycloneGraphEventsHandler | |
12 from rdfdb.patch import Patch | |
13 from rdflib import Namespace, URIRef, Literal, Graph | |
14 from rdflib.parser import StringInputSource | |
15 from twisted.internet import reactor | |
16 from twisted.python.filepath import FilePath | |
17 import cyclone.web | |
652
92cb3245fdd1
index page is a data browser. support for -v
drewp@bigasterisk.com
parents:
458
diff
changeset
|
18 from standardservice.logsetup import log, verboseLogging |
411 | 19 |
20 ROOM = Namespace('http://projects.bigasterisk.com/room/') | |
21 | |
22 CTX = ROOM['stored'] | |
23 | |
458
4c68b604c7ec
change port, put to /values, build updates
drewp@bigasterisk.com
parents:
411
diff
changeset
|
24 class ValuesResource(cyclone.web.RequestHandler): |
411 | 25 def put(self): |
26 arg = self.request.arguments | |
27 if arg.get('s') and arg.get('p'): | |
28 self._onQueryStringStatement(arg['s'][-1], arg['p'][-1], self.request.body) | |
29 else: | |
30 self._onGraphBodyStatements(self.request.body, self.request.headers) | |
458
4c68b604c7ec
change port, put to /values, build updates
drewp@bigasterisk.com
parents:
411
diff
changeset
|
31 post = put |
411 | 32 def _onQueryStringStatement(self, s, p, body): |
458
4c68b604c7ec
change port, put to /values, build updates
drewp@bigasterisk.com
parents:
411
diff
changeset
|
33 subj = URIRef(s) |
4c68b604c7ec
change port, put to /values, build updates
drewp@bigasterisk.com
parents:
411
diff
changeset
|
34 pred = URIRef(p) |
411 | 35 turtleLiteral = self.request.body |
36 try: | |
37 obj = Literal(float(turtleLiteral)) | |
38 except ValueError: | |
39 obj = Literal(turtleLiteral) | |
40 self._onStatements([(subj, pred, obj)]) | |
652
92cb3245fdd1
index page is a data browser. support for -v
drewp@bigasterisk.com
parents:
458
diff
changeset
|
41 |
411 | 42 def _onGraphBodyStatements(self, body, headers): |
43 # maybe quads only so we can track who made the input and from what interface? | |
44 # Or your input of triples gets wrapped in a new quad in here? | |
45 g = Graph() | |
46 g.parse(StringInputSource(body), format='nt') | |
47 if not g: | |
48 raise ValueError("expected graph body") | |
49 self._onStatements(list(g.triples((None, None, None)))) | |
652
92cb3245fdd1
index page is a data browser. support for -v
drewp@bigasterisk.com
parents:
458
diff
changeset
|
50 |
411 | 51 def _onStatements(self, stmts): |
52 g = self.settings.masterGraph | |
53 for s, p, o in stmts: | |
54 patch = g.getObjectPatch(CTX, s, p, o) | |
55 if o == ROOM['unset']: | |
56 patch = Patch(delQuads=patch.delQuads) | |
57 g.patch(patch) | |
58 nquads = g.serialize(None, format='nquads') | |
59 self.settings.dbFile.setContent(nquads) | |
652
92cb3245fdd1
index page is a data browser. support for -v
drewp@bigasterisk.com
parents:
458
diff
changeset
|
60 |
411 | 61 if __name__ == '__main__': |
62 arg = docopt(""" | |
63 Usage: store.py [options] | |
64 | |
65 -v Verbose | |
66 """) | |
652
92cb3245fdd1
index page is a data browser. support for -v
drewp@bigasterisk.com
parents:
458
diff
changeset
|
67 verboseLogging(arg['-v']) |
411 | 68 |
69 masterGraph = PatchableGraph() | |
70 dbFile = FilePath('/opt/homeauto_store/db.nquads') | |
71 if dbFile.exists(): | |
72 masterGraph._graph.parse(dbFile.open(), format='nquads') | |
652
92cb3245fdd1
index page is a data browser. support for -v
drewp@bigasterisk.com
parents:
458
diff
changeset
|
73 |
458
4c68b604c7ec
change port, put to /values, build updates
drewp@bigasterisk.com
parents:
411
diff
changeset
|
74 port = 10015 |
411 | 75 reactor.listenTCP(port, cyclone.web.Application([ |
76 (r"/()", cyclone.web.StaticFileHandler, | |
77 {"path": ".", "default_filename": "index.html"}), | |
653 | 78 (r"/store", CycloneGraphHandler, {'masterGraph': masterGraph}), |
79 (r"/store/events", CycloneGraphEventsHandler, | |
411 | 80 {'masterGraph': masterGraph}), |
458
4c68b604c7ec
change port, put to /values, build updates
drewp@bigasterisk.com
parents:
411
diff
changeset
|
81 (r'/values', ValuesResource), |
411 | 82 ], masterGraph=masterGraph, dbFile=dbFile, debug=arg['-v']), |
83 interface='::') | |
84 log.warn('serving on %s', port) | |
85 | |
86 reactor.run() |