Mercurial > code > home > repos > light9
annotate bin/effecteval @ 1019:5939fce98fad
notes for next step
Ignore-this: 302635a526c833e818eaff3e566d07e7
author | drewp@bigasterisk.com |
---|---|
date | Mon, 26 May 2014 07:45:06 +0000 |
parents | e28a443bd153 |
children | a38414bd3929 |
rev | line source |
---|---|
1018
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
1 #!bin/python |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
2 from run_local import log |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
3 from twisted.internet import reactor |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
4 import cyclone.web, cyclone.websocket |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
5 import sys, optparse, logging, subprocess |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
6 from rdflib import URIRef, RDF |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
7 |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
8 sys.path.append(".") |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
9 from light9 import networking, showconfig |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
10 from light9.rdfdb.syncedgraph import SyncedGraph |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
11 from light9.namespaces import L9, DCTERMS |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
12 |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
13 sys.path.append("../homeauto/lib") |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
14 sys.path.append("/home/drewp/projects/homeauto/lib") |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
15 from cycloneerr import PrettyErrorHandler |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
16 |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
17 class EffectEdit(cyclone.web.RequestHandler): |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
18 def get(self): |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
19 self.write(open("light9/effecteval/effect.html").read()) |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
20 |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
21 class EffectData(cyclone.websocket.WebSocketHandler): |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
22 """ |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
23 stays alive for the life of the effect page |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
24 """ |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
25 def connectionMade(self, *args, **kwargs): |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
26 log.info("websocket opened") |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
27 self.uri = URIRef(self.get_argument('uri')) |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
28 self.sendMessage({'hello': repr(self)}) |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
29 |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
30 self.graph = self.settings.graph |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
31 self.graph.addHandler(self.updateClient) |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
32 |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
33 def updateClient(self): |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
34 # todo: if client has dropped, abort and don't get any more |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
35 # graph updates |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
36 self.sendMessage({'code': self.graph.value(self.uri, L9['code'])}) |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
37 |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
38 def connectionLost(self, reason): |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
39 log.info("websocket closed") |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
40 |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
41 def messageReceived(self, message): |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
42 log.info("got message %s" % message) |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
43 # write a patch back to the graph |
1019 | 44 |
45 def effectDmxDict(graph, effect): | |
46 | |
47 | |
48 class EffectEval(PrettyErrorHandler, cyclone.web.RequestHandler): | |
49 def get(self): | |
50 uri = URIRef(self.get_argument('uri')) | |
51 # return dmx dict for that effect | |
52 self.write(effectDmxDict(self.settings.graph, uri)) | |
53 | |
54 class SongEffectsEval(PrettyErrorHandler, cyclone.web.RequestHandler): | |
55 def get(self): | |
56 song = URIRef(self.get_argument('song')) | |
57 effects = effectsForSong(self.settings.graph, song) | |
58 self.write(maxDict(effectDmxDict(e) for e in effects)) | |
59 # return dmx dict for all effects in the song, already combined | |
1018
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
60 |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
61 class App(object): |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
62 def __init__(self, show): |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
63 self.show = show |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
64 self.graph = SyncedGraph("effectEval") |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
65 SFH = cyclone.web.StaticFileHandler |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
66 self.cycloneApp = cyclone.web.Application(handlers=[ |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
67 (r'/()', SFH, |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
68 {'path': 'light9/effecteval', 'default_filename': 'index.html'}), |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
69 (r'/effect', EffectEdit), |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
70 (r'/(websocket\.js)', SFH, {'path': 'light9/rdfdb/web/'}), |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
71 (r'/(knockout-2\.2\.1\.js)', SFH, {'path': 'light9/subserver/'}), |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
72 (r'/effect\.js', StaticCoffee, {'src': 'light9/effecteval/effect.coffee'}), |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
73 (r'/effectData', EffectData), |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
74 (r'/static/(.*)', SFH, {'path': 'static/'}), |
1019 | 75 (r'/effect/eval', EffectEval), |
76 (r'/songEffects/eval', SongEffectsEval), | |
1018
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
77 ], debug=True, graph=self.graph) |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
78 #graph.initiallySynced.addCallback( |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
79 |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
80 # see bin/subserver |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
81 |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
82 class StaticCoffee(PrettyErrorHandler, cyclone.web.RequestHandler): |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
83 def initialize(self, src): |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
84 super(StaticCoffee, self).initialize() |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
85 self.src = src |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
86 def get(self): |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
87 self.set_header('Content-Type', 'application/javascript') |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
88 self.write(subprocess.check_output([ |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
89 '/usr/bin/coffee', '--compile', '--print', self.src])) |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
90 |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
91 |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
92 if __name__ == "__main__": |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
93 parser = optparse.OptionParser() |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
94 parser.add_option('--show', |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
95 help='show URI, like http://light9.bigasterisk.com/show/dance2008', |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
96 default=showconfig.showUri()) |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
97 parser.add_option("-v", "--verbose", action="store_true", |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
98 help="logging.DEBUG") |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
99 parser.add_option("--twistedlog", action="store_true", |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
100 help="twisted logging") |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
101 (options, args) = parser.parse_args() |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
102 |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
103 log.setLevel(logging.DEBUG if options.verbose else logging.INFO) |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
104 |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
105 if not options.show: |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
106 raise ValueError("missing --show http://...") |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
107 |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
108 app = App(URIRef(options.show)) |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
109 if options.twistedlog: |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
110 from twisted.python import log as twlog |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
111 twlog.startLogging(sys.stderr) |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
112 reactor.listenTCP(networking.effectEval.port, app.cycloneApp) |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
113 log.info("listening on %s" % networking.effectEval.port) |
e28a443bd153
initial effecteval that can propagate changes from the graph to a web page
drewp@bigasterisk.com
parents:
diff
changeset
|
114 reactor.run() |