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

tkabber-svn at jabber.ru tkabber-svn at jabber.ru
Mon Sep 17 20:27:47 MSD 2007


Author: kostix
Date: 2007-09-17 20:27:46 +0400 (Mon, 17 Sep 2007)
New Revision: 42

Modified:
   trunk/plugins/np_file/contrib/quodlibet/np_simple.py
   trunk/plugins/np_file/np_file.tcl
Log:
np_file/contrib/quodlibet/np_simple.py: now saves "inactive" status when the player exits (or killed via SIGTERM).

np_file/np_file.tcl: several fixes, "mtime_threshold" configuration option renamed to "update_threshold".
  Works with "np_simple" of Quod Libet now.


Modified: trunk/plugins/np_file/contrib/quodlibet/np_simple.py
===================================================================
--- trunk/plugins/np_file/contrib/quodlibet/np_simple.py	2007-09-17 16:26:06 UTC (rev 41)
+++ trunk/plugins/np_file/contrib/quodlibet/np_simple.py	2007-09-17 16:27:46 UTC (rev 42)
@@ -6,11 +6,14 @@
 # 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.
+# TODO
+# * Dump all available info.
+# * Probably dump song info also on "paused" state.
+# What's with Unicode/UTF-8? Ideally we need UTF-8 file.
 
 import os
 import util
+import atexit
 
 from plugins.events import EventPlugin
 
@@ -24,49 +27,51 @@
 ~#length=%d
 """
 
+def report_inactive(state):
+	try:
+		f = file(outfile, 'w')
+		f.write("status=%s\n" % state)
+	except EnvironmentError: pass
+	else: f.close()
+
 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_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"
+	PLUGIN_VERSION = '0.2'
 
-	def dump_paused(self):
+	def report_song_info(self, song):
 		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 = 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")))
+				'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)
+			self.report_song_info(song)
 		else:
-			self.dump_paused()
+			report_inactive('paused')
 
 	def plugin_on_paused(self):
-		self.dump_paused()
+		report_inactive('paused')
 
 	def plugin_on_unpaused(self):
-		self.dump_song_info(self.song)
+		self.report_song_info(self.song)
 
 	def plugin_on_song_ended(self, song, stopped):
-		self.dump_paused()
+		report_inactive('paused')
 		
 	def disabled(self):
-		self.dump_paused()
+		report_inactive('inactive')
 
+atexit.register(report_inactive, 'inactive')
+

Modified: trunk/plugins/np_file/np_file.tcl
===================================================================
--- trunk/plugins/np_file/np_file.tcl	2007-09-17 16:26:06 UTC (rev 41)
+++ trunk/plugins/np_file/np_file.tcl	2007-09-17 16:27:46 UTC (rev 42)
@@ -57,12 +57,12 @@
 		-type integer \
 		-group NowPlayingFile
 
-	custom::defvar options(mtime_threshold) 30 \
+	custom::defvar options(update_threshold) 30 \
 		[::msgcat::mc "Minimal amount of time (in seconds) that must tick\
 			between adjacent now-playing file modifications\
 			for its contents to be used."] \
 		-type integer \
-		-command on_mtime_thresh_changed \
+		-command [mycmd on_update_thresh_changed] \
 		-group NowPlayingFile
 
 	hook::add connected_hook \
@@ -189,7 +189,7 @@
 	}
 
 	set mtime [file mtime $options(file)]
-	if {$mtime - $lasttime < $options(mtime_threshold)} return
+	if {$mtime - $lasttime < $options(update_threshold)} return
 	set lasttime $mtime
 
 	upvar 0 options(media_player) tag
@@ -208,12 +208,12 @@
 	monitoring [expr {[enabled] && [llength $connections] > 0}]
 }
 
-proc np_file::on_mtime_thresh_changed args {
+proc np_file::on_update_thresh_changed args {
 	variable options
 
-	# Prevent modification time threshold to be lower than 10 sec:
-	if {$options(mtime_threshold) < 10} {
-		set options(mtime_threshold) 10
+	# Prevent modification time threshold from being lower than 10 sec:
+	if {$options(update_threshold) < 10} {
+		set options(update_threshold) 10
 	}
 }
 
@@ -300,61 +300,66 @@
 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
+	quodlibet_parse $data info
 
-	switch -- $status {
+	switch -- $info(status) {
 		paused -
-		stopped {
-			# TODO implement publishing of "stopped":
+		inactive {
 			foreach connid $connections {
-				#::plugins::tune::unpublish $connid
+				::plugins::tune::unpublish $connid
 			}
-			return
 		}
 		playing {
+			unset info(status)
+			set args [list]
+			foreach {key val} [array get info] {
+				if {$val == ""} continue
+				lappend args -$key $val
+			}
 			foreach connid $connections {
-				::plugins::tune::publish $connid \
-					$artist $title $track $length $source $uri
+				eval [list ::plugins::tune::publish $connid] $args
 			}
 		}
+		default {
+			puts stderr "np_file: unknown or missing playing status, ignored"
+		}
 	}
 }
 
-proc np_file::quodlibet_parse {data \
-		&status &artist &title &track &length &source &uri} {
-	uprefs
+proc np_file::quodlibet_parse {data vinfo} {
+	upvar 1 $vinfo info
 
-	set status ""
-	set artist ""
-	set title  ""
-	set track  ""
-	set length ""
-	set source ""
-	set uri    ""
+	array set info {
+		status ""
+		artist ""
+		title  ""
+		track  ""
+		length ""
+		source ""
+		uri    ""
+	}
 
 	foreach line [split $data \n] {
 		set line [string trim $line]
-		if {[string length $line] = 0} continue
+		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 }
+			status      { set info(status) $val }
+			album       { set info(source) $val }
+			artist      { set info(artist) $val }
+			date        { set info(date)   $val }
+			~#length    { set info(length) $val }
+			title       { set info(title)  $val }
+			tracknumber { set info(track)  $val }
 			default     {
-				puts stderr "np_file: junk key: \"$key\", ignored"
+				puts stderr "np_file: unknown key: \"$key\", ignored"
 			}
 		}
 	}



More information about the Tkabber-dev mailing list