diff --git a/bin/curvecalc b/bin/curvecalc --- a/bin/curvecalc +++ b/bin/curvecalc @@ -12,19 +12,9 @@ todo: curveview should preserve more obj from __future__ import division import time,textwrap,math,random,os,optparse, urllib2 import Tix as tk -try: - from dispatch import dispatcher -except ImportError: - import louie as dispatcher +import louie as dispatcher from twisted.internet import reactor,tksupport -import json, restkit -import twisted -from twisted.web.client import Agent -from twisted.internet.protocol import Protocol -from twisted.internet.defer import Deferred -from zope.interface import implements -from twisted.internet.defer import succeed -from twisted.web.iweb import IBodyProducer + from rdflib import Literal, URIRef, RDF, RDFS from rdflib import Graph import rdflib @@ -32,76 +22,17 @@ import logging log = logging.getLogger() import run_local -from light9 import Submaster, dmxclient, networking, showconfig, prof, Patch +from light9 import Submaster, dmxclient, showconfig, prof, Patch from light9.TLUtility import make_attributes_from_args from light9.curvecalc.zoomcontrol import Zoomcontrol from light9.curvecalc.curve import Curveset from light9.curvecalc.curveview import Curvesetview +from light9.curvecalc.musicaccess import Music, currentlyPlayingSong from light9.wavelength import wavelength from light9.uihelpers import toplevelat from light9.namespaces import L9 import light9.Effects -class GatherJson(Protocol): - """calls back the 'finished' deferred with the parsed json data we - received""" - def __init__(self, finished): - self.finished = finished - self.buf = "" - - def dataReceived(self, bytes): - self.buf += bytes - - def connectionLost(self, reason): - self.finished.callback(json.loads(self.buf)) - -class StringProducer(object): - # http://twistedmatrix.com/documents/current/web/howto/client.html - implements(IBodyProducer) - - def __init__(self, body): - self.body = body - self.length = len(body) - - def startProducing(self, consumer): - consumer.write(self.body) - return succeed(None) - - def pauseProducing(self): - pass - - def stopProducing(self): - pass - -class Music: - def __init__(self): - self.recenttime=0 - self.player = Agent(reactor) - dispatcher.connect(self.seekplay_or_pause,"music seek") - self.timePath = networking.musicPlayer.path("time") - - def current_time(self): - """return deferred which gets called with the current - time. This gets called really often""" - d = self.player.request("GET", self.timePath) - d.addCallback(self._timeReturned) - return d - - def _timeReturned(self, response): - done = Deferred() - done.addCallback(self._bodyReceived) - response.deliverBody(GatherJson(done)) - return done - - def _bodyReceived(self, data): - dispatcher.send("input time",val=data['t']) - return data['t'] # pass along to the real receiver - - def seekplay_or_pause(self,t): - d = self.player.request("POST", - networking.musicPlayer.path("seekPlayOrPause"), - bodyProducer=StringProducer(json.dumps({"t" : t}))) - class Expr(object): """singleton, provides functions for use in subterm expressions, e.g. chases""" @@ -499,14 +430,6 @@ def createHelpLines(root): anchor='w') line.pack(side='top',fill='x') -def currentlyPlayingSong(): - """ask the music player what song it's on""" - player = restkit.Resource(networking.musicPlayer.url) - t = json.loads(player.get("time").body_string()) - if t['song'] is None: - raise ValueError("music player is not playing any song") - return URIRef(t['song']) - def main(): startTime = time.time() parser = optparse.OptionParser() diff --git a/light9/curvecalc/musicaccess.py b/light9/curvecalc/musicaccess.py new file mode 100644 --- /dev/null +++ b/light9/curvecalc/musicaccess.py @@ -0,0 +1,82 @@ +import restkit +import json +from louie import dispatcher +from rdflib import URIRef +from light9 import networking +from twisted.internet import reactor +from twisted.web.client import Agent +from twisted.internet.protocol import Protocol +from twisted.internet.defer import Deferred +from zope.interface import implements +from twisted.internet.defer import succeed +from twisted.web.iweb import IBodyProducer + +class GatherJson(Protocol): + """calls back the 'finished' deferred with the parsed json data we + received""" + def __init__(self, finished): + self.finished = finished + self.buf = "" + + def dataReceived(self, bytes): + self.buf += bytes + + def connectionLost(self, reason): + self.finished.callback(json.loads(self.buf)) + +class StringProducer(object): + # http://twistedmatrix.com/documents/current/web/howto/client.html + implements(IBodyProducer) + + def __init__(self, body): + self.body = body + self.length = len(body) + + def startProducing(self, consumer): + consumer.write(self.body) + return succeed(None) + + def pauseProducing(self): + pass + + def stopProducing(self): + pass + +class Music: + def __init__(self): + self.recenttime=0 + self.player = Agent(reactor) + dispatcher.connect(self.seekplay_or_pause,"music seek") + self.timePath = networking.musicPlayer.path("time") + + def current_time(self): + """return deferred which gets called with the current + time. This gets called really often""" + d = self.player.request("GET", self.timePath) + d.addCallback(self._timeReturned) + return d + + def _timeReturned(self, response): + done = Deferred() + done.addCallback(self._bodyReceived) + response.deliverBody(GatherJson(done)) + return done + + def _bodyReceived(self, data): + dispatcher.send("input time",val=data['t']) + return data['t'] # pass along to the real receiver + + def seekplay_or_pause(self,t): + d = self.player.request("POST", + networking.musicPlayer.path("seekPlayOrPause"), + bodyProducer=StringProducer(json.dumps({"t" : t}))) + + + +def currentlyPlayingSong(): + """ask the music player what song it's on""" + player = restkit.Resource(networking.musicPlayer.url) + t = json.loads(player.get("time").body_string()) + if t['song'] is None: + raise ValueError("music player is not playing any song") + return URIRef(t['song'])