#!/usr/bin/env python # -*- coding: Latin-1 -*- import user import os from xml.dom import minidom # the current.xml file PLAYLISTFILE = "%s/.trinity/share/apps/amarok/current.xml"%(user.home) # the fields to be shown via http FIELDS = ("Artist", "Title", "Album", "Track", "Length", "Genre", "Score" ) class Track(object): """Class that holds the information of one track in the current playlist""" __Q_SLOTS__ = FIELDS def __init__(self, **kwargs): for key,value in kwargs.iteritems(): setattr(self, key, value) def toRow(self, style=''): """Returns the a html-table-row with member values of this class""" tmp = ['%s'%(i) for i in [getattr(self,f) for f in \ self.__Q_SLOTS__ ]] tr_style = '' if style: tr_style='class="%s"'%(style) return '%s'%(tr_style, ''.join(tmp)) class Playlist: """The Playlist class represents the Playlist as one object. It holds all needed information and does most of the work""" def __init__(self): """The Constructor takes no arguments.""" self.sync() self.tracks = [] self.mtime = self._getMtime() self.firstRun = 1 self.code=""" \n AmaroK playlist\n
current amarok playlist of %s
\n
%s
\n \n """ self.encoding = "UTF-8" self.fullPage = "" self._buildDoc() def toHtml(self): """Returns a html representation of the whole Playlist""" if self.firstRun: self._setFullPage() self.firstRun = 0 else: newmtime = self._getMtime() if newmtime !=self.mtime: self._buildDoc() self._setFullPage() self.mtime = newmtime return self.fullPage def _setFullPage(self): self.fullPage = self.code%(os.environ['LOGNAME'],(self._createTable()).encode(self.encoding,'replace')) def _getMtime(self): """gets the mtime from the current.xml file, to check if the current.xml has been modified or not""" return os.stat(PLAYLISTFILE)[-2] def _buildDoc(self): """Build the DOM-doc and calls the _extractTracks-Method""" self.doc = minidom.parse(PLAYLISTFILE) self._extractTracks() def _extractTracks(self): """extracts all "item"-elements from the doc and creates an Track-object to store the associated information""" append = self.tracks.append for item in self.doc.getElementsByTagName("item"): curTrack = Track() for elem in FIELDS: try: value = item.getElementsByTagName(elem)[0].firstChild.nodeValue except: value = ' ' setattr(curTrack, elem, value) append(curTrack) def _createTable(self): """Returns the HTML-Table""" tbl = """ %s%s
""" rows = self._createRows() thead = "".join(['%s'%(i) for i in FIELDS]) return tbl %(thead,"".join(rows)) def _createRows(self): """Returns the table rows""" retval = [] i = 1 for track in self.tracks: style = 'tr_one' if i %2 == 0: style = 'tr_two' retval.append(track.toRow(style=style)) i = i+1 return retval def sync(self): """Saves the current Amarok-Playlist to the current.xml file. Calls amarok via dcop""" os.system("dcop amarok playlist saveCurrentPlaylist")