From 2b72b4e8f8b00f9835e376b7a10bea78df5d93de Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 7 Mar 2009 14:14:03 +0100 Subject: clSong->song --- nephilim/clSong.py | 76 ---------------------------------------------------- nephilim/mpclient.py | 2 +- nephilim/song.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 77 deletions(-) delete mode 100644 nephilim/clSong.py create mode 100644 nephilim/song.py diff --git a/nephilim/clSong.py b/nephilim/clSong.py deleted file mode 100644 index 3bd319c..0000000 --- a/nephilim/clSong.py +++ /dev/null @@ -1,76 +0,0 @@ -from PyQt4 import QtCore -import os - -from misc import sec2min - -class Song: - """The Song class offers an abstraction of a song.""" - _data = None - - def __init__(self, data): - self._data = data - if 'id' in self._data: - self._data['id'] = int(self._data['id']) - if 'track' in self._data: - # make sure the track is a valid number! - t=self._data['track'] - for i in xrange(len(t)): - if ord(t[i])ord('9'): - try: - self._data['track']=int(t[0:i]) - except TypeError: - self._data['track']=-1 - break - self._data['track']=int(self._data['track']) - - # ensure all string-values are utf-8 encoded - for tag in self._data.keys(): - if isinstance(self._data[tag], str): - self._data[tag] = self._data[tag].decode('utf-8') - if 'time' in self._data: - self._data['time'] = int(self._data['time']) - self._data['timems'] = '%i:%i'%(self._data['time'] / 60, self._data['time'] % 60) - self._data['length'] = sec2min(self._data['time']) - - def id(self): - """Get the song's playlist ID. (-1 if not in playlist).""" - return self.tag('id', -1) - - def title(self): - """Get the song's title (or filename if it has no title).""" - return self.tag('title', self._data['file']) - - def artist(self): - """Get the song's artist.""" - return self.tag('artist') - - def track(self): - """Get the song's track number.""" - return self.tag('track') - - def album(self): - """Get the album.""" - return self.tag('album') - - def filepath(self): - """Get the filepath.""" - return self._data['file'] - - def tag(self, tag, default=''): - """Get a tag. If it doesn't exist, return default.""" - if tag in self._data: - return self._data[tag] - if tag=='song': - return self.__str__() - - return default - - def expand_tags(self, str): - """Expands tags in form $tag in str.""" - ret = str - ret = ret.replace('$title', self.title()) #to ensure that it is set to at least filename - for tag in self._data: - ret = ret.replace('$' + tag, unicode(self._data[tag])) - ret = ret.replace('$songdir', os.path.dirname(self.filepath())) - return ret - diff --git a/nephilim/mpclient.py b/nephilim/mpclient.py index 148e1e5..526f85c 100644 --- a/nephilim/mpclient.py +++ b/nephilim/mpclient.py @@ -3,7 +3,7 @@ import mpd from threading import Thread import socket -from clSong import Song +from song import Song class MPClient(QtCore.QObject): """This class offers another layer above pympd, with usefull events.""" diff --git a/nephilim/song.py b/nephilim/song.py new file mode 100644 index 0000000..3bd319c --- /dev/null +++ b/nephilim/song.py @@ -0,0 +1,76 @@ +from PyQt4 import QtCore +import os + +from misc import sec2min + +class Song: + """The Song class offers an abstraction of a song.""" + _data = None + + def __init__(self, data): + self._data = data + if 'id' in self._data: + self._data['id'] = int(self._data['id']) + if 'track' in self._data: + # make sure the track is a valid number! + t=self._data['track'] + for i in xrange(len(t)): + if ord(t[i])ord('9'): + try: + self._data['track']=int(t[0:i]) + except TypeError: + self._data['track']=-1 + break + self._data['track']=int(self._data['track']) + + # ensure all string-values are utf-8 encoded + for tag in self._data.keys(): + if isinstance(self._data[tag], str): + self._data[tag] = self._data[tag].decode('utf-8') + if 'time' in self._data: + self._data['time'] = int(self._data['time']) + self._data['timems'] = '%i:%i'%(self._data['time'] / 60, self._data['time'] % 60) + self._data['length'] = sec2min(self._data['time']) + + def id(self): + """Get the song's playlist ID. (-1 if not in playlist).""" + return self.tag('id', -1) + + def title(self): + """Get the song's title (or filename if it has no title).""" + return self.tag('title', self._data['file']) + + def artist(self): + """Get the song's artist.""" + return self.tag('artist') + + def track(self): + """Get the song's track number.""" + return self.tag('track') + + def album(self): + """Get the album.""" + return self.tag('album') + + def filepath(self): + """Get the filepath.""" + return self._data['file'] + + def tag(self, tag, default=''): + """Get a tag. If it doesn't exist, return default.""" + if tag in self._data: + return self._data[tag] + if tag=='song': + return self.__str__() + + return default + + def expand_tags(self, str): + """Expands tags in form $tag in str.""" + ret = str + ret = ret.replace('$title', self.title()) #to ensure that it is set to at least filename + for tag in self._data: + ret = ret.replace('$' + tag, unicode(self._data[tag])) + ret = ret.replace('$songdir', os.path.dirname(self.filepath())) + return ret + -- cgit v1.2.3