Mercurial > code > home > repos > light9
comparison bin/curvecalc @ 848:dacbb278d91d
curvecalc port to SyncedGraph. starts up ok, saving is broken
Ignore-this: ef920ab36bc7959f94d9154a9f582c27
author | drewp@bigasterisk.com |
---|---|
date | Tue, 26 Mar 2013 16:50:48 +0000 |
parents | 431ddd043b47 |
children | a59d0f4563cc |
comparison
equal
deleted
inserted
replaced
847:431ddd043b47 | 848:dacbb278d91d |
---|---|
22 import logging | 22 import logging |
23 log = logging.getLogger() | 23 log = logging.getLogger() |
24 | 24 |
25 import run_local | 25 import run_local |
26 from light9 import showconfig, prof, networking | 26 from light9 import showconfig, prof, networking |
27 from light9.rdfdb import clientsession | |
27 from light9.curvecalc.curve import Curveset | 28 from light9.curvecalc.curve import Curveset |
28 from light9.curvecalc import curveview | 29 from light9.curvecalc import curveview |
29 from light9.curvecalc.musicaccess import Music, currentlyPlayingSong | 30 from light9.curvecalc.musicaccess import Music, currentlyPlayingSong |
30 from light9.wavelength import wavelength | 31 from light9.wavelength import wavelength |
31 from light9.namespaces import L9 | 32 from light9.namespaces import L9 |
37 | 38 |
38 class SubtermExists(ValueError): | 39 class SubtermExists(ValueError): |
39 pass | 40 pass |
40 | 41 |
41 class Main(object): | 42 class Main(object): |
42 def __init__(self, graph, opts, song, curveset, subterms, music): | 43 def __init__(self, graph, opts, session, curveset, subterms, music): |
43 self.graph, self.opts, self.song = graph, opts, song | 44 self.graph, self.opts, self.session = graph, opts, session |
44 self.curveset, self.subterms, self.music = curveset, subterms, music | 45 self.curveset, self.subterms, self.music = curveset, subterms, music |
45 self.lastSeenInputTime = 0 | 46 self.lastSeenInputTime = 0 |
46 | 47 |
47 wtree = self.wtree = gtk.Builder() | 48 wtree = self.wtree = gtk.Builder() |
48 wtree.add_from_file("light9/curvecalc/curvecalc.glade") | 49 wtree.add_from_file("light9/curvecalc/curvecalc.glade") |
57 mainwin.show_all() | 58 mainwin.show_all() |
58 | 59 |
59 mainwin.connect("delete-event", lambda *args: reactor.crash()) | 60 mainwin.connect("delete-event", lambda *args: reactor.crash()) |
60 def updateTitle(): | 61 def updateTitle(): |
61 # song will soon be a lookup on this curvecalc session | 62 # song will soon be a lookup on this curvecalc session |
62 mainwin.set_title("curvecalc - %s" % graph.label(song)) | 63 mainwin.set_title("curvecalc - %s" % |
64 graph.label( | |
65 graph.value(session, L9['currentSong']))) | |
63 graph.addHandler(updateTitle) | 66 graph.addHandler(updateTitle) |
64 mainwin.parse_geometry("1x1-0+0") | 67 mainwin.parse_geometry("1x1-0+0") |
65 | 68 |
66 # this is the only one i found that would set the size right, | 69 # this is the only one i found that would set the size right, |
67 # but it's a minimum size, which i don't really want | 70 # but it's a minimum size, which i don't really want |
156 | 159 |
157 def add_subterms_for_song(self): | 160 def add_subterms_for_song(self): |
158 master = self.wtree.get_object("subterms") | 161 master = self.wtree.get_object("subterms") |
159 [master.remove(c) for c in master.get_children()] | 162 [master.remove(c) for c in master.get_children()] |
160 | 163 |
161 for st in self.graph.objects(self.song, L9['subterm']): | 164 song = self.graph.value(self.session, L9['currentSong']) |
162 log.info("song %s has subterm %s", self.song, st) | 165 |
166 for st in self.graph.objects(song, L9['subterm']): | |
167 log.info("song %s has subterm %s", song, st) | |
163 add_one_subterm(self.graph, | 168 add_one_subterm(self.graph, |
164 self.graph.value(st, L9['sub']), | 169 self.graph.value(st, L9['sub']), |
165 self.curveset, | 170 self.curveset, |
166 self.subterms, | 171 self.subterms, |
167 master, | 172 master, |
213 results = dispatcher.send("onPlayPause") | 218 results = dispatcher.send("onPlayPause") |
214 times = [t for listener, t in results if t is not None] | 219 times = [t for listener, t in results if t is not None] |
215 self.music.playOrPause(t=times[0] if times else None) | 220 self.music.playOrPause(t=times[0] if times else None) |
216 | 221 |
217 def onSave(self, *args): | 222 def onSave(self, *args): |
218 savekey(self.song, self.subterms, self.curveset) | 223 with self.graph.currentState() as g: |
224 savekey(g.value(self.session, L9['currentSong']), | |
225 self.subterms, self.curveset) | |
219 | 226 |
220 def makeStatusLines(self, master): | 227 def makeStatusLines(self, master): |
221 """various labels that listen for dispatcher signals""" | 228 """various labels that listen for dispatcher signals""" |
222 for row, (signame, textfilter) in enumerate([ | 229 for row, (signame, textfilter) in enumerate([ |
223 ('input time', lambda t: "%.2fs"%t), | 230 ('input time', lambda t: "%.2fs"%t), |
282 traceback.print_exc() | 289 traceback.print_exc() |
283 if self.opts.reload: | 290 if self.opts.reload: |
284 reactor.callLater(1, self.refreshCurveView) | 291 reactor.callLater(1, self.refreshCurveView) |
285 | 292 |
286 | 293 |
294 class MaxTime(object): | |
295 """ | |
296 looks up the time in seconds for the session's current song | |
297 """ | |
298 def __init__(self, graph, session): | |
299 self.graph, self.session = graph, session | |
300 graph.addHandler(self.update) | |
301 | |
302 def update(self): | |
303 song = self.graph.value(self.session, L9['currentSong']) | |
304 if song is None: | |
305 self.maxtime = 0 | |
306 return | |
307 musicfilename = showconfig.songOnDisk(song) | |
308 self.maxtime = wavelength(musicfilename) | |
309 log.info("new max time %r", self.maxtime) | |
310 dispatcher.send("max time", maxtime=self.maxtime) | |
311 | |
312 def get(self): | |
313 return self.maxtime | |
314 | |
287 def main(): | 315 def main(): |
288 startTime = time.time() | 316 startTime = time.time() |
289 parser = optparse.OptionParser() | 317 parser = optparse.OptionParser() |
290 parser.set_usage("%prog [opts] [songURI]") | 318 parser.set_usage("%prog [opts] [songURI]") |
291 parser.add_option("--sliders", action='store_true', | 319 parser.add_option("--sliders", action='store_true', |
296 help="log at DEBUG") | 324 help="log at DEBUG") |
297 parser.add_option("--reload", action="store_true", | 325 parser.add_option("--reload", action="store_true", |
298 help="live reload of themes and code") | 326 help="live reload of themes and code") |
299 parser.add_option("--startup-only", action='store_true', | 327 parser.add_option("--startup-only", action='store_true', |
300 help="quit after loading everything (for timing tests)") | 328 help="quit after loading everything (for timing tests)") |
329 clientsession.add_option(parser) | |
301 opts, args = parser.parse_args() | 330 opts, args = parser.parse_args() |
302 | 331 |
303 logging.basicConfig(format="%(asctime)s %(levelname)-5s %(name)s %(filename)s:%(lineno)d: %(message)s") | 332 logging.basicConfig(format="%(asctime)s %(levelname)-5s %(name)s %(filename)s:%(lineno)d: %(message)s") |
304 log.setLevel(logging.DEBUG if opts.debug else logging.INFO) | 333 log.setLevel(logging.DEBUG if opts.debug else logging.INFO) |
305 | 334 |
306 log.debug("startup: music %s", time.time() - startTime) | 335 log.debug("startup: music %s", time.time() - startTime) |
336 | |
337 | |
338 session = clientsession.getUri('curvecalc', opts) | |
339 | |
340 music = Music() | |
341 graph = SyncedGraph("curvecalc") | |
342 | |
307 try: | 343 try: |
308 song = URIRef(args[0]) | 344 song = URIRef(args[0]) |
345 graph.patchObject(context=session, | |
346 subject=session, | |
347 predicate=L9['currentSong'], | |
348 newObject=song) | |
309 except IndexError: | 349 except IndexError: |
310 song = currentlyPlayingSong() | 350 pass |
311 | |
312 music = Music() | |
313 graph = SyncedGraph("curvecalc") | |
314 | 351 |
315 curveset = Curveset(sliders=opts.sliders) | 352 curveset = Curveset(sliders=opts.sliders) |
316 subterms = [] | 353 subterms = [] |
317 | 354 |
318 curveset.load(basename=os.path.join( | 355 def curvesetReload(): |
319 showconfig.curvesDir(), | 356 # not sure if this clears right or not yet |
320 showconfig.songFilenameFromURI(song)), | 357 song = graph.value(session, L9['currentSong']) |
321 skipMusic=opts.skip_music) | 358 if song is None: |
322 | 359 return |
360 curveset.load(basename=os.path.join( | |
361 showconfig.curvesDir(), | |
362 showconfig.songFilenameFromURI(song)), | |
363 skipMusic=opts.skip_music) | |
364 graph.addHandler(curvesetReload) | |
365 | |
323 log.debug("startup: output %s", time.time() - startTime) | 366 log.debug("startup: output %s", time.time() - startTime) |
324 out = Output(subterms, music) | 367 out = Output(subterms, music) |
325 | 368 |
326 musicfilename = showconfig.songOnDisk(song) | 369 mt = MaxTime(graph, session) |
327 maxtime = wavelength(musicfilename) | 370 dispatcher.connect(lambda: mt.get(), "get max time", weak=False) |
328 dispatcher.connect(lambda: maxtime, "get max time", weak=False) | 371 |
329 | 372 start = Main(graph, opts, session, curveset, subterms, music) |
330 start = Main(graph, opts, song, curveset, subterms, music) | 373 |
331 | |
332 dispatcher.send("max time", maxtime=maxtime) | |
333 dispatcher.send("show all") | 374 dispatcher.send("show all") |
334 | 375 |
335 if opts.startup_only: | 376 if opts.startup_only: |
336 log.debug("quitting now because of --startup-only") | 377 log.debug("quitting now because of --startup-only") |
337 return | 378 return |
344 results = dispatcher.send("onPlayPause") | 385 results = dispatcher.send("onPlayPause") |
345 times = [t for listener, t in results if t is not None] | 386 times = [t for listener, t in results if t is not None] |
346 if not times: | 387 if not times: |
347 request.setResponseCode(404) | 388 request.setResponseCode(404) |
348 return "not hovering over any time" | 389 return "not hovering over any time" |
349 | 390 with graph.currentState() as g: |
350 return json.dumps({"song":song, "hoverTime" : times[0]}) | 391 song = g.value(session, L9['currentSong']) |
392 return json.dumps({"song": song, "hoverTime" : times[0]}) | |
351 raise NotImplementedError() | 393 raise NotImplementedError() |
352 | 394 |
353 reactor.listenTCP(networking.curveCalc.port, | 395 reactor.listenTCP(networking.curveCalc.port, |
354 server.Site(Hover())) | 396 server.Site(Hover())) |
355 | 397 |