AlbumCover: refresh has to take two parameters.
[nephilim.git] / clSong.py
blob9b38076dbcbcff9c8a5a82625db09b77d3d8c1e9
1 from PyQt4 import QtCore
2 from misc import ORGNAME, APPNAME, sec2min
3 import os
5 # compare two songs with respect to their album
6 def isSameAlbum(song1, song2):
7 return song1.getAlbum()==song2.getAlbum() \
8 and song1.getTag('date')==song2.getTag('date')
10 class Song:
11 """The Song class offers an abstraction of a song."""
12 _data=None
14 def __init__(self, data):
15 self._data=data
16 if 'id' in self._data:
17 self._data['id']=int(self._data['id'])
18 if 'track' in self._data:
19 # make sure the track is a valid number!
20 t=self._data['track']
21 for i in xrange(len(t)):
22 if ord(t[i])<ord('0') or ord(t[i])>ord('9'):
23 try:
24 self._data['track']=int(t[0:i])
25 except TypeError:
26 self._data['track']=-1
27 break
28 self._data['track']=int(self._data['track'])
30 # ensure all string-values are utf-8 encoded
31 for tag in self._data.keys():
32 if isinstance(self._data[tag], str):
33 self._data[tag]=unicode(self._data[tag], "utf-8")
34 if 'time' in self._data:
35 self._data['time'] = int(self._data['time'])
36 self._data['timems'] = '%i:%i'%(self._data['time'] / 60, self._data['time'] % 60)
37 self._data['length'] = sec2min(self._data['time'])
39 def getID(self):
40 """Get the ID."""
41 return self.getTag('id', -1)
43 def getTitle(self):
44 """Get the title."""
45 return self.getTag('title', self._data['file'])
47 def getArtist(self):
48 """Get the artist."""
49 return self.getTag('artist', self._data['file'])
51 def getTrack(self):
52 """Get the track."""
53 return self.getTag('track')
55 def getAlbum(self):
56 """Get the album."""
57 return self.getTag('album')
59 def getFilepath(self):
60 """Get the filepath."""
61 return self._data['file']
63 def match(self, str):
64 """Checks if the string str matches this song. Assumes str is lowercase."""
65 return str.__str__() in self.__str__().lower()
67 def __str__(self):
68 return "%s - %s [%s]" % (self.getTag('artist'), self.getTag('title'), self.getTag('album'))
70 def getTag(self, tag, default=''):
71 """Get a tag. If it doesn't exist, return $default."""
72 if tag in self._data:
73 return self._data[tag]
74 if tag=='song':
75 return self.__str__()
77 return default
79 def expand_tags(self, str):
80 """Expands tags in form $tag in str."""
81 ret = str
82 for tag in self._data:
83 ret = ret.replace('$' + tag, unicode(self._data[tag]))
84 return ret