changeset 689:03453848ed4c

refactor musicaccess Ignore-this: e9cb1810285e3730c9d7381ab0229075
author drewp@bigasterisk.com
date Tue, 05 Jun 2012 22:56:41 +0000
parents 97b028ed0e47
children 847edbfe65c8
files bin/curvecalc light9/curvecalc/musicaccess.py
diffstat 2 files changed, 86 insertions(+), 81 deletions(-) [+]
line wrap: on
line diff
--- a/bin/curvecalc	Tue Jun 05 21:48:42 2012 +0000
+++ b/bin/curvecalc	Tue Jun 05 22:56:41 2012 +0000
@@ -12,19 +12,9 @@
 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 @@
 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 @@
                         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()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/light9/curvecalc/musicaccess.py	Tue Jun 05 22:56:41 2012 +0000
@@ -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'])