Lyrics: support for loading stored lyrics.
[nephilim.git] / nephilim / song.py
blobd2221ec7b122136c18d5ea0bee2b6feb85c2d64c
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 misc 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 # make sure the track is a valid number!
34 t=self._data['track']
35 for i in xrange(len(t)):
36 if ord(t[i])<ord('0') or ord(t[i])>ord('9'):
37 try:
38 self._data['track']=int(t[0:i])
39 except TypeError:
40 self._data['track']=-1
41 break
42 self._data['track']=int(self._data['track'])
44 # ensure all string-values are utf-8 encoded
45 for tag in self._data.keys():
46 if isinstance(self._data[tag], str):
47 self._data[tag] = self._data[tag].decode('utf-8')
48 if 'time' in self._data:
49 self._data['time'] = int(self._data['time'])
50 self._data['timems'] = '%i:%i'%(self._data['time'] / 60, self._data['time'] % 60)
51 self._data['length'] = sec2min(self._data['time'])
53 def __eq__(self, other):
54 if not isinstance(other, Song):
55 return NotImplemented
56 return self._data['file'] == other._data['file']
58 def __ne__(self, other):
59 if not isinstance(other, Song):
60 return NotImplemented
61 return self._data['file'] != other._data['file']
63 def id(self):
64 """Get the song's playlist ID. (-1 if not in playlist)."""
65 return self.tag('id', -1)
67 def title(self):
68 """Get the song's title (or filename if it has no title)."""
69 return self.tag('title', self._data['file'])
71 def artist(self):
72 """Get the song's artist."""
73 return self.tag('artist')
75 def track(self):
76 """Get the song's track number."""
77 return self.tag('track')
79 def album(self):
80 """Get the album."""
81 return self.tag('album')
83 def filepath(self):
84 """Get the filepath."""
85 return self._data['file']
87 def tag(self, tag, default=''):
88 """Get a tag. If it doesn't exist, return default."""
89 if tag in self._data:
90 return self._data[tag]
91 if tag=='song':
92 return self.__str__()
94 return default
96 def expand_tags(self, str):
97 """Expands tags in form $tag in str."""
98 ret = str
99 ret = ret.replace('$title', self.title()) #to ensure that it is set to at least filename
100 for tag in self._data:
101 ret = ret.replace('$' + tag, unicode(self._data[tag]))
102 ret = ret.replace('$songdir', os.path.dirname(self.filepath()))
103 return ret