Mercurial > code > home > repos > light9
comparison bin/subserver @ 942:dd896321faee
subserver can get a snapshot from vidref and display it on the sub
Ignore-this: 9ea0a172869922d22d8c5cf6ee4bf3da
author | drewp@bigasterisk.com |
---|---|
date | Thu, 13 Jun 2013 01:31:16 +0000 |
parents | 8b95d2865643 |
children | 891f2d75c686 |
comparison
equal
deleted
inserted
replaced
941:1d9547f90737 | 942:dd896321faee |
---|---|
2 """ | 2 """ |
3 live web display of all existing subs with pictures, mainly for | 3 live web display of all existing subs with pictures, mainly for |
4 dragging them into CC | 4 dragging them into CC |
5 """ | 5 """ |
6 from run_local import log | 6 from run_local import log |
7 from twisted.internet import reactor | 7 import sys, optparse, logging, json, subprocess, datetime |
8 import twisted.internet.error | 8 from dateutil.tz import tzlocal |
9 import sys, optparse, logging, json, os, subprocess | 9 from twisted.internet import reactor, defer |
10 import cyclone.web, cyclone.httpclient, cyclone.websocket | 10 import cyclone.web, cyclone.httpclient, cyclone.websocket |
11 from rdflib import RDF, URIRef, Literal | |
12 import pyjade.utils | |
11 from light9.rdfdb.syncedgraph import SyncedGraph | 13 from light9.rdfdb.syncedgraph import SyncedGraph |
12 from rdflib import RDF | 14 from light9.rdfdb.patch import Patch |
13 from light9.namespaces import L9 | 15 from light9.namespaces import L9, DCTERMS |
14 import pyjade.utils | 16 from light9 import networking, showconfig |
15 | 17 |
16 try: | 18 try: |
17 import sys | |
18 sys.path.append("../homeauto/lib") | 19 sys.path.append("../homeauto/lib") |
19 sys.path.append("/home/drewp/projects/homeauto/lib") | 20 sys.path.append("/home/drewp/projects/homeauto/lib") |
20 from cycloneerr import PrettyErrorHandler | 21 from cycloneerr import PrettyErrorHandler |
21 except ImportError: | 22 except ImportError: |
22 class PrettyErrorHandler(object): | 23 class PrettyErrorHandler(object): |
65 self.write(html) | 66 self.write(html) |
66 | 67 |
67 def responseStaticCoffee(self, src): | 68 def responseStaticCoffee(self, src): |
68 self.write(subprocess.check_output([ | 69 self.write(subprocess.check_output([ |
69 '/usr/bin/coffee', '--compile', '--print', src])) | 70 '/usr/bin/coffee', '--compile', '--print', src])) |
71 | |
72 class Snapshot(PrettyErrorHandler, cyclone.web.RequestHandler): | |
73 @defer.inlineCallbacks | |
74 def post(self): | |
75 about = URIRef(self.get_argument("about")) | |
76 response = yield cyclone.httpclient.fetch(networking.vidref.path("snapshot"), method="POST") | |
77 | |
78 snapUri = URIRef(json.loads(response.body)['snapshot']) | |
79 # vidref could write about when it was taken, etc. would it be | |
80 # better for us to tell vidref where to attach the result in | |
81 # the graph, and then it doesn't even have to return anything? | |
82 | |
83 ctx = showconfig.showUri() + "/snapshots" | |
84 | |
85 self.settings.graph.patch(Patch(addQuads=[ | |
86 (about, L9['image'], snapUri, ctx), | |
87 (snapUri, DCTERMS['created'], | |
88 Literal(datetime.datetime.now(tzlocal())), ctx), | |
89 ])) | |
90 | |
91 self.write(json.dumps({'snapshot': snapUri})) | |
92 | |
93 def newestImage(subject): | |
94 newest = (None, None) | |
95 for img in graph.objects(subject, L9['image']): | |
96 created = graph.value(img, DCTERMS['created']) | |
97 if created > newest[0]: | |
98 newest = (created, img) | |
99 return newest[1] | |
70 | 100 |
71 if __name__ == "__main__": | 101 if __name__ == "__main__": |
72 parser = optparse.OptionParser() | 102 parser = optparse.OptionParser() |
73 parser.add_option("-v", "--verbose", action="store_true", | 103 parser.add_option("-v", "--verbose", action="store_true", |
74 help="logging.DEBUG") | 104 help="logging.DEBUG") |
78 | 108 |
79 graph = SyncedGraph("subserver") | 109 graph = SyncedGraph("subserver") |
80 | 110 |
81 d = {} | 111 d = {} |
82 def updateSubs(): | 112 def updateSubs(): |
83 | |
84 subs = [] | 113 subs = [] |
85 for sub in graph.subjects(RDF.type, L9['Submaster']): | 114 for sub in sorted(graph.subjects(RDF.type, L9['Submaster'])): |
86 rec = {'uri' : sub} | 115 rec = {'uri' : sub} |
87 rec['isLocal'] = graph.contains((sub, RDF.type, | 116 rec['isLocal'] = graph.contains((sub, RDF.type, |
88 L9['LocalSubmaster'])) | 117 L9['LocalSubmaster'])) |
89 rec['label'] = graph.label(sub) | 118 rec['label'] = graph.label(sub) |
119 rec['img'] = newestImage(sub) | |
90 subs.append(rec) | 120 subs.append(rec) |
91 | 121 |
92 d.clear() | 122 d.clear() |
93 d.update({'subs': subs}) | 123 d.update({'subs': subs}) |
94 sendToLiveClients(d=d) | 124 sendToLiveClients(d=d) |
99 | 129 |
100 | 130 |
101 port = 8052 | 131 port = 8052 |
102 reactor.listenTCP(port, cyclone.web.Application(handlers=[ | 132 reactor.listenTCP(port, cyclone.web.Application(handlers=[ |
103 (r'/live', Live), | 133 (r'/live', Live), |
134 (r'/snapshot', Snapshot), | |
104 (r'/(.*)', Static, | 135 (r'/(.*)', Static, |
105 {"path" : "light9/subserver", | 136 {"path" : "light9/subserver", |
106 "default_filename" : "index.jade"}), | 137 "default_filename" : "index.jade"}), |
107 ], debug=True, graph=graph, onNewClient=onNewClient)) | 138 ], debug=True, graph=graph, onNewClient=onNewClient)) |
108 log.info("serving on %s" % port) | 139 log.info("serving on %s" % port) |