Mercurial > code > home > repos > light9
annotate bin/subserver @ 2019:a6045f11198d
don't show a misleading TL cursor when we're viewing a song that's not the playing one
Ignore-this: 33928b4331aa09f2191b6d45e4e163a
author | drewp@bigasterisk.com |
---|---|
date | Mon, 10 Jun 2019 23:22:06 +0000 |
parents | 3c523c71da29 |
children |
rev | line source |
---|---|
919
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
1 #!bin/python |
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
2 """ |
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
3 live web display of all existing subs with pictures, mainly for |
1530 | 4 dragging them into CC or Timeline |
919
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
5 """ |
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
6 from run_local import log |
1866
3c523c71da29
pyflakes cleanups and some refactors
Drew Perttula <drewp@bigasterisk.com>
parents:
1861
diff
changeset
|
7 import optparse, logging, json, subprocess, datetime |
942
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
8 from dateutil.tz import tzlocal |
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
9 from twisted.internet import reactor, defer |
919
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
10 import cyclone.web, cyclone.httpclient, cyclone.websocket |
1866
3c523c71da29
pyflakes cleanups and some refactors
Drew Perttula <drewp@bigasterisk.com>
parents:
1861
diff
changeset
|
11 from rdflib import URIRef, Literal |
942
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
12 import pyjade.utils |
1692 | 13 from rdfdb.syncedgraph import SyncedGraph |
14 from rdfdb.patch import Patch | |
942
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
15 from light9.namespaces import L9, DCTERMS |
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
16 from light9 import networking, showconfig |
222 | 17 |
1861 | 18 from cycloneerr import PrettyErrorHandler |
1858 | 19 |
20 | |
919
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
21 class Static(PrettyErrorHandler, cyclone.web.StaticFileHandler): |
1858 | 22 |
919
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
23 def get(self, path, *args, **kw): |
1104
448fe9f81501
start to convert subserver to offer the new effects
Drew Perttula <drewp@bigasterisk.com>
parents:
1060
diff
changeset
|
24 if path in ['', 'effects']: |
953
891f2d75c686
subserver now also serves some premade subterms with the defined chases
drewp@bigasterisk.com
parents:
942
diff
changeset
|
25 return self.respondStaticJade("light9/subserver/%s.jade" % |
891f2d75c686
subserver now also serves some premade subterms with the defined chases
drewp@bigasterisk.com
parents:
942
diff
changeset
|
26 (path or 'index')) |
1858 | 27 |
1041
a4632a7b2e17
upgrade knockout and jquery, simplify the static/ dirs for all web services
Drew Perttula <drewp@bigasterisk.com>
parents:
976
diff
changeset
|
28 if path.endswith(".js"): |
953
891f2d75c686
subserver now also serves some premade subterms with the defined chases
drewp@bigasterisk.com
parents:
942
diff
changeset
|
29 return self.responseStaticCoffee( |
891f2d75c686
subserver now also serves some premade subterms with the defined chases
drewp@bigasterisk.com
parents:
942
diff
changeset
|
30 'light9/subserver/%s' % |
1858 | 31 path.replace(".js", ".coffee")) # potential security hole |
953
891f2d75c686
subserver now also serves some premade subterms with the defined chases
drewp@bigasterisk.com
parents:
942
diff
changeset
|
32 |
919
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
33 cyclone.web.StaticFileHandler.get(self, path, *args, **kw) |
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
34 |
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
35 def respondStaticJade(self, src): |
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
36 html = pyjade.utils.process(open(src).read()) |
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
37 self.write(html) |
222 | 38 |
919
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
39 def responseStaticCoffee(self, src): |
1858 | 40 self.write( |
41 subprocess.check_output( | |
42 ['/usr/bin/coffee', '--compile', '--print', src])) | |
43 | |
942
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
44 |
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
45 class Snapshot(PrettyErrorHandler, cyclone.web.RequestHandler): |
1858 | 46 |
942
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
47 @defer.inlineCallbacks |
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
48 def post(self): |
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
49 about = URIRef(self.get_argument("about")) |
1858 | 50 response = yield cyclone.httpclient.fetch( |
51 networking.vidref.path("snapshot"), method="POST", timeout=1) | |
942
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
52 |
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
53 snapUri = URIRef(json.loads(response.body)['snapshot']) |
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
54 # vidref could write about when it was taken, etc. would it be |
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
55 # better for us to tell vidref where to attach the result in |
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
56 # the graph, and then it doesn't even have to return anything? |
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
57 |
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
58 ctx = showconfig.showUri() + "/snapshots" |
1858 | 59 |
60 self.settings.graph.patch( | |
61 Patch(addQuads=[ | |
62 (about, L9['image'], snapUri, ctx), | |
63 (snapUri, DCTERMS['created'], | |
64 Literal(datetime.datetime.now(tzlocal())), ctx), | |
942
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
65 ])) |
1858 | 66 |
942
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
67 self.write(json.dumps({'snapshot': snapUri})) |
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
68 |
1858 | 69 |
942
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
70 def newestImage(subject): |
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
71 newest = (None, None) |
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
72 for img in graph.objects(subject, L9['image']): |
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
73 created = graph.value(img, DCTERMS['created']) |
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
74 if created > newest[0]: |
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
75 newest = (created, img) |
dd896321faee
subserver can get a snapshot from vidref and display it on the sub
drewp@bigasterisk.com
parents:
923
diff
changeset
|
76 return newest[1] |
1858 | 77 |
78 | |
919
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
79 if __name__ == "__main__": |
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
80 parser = optparse.OptionParser() |
1858 | 81 parser.add_option("-v", |
82 "--verbose", | |
83 action="store_true", | |
919
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
84 help="logging.DEBUG") |
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
85 (options, args) = parser.parse_args() |
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
86 |
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
87 log.setLevel(logging.DEBUG if options.verbose else logging.INFO) |
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
88 |
1217
e703b3434dbd
websocket and web cleanup
Drew Perttula <drewp@bigasterisk.com>
parents:
1212
diff
changeset
|
89 graph = SyncedGraph(networking.rdfdb.url, "subServer") |
919
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
90 |
1217
e703b3434dbd
websocket and web cleanup
Drew Perttula <drewp@bigasterisk.com>
parents:
1212
diff
changeset
|
91 port = networking.subServer.port |
1858 | 92 reactor.listenTCP( |
93 port, | |
94 cyclone.web.Application(handlers=[ | |
95 (r'/snapshot', Snapshot), | |
96 (r'/(.*)', Static, { | |
97 "path": "light9/subserver", | |
98 "default_filename": "index.jade" | |
99 }), | |
100 ], | |
101 debug=True, | |
102 graph=graph)) | |
919
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
103 log.info("serving on %s" % port) |
33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
drewp@bigasterisk.com
parents:
310
diff
changeset
|
104 reactor.run() |