AlbumCover: implement manually setting cover
[nephilim.git] / nephilim / song.py
blob008312a249c3a49f21a11149f74fc371615d0f66
2 # Copyright (C) 2008 jerous <jerous@gmail.com>
3 # Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
5 # Nephilim is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # Nephilim is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Nephilim. If not, see <http://www.gnu.org/licenses/>.
19 from PyQt4 import QtCore
20 import os
22 from common import sec2min
24 class Song:
25 """The Song class offers an abstraction of a song."""
26 _data = None
28 def __init__(self, data):
29 self._data = data
30 if 'id' in self._data:
31 self._data['id'] = int(self._data['id'])
32 if 'track' in self._data:
33 try:
34 self._data['track'] = int(self._data['track'])
35 except ValueError:
36 # OGG tracks come in #track/#total format
37 try:
38 self._data['track'] = int(self._data['track'].split('/')[0])
39 except ValueError:
40 self._data['track'] = 0
42 # ensure all string-values are utf-8 encoded
43 for tag in self._data.keys():
44 if isinstance(self._data[tag], str):
45 self._data[tag] = self._data[tag].decode('utf-8')
46 if 'time' in self._data:
47 self._data['time'] = int(self._data['time'])
48 self._data['timems'] = '%i:%i'%(self._data['time'] / 60, self._data['time'] % 60)
49 self._data['length'] = sec2min(self._data['time'])
51 def __eq__(self, other):
52 if not isinstance(other, Song):
53 return NotImplemented
54 return self._data['file'] == other._data['file']
56 def __ne__(self, other):
57 if not isinstance(other, Song):
58 return NotImplemented
59 return self._data['file'] != other._data['file']
61 def id(self):
62 """Get the song's playlist ID. (-1 if not in playlist)."""
63 return self.tag('id', -1)
65 def title(self):
66 """Get the song's title (or filename if it has no title)."""
67 return self.tag('title', self._data['file'])
69 def artist(self):
70 """Get the song's artist."""
71 return self.tag('artist')
73 def track(self):
74 """Get the song's track number."""
75 return self.tag('track')
77 def album(self):
78 """Get the album."""
79 return self.tag('album')
81 def filepath(self):
82 """Get the filepath."""
83 return self._data['file']
85 def tag(self, tag, default=''):
86 """Get a tag. If it doesn't exist, return default."""
87 if tag in self._data:
88 return self._data[tag]
89 if tag=='song':
90 return self.__str__()
92 return default
94 def expand_tags(self, str):
95 """Expands tags in form $tag in str."""
96 ret = str
97 ret = ret.replace('$title', self.title()) #to ensure that it is set to at least filename
98 for tag in self._data:
99 ret = ret.replace('$' + tag, unicode(self._data[tag]))
100 ret = ret.replace('$songdir', os.path.dirname(self.filepath()))
101 return ret