changeset 304:5f9cf6174e62

ascoltami now uses rdf for config
author Drew Perttula <drewp@bigasterisk.com>
date Sun, 18 Mar 2007 23:44:54 +0000
parents d434a74fc068
children 4072d93f02c5
files bin/ascoltami light9/namespaces.py light9/showconfig.py
diffstat 3 files changed, 72 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/bin/ascoltami	Tue Mar 21 04:50:35 2006 +0000
+++ b/bin/ascoltami	Sun Mar 18 23:44:54 2007 +0000
@@ -29,6 +29,7 @@
 
 import run_local
 from light9 import networking, showconfig, wavelength
+from light9.namespaces import L9, MUS
 
 from pympd import Mpd
 
@@ -67,9 +68,10 @@
     def xmlrpc_songlength(self):
         """song length, in seconds"""
         return float(self.player.total_time.get())
-    def xmlrpc_songname(self):
-        """song filename, or None"""
-        return self.player.filename_var.get() or "No song"
+
+    def xmlrpc_song_uri(self):
+        """song URI, or None"""
+        return self.player.song_uri.encode('utf8') or "No song"
 
 class Player:
     """semprini-style access to mpd. in here is where we add the padding"""
@@ -85,6 +87,7 @@
         self.current_time = tk.DoubleVar()
         self.total_time = tk.DoubleVar()
         self.filename_var = tk.StringVar()
+        self.song_uri = None
 
         self.pre_post_names = showconfig.prePostSong()
 
@@ -150,24 +153,25 @@
         finally:
             reactor.callLater(.05, self.pollStatus)
 
-    def set_total_time(self, song_path):
+    def set_total_time(self, song):
         # currently only good for .wav
-        p = os.path.join(showconfig.musicDir(), song_path)
+        p = showconfig.songOnDisk(song)
         self.total_time.set(wavelength.wavelength(p))
 
-    def play(self, song_path=None):
-        if song_path is None:
+    def play(self, song=None):
+        if song is None:
             self.mpd.play()
             return
     
         self.autopausedthissong = False
         self.mpd.clear()
-        self.mpd.add(showconfig.songInMpd(self.pre_post_names[0]))
-        self.mpd.add(showconfig.songInMpd(song_path))
-        self.mpd.add(showconfig.songInMpd(self.pre_post_names[1]))
-        self.filename_var.set(song_path)
+        self.mpd.add(showconfig.songInMpd(MUS['preSong']))
+        self.mpd.add(showconfig.songInMpd(song))
+        self.mpd.add(showconfig.songInMpd(MUS['postSong']))
+        self.filename_var.set(graph.value(song, L9['showPath']))
+        self.song_uri = song
 
-        self.set_total_time(song_path)
+        self.set_total_time(song)
         self.mpd.seek(seconds=0, song=0)
 
     def check_autopause(self):
@@ -204,23 +208,20 @@
         self.mpd.pause()
 
 
-def buildsonglist(root,songfiles,player):
+def buildsonglist(root, graph, songs, player):
     songlist=tk.Frame(root,bd=2,relief='raised',bg='black')
 
-    prefixlen=len(os.path.commonprefix(songfiles))
-    # include to the last os.sep- dont crop path elements in the middle
-    prefixlen=songfiles[0].rfind(os.sep)+1 
-    maxsfwidth=max([len(x[prefixlen:]) for x in songfiles])
+    maxsfwidth=max([len(graph.label(song)) for song in songs])
 
-    for i,sf in enumerate(songfiles):
-        b=tk.Button(songlist,text=sf[prefixlen:],width=maxsfwidth,
+    for i,song in enumerate(songs):
+        b=tk.Button(songlist,text=graph.label(song),width=maxsfwidth,
                     anchor='w',pady=0,bd=0,relief='flat',
                     font="arial 14 bold")
         b.bind("<Configure>",lambda ev,b=b:
                b.config(font="arial %d bold" % min(12,int((ev.height-3)*.8))))
         try:
             # rainbow colors
-            frac=i/len(songfiles)
+            frac=i/len(songs)
             b.config(bg='black',
                      fg="#%02x%02x%02x" % tuple([int(255*(.7+.3*
                                                           math.sin(frac*4+x))
@@ -228,13 +229,13 @@
         except Exception,e:
             print "rainbow failed: %s"%e
         
-        b.config(command=lambda sf=sf: player.play(sf))
+        b.config(command=lambda song=song: player.play(song))
         b.pack(side='top',fill='both',exp=1,padx=0,pady=0,ipadx=0,ipady=0)
 
 
-        def color_buttons(x, y, z, sf=sf, b=b):
+        def color_buttons(x, y, z, song=song, b=b):
             name = player.filename_var.get()
-            if name == sf[prefixlen:]:
+            if name == graph.value(song, L9['showPath']):
                 b['bg'] = 'grey50'
             else:
                 b['bg'] = 'black'
@@ -391,10 +392,14 @@
 
 (options,songfiles)=parser.parse_args()
 
+graph = showconfig.getGraph()
+
 if len(songfiles)<1:
-    songfiles = [f for f in os.listdir(showconfig.musicDir())
-                 if f.endswith('wav')]
-    songfiles.sort()
+    graph = showconfig.getGraph()
+    playList = graph.value(L9['show/dance2007'], L9['playList'])
+    songs = list(graph.items(playList))
+else:
+    raise NotImplementedError("don't know how to make rdf song nodes from cmdline song paths")
 
 root=tk.Tk()
 root.wm_title("ascoltami")
@@ -402,7 +407,7 @@
 root.config(bg="black")
 player=Player(None,None)
 
-songlist = buildsonglist(root,songfiles,player)
+songlist = buildsonglist(root, graph, songs, player)
 songlist.pack(fill='both',exp=1)
 
 f2 = tk.Frame(bg='black')
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/light9/namespaces.py	Sun Mar 18 23:44:54 2007 +0000
@@ -0,0 +1,4 @@
+from rdflib import Namespace
+
+L9 = Namespace("http://light9.bigasterisk.com/")
+MUS = Namespace("http://light9.bigasterisk.com/music/")
--- a/light9/showconfig.py	Tue Mar 21 04:50:35 2006 +0000
+++ b/light9/showconfig.py	Sun Mar 18 23:44:54 2007 +0000
@@ -1,5 +1,12 @@
 from os import path,getenv
-import ConfigParser
+from rdflib.Graph import Graph
+from rdflib import URIRef
+from namespaces import MUS, L9
+
+def getGraph():
+    graph = Graph()
+    graph.parse(path.join(root(), 'config.n3'), format='n3')
+    return graph
 
 def root():
     r = getenv("LIGHT9_SHOW")
@@ -17,10 +24,30 @@
     /my/music. song is a file in musicDir; this function returns a
     version starting with the mpd path, but minus the mpd root itself.
     the mpc ~/.mpdconf """
-    
-    if 'dance2005' in root():
-        return "projects/dance2005/%s" % song
-    raise NotImplementedError
+
+    assert isinstance(song, URIRef), "songInMpd now takes URIRefs"
+
+    mpdHome = None
+    for line in open(path.expanduser("~/.mpdconf")):
+        if line.startswith("music_directory"):
+            mpdHome = line.split()[1].strip('"')
+    if mpdHome is None:
+        raise ValueError("can't find music_directory in your ~/.mpdconf")
+    mpdHome = mpdHome.rstrip(path.sep) + path.sep
+
+    songFullPath = songOnDisk(song)
+    if not songFullPath.startswith(mpdHome):
+        raise ValueError("the song path %r is not under your MPD music_directory (%r)" % (songFullPath, mpdHome))
+        
+    mpdRelativePath = songFullPath[len(mpdHome):]
+    if path.join(mpdHome, mpdRelativePath) != songFullPath:
+        raise ValueError("%r + %r doesn't make the songpath %r" % (mpdHome, mpdRelativePath, songFullPath))
+    return mpdRelativePath.encode('ascii')
+
+def songOnDisk(song):
+    graph = getGraph()
+    songFullPath = path.join(root(), graph.value(song, L9['showPath']))
+    return songFullPath
 
 def curvesDir():
     return path.join(root(),"curves")
@@ -41,6 +68,7 @@
     return path.join(root(),"patchdata.py")
 
 def prePostSong():
-    p = ConfigParser.SafeConfigParser()
-    p.read([path.join(root(),'config')])
-    return p.get('music','preSong'), p.get('music','postSong')
+    graph = getGraph()
+    return [graph.value(MUS['preSong'], L9['showPath']),
+            graph.value(MUS['postSong'], L9['showPath'])]
+