[Tkabber-dev] r38 - in trunk/plugins/np_file: . contrib contrib/quodlibet

tkabber-svn at jabber.ru tkabber-svn at jabber.ru
Sun Sep 16 04:20:21 MSD 2007


Author: kostix
Date: 2007-09-16 04:20:20 +0400 (Sun, 16 Sep 2007)
New Revision: 38

Added:
   trunk/plugins/np_file/contrib/
   trunk/plugins/np_file/contrib/quodlibet/
   trunk/plugins/np_file/contrib/quodlibet/np_simple.py
Modified:
   trunk/plugins/np_file/np_file.tcl
Log:
np_file/np_file.tcl
  Implemented support for "np_simple" Quod Libet plugin.

np_file/contrib
  Added directory for contributions.

np_file/contrib/quodlibet/np_simple.py
  Added Quod Libet plugin which works like QL's internal
  ~/.quodlibet/current but also provides playing state info.


Added: trunk/plugins/np_file/contrib/quodlibet/np_simple.py
===================================================================
--- trunk/plugins/np_file/contrib/quodlibet/np_simple.py	                        (rev 0)
+++ trunk/plugins/np_file/contrib/quodlibet/np_simple.py	2007-09-16 00:20:20 UTC (rev 38)
@@ -0,0 +1,72 @@
+# $Id$
+# "Now playing simple" plugin for Quod Libet: writes info about
+# currently playing song to a text file.
+# Format is similar to ~/.quodlibet/current, but it contains less data
+# (only what is needed for XEP-0118) and also tracks playing/paused state.
+# Written by Konstantin Khomoutov <flatworm at users.sourceforge.net>
+# Inspired by JEP118 plugin.
+
+# TODO dump all available info.
+# TODO file feature request to modify "current" instead.
+
+import os
+import util
+
+from plugins.events import EventPlugin
+
+outfile = os.path.expanduser("~/.quodlibet/now_playing")
+format = """\
+status=%s
+artist=%s
+title=%s
+album=%s
+tracknumber=%s
+~#length=%d
+"""
+
+class np_simple(EventPlugin):
+	PLUGIN_ID = "np_simple"
+	PLUGIN_NAME = "Now Playing Simple"
+	PLUGIN_DESC = "Outputs simple now playing info to a text file"
+	PLUGIN_ICON = 'gtk-save'
+	PLUGIN_VERSION = "0.1"
+
+	def dump_paused(self):
+		try:
+			f = file(outfile, "w")
+			f.write("status=paused\n")
+		except EnvironmentError: pass
+		else: f.close()
+
+	def dump_song_info(self, song):
+		try:
+			f = file(outfile, "w")
+			f.write(format % (
+				"playing",
+				util.escape(song.comma("artist")),
+				util.escape(song.comma("title")),
+				util.escape(song.comma("album")),
+				song("tracknumber"),
+				song("~#length")))
+		except EnvironmentError: pass
+		else: f.close()
+
+	def plugin_on_song_started(self, song):
+		if song:
+			self.song = song
+			self.dump_song_info(song)
+		else:
+			self.dump_paused()
+
+	def plugin_on_paused(self):
+		self.dump_paused()
+
+	def plugin_on_unpaused(self):
+		self.dump_song_info(self.song)
+
+	def plugin_on_song_ended(self, song, stopped):
+		self.dump_paused()
+		
+	def disabled(self):
+		self.dump_paused()
+


Property changes on: trunk/plugins/np_file/contrib/quodlibet/np_simple.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: trunk/plugins/np_file/np_file.tcl
===================================================================
--- trunk/plugins/np_file/np_file.tcl	2007-09-15 17:29:14 UTC (rev 37)
+++ trunk/plugins/np_file/np_file.tcl	2007-09-16 00:20:20 UTC (rev 38)
@@ -17,6 +17,14 @@
 		lset args 0 [uplevel 1 namespace current]::[lindex $args 0]
 	}
 
+	proc uprefs {} {
+		foreach v [uplevel 1 info locals] {
+			if {[string match &* $v]} {
+				uplevel 1 upvar 1 "\${$v} [string range $v 1 end]"
+			}
+		}
+	}
+
 	custom::defgroup "User Tune" \
 		[::msgcat::mc "Plugins for automatic gathering of\
 			information for User Tune personal eventing facility."] \
@@ -161,12 +169,7 @@
 	variable options
 	variable repollid
 
-	upvar 0 options(media_player) tag
-	set failed [catch $options(parser,$tag) err]
-	if {$failed} {
-		puts stderr "Parser scrit for player \"$options(name,$tag)\"\
-			failed: $err"
-	}
+	yield
 
 	if {$options(check_timeout) > 0} {
 		set repollid [after \
@@ -174,6 +177,31 @@
 	}
 }
 
+proc np_file::yield {} {
+	variable options
+	variable lasttime
+	variable monitoring
+	variable connections
+
+	if {![file exists $options(file)]} {
+		puts stderr "np_file: file \"$options(file)\" doesn't exist"
+		return
+	}
+
+	set mtime [file mtime $options(file)]
+	if {$mtime - $lasttime < $options(mtime_threshold)} return
+	set lasttime $mtime
+
+	upvar 0 options(media_player) tag
+	set failed [catch [list $options(parser,$tag) $options(file)]]
+	if {$failed} {
+		global errorInfo
+		set monitoring false
+		puts stderr "Parser scrit for player \"$options(name,$tag)\"\
+			failed: $errorInfo"
+	}
+}
+
 proc np_file::on_media_player_changed args {
 	variable connections
 
@@ -191,22 +219,10 @@
 
 #### Foobar2000:
 
-proc np_file::foobar_yield {} {
-	variable options
-	variable lasttime
-	variable monitoring
-	variable connections
+proc np_file::foobar_yield fname {
+	debugmsg tune [info level 0]
 
-	if {![file exists $options(file)]} {
-		puts stderr "np_file: file \"$options(file)\" doesn't exist"
-		return
-	}
-
-	set mtime [file mtime $options(file)]
-	if {$mtime - $lasttime < $options(mtime_threshold)} return
-	set lasttime $mtime
-
-	set fd [open $options(file)]
+	set fd [open $fname]
 	fconfigure $fd -encoding utf-8
 	set data [read $fd]
 	close $fd
@@ -228,10 +244,9 @@
 	}
 }
 
-proc np_file::parse_foobar {data \
-		vstatus vartist vtitle vtrack vlength vsource vuri} {
-	upvar 1 $vstatus status $vartist artist $vtitle title \
-		$vtrack track $vlength length $vsource source $vuri uri
+proc np_file::foobar_parse {data \
+		&status &artist &title &track &length &source &uri} {
+	uprefs
 
 	set lines [list]
 	foreach line [split $data \n] {
@@ -280,11 +295,70 @@
 	}
 }
 
-#### Quod Libit:
+#### Quod Libet:
 
-proc np_file::quodlibet_yield {} {
+proc np_file::quodlibet_yield fname {
+	variable connections
+
 	debugmsg tune [info level 0]
+
+	set fd [open $fname]
+	fconfigure $fd -encoding utf-8
+	set data [read $fd]
+	close $fd
+
+	quodlibet_parse $data status artist title track length source uri
+
+	switch -- $status {
+		paused -
+		stopped {
+			# TODO implement publishing of "stopped":
+			foreach connid $connections {
+				#::plugins::tune::unpublish $connid
+			}
+			return
+		}
+		playing {
+			foreach connid $connections {
+				::plugins::tune::publish $connid \
+					$artist $title $track $length $source $uri
+			}
+		}
+	}
 }
 
+proc np_file::quodlibet_parse {data \
+		&status &artist &title &track &length &source &uri} {
+	uprefs
+
+	set status ""
+	set artist ""
+	set title  ""
+	set track  ""
+	set length ""
+	set source ""
+	set uri    ""
+
+	foreach line [split $data \n] {
+		set line [string trim $line]
+		if {[string length $line] = 0} continue
+		set ix [string first = $line]
+		set key [string range $line 0 [expr {$ix - 1}]]
+		set val [string range $line [expr {$ix + 1}] end]
+		switch -- $key {
+			status      { set status $val }
+			album       { set source $val }
+			artist      { set artist $val }
+			date        { set date   $val }
+			~#length    { set length $val }
+			title       { set title  $val }
+			tracknumber { set track  $val }
+			default     {
+				puts stderr "np_file: junk key: \"$key\", ignored"
+			}
+		}
+	}
+}
+
 np_file::enumerate_media_players
 



More information about the Tkabber-dev mailing list