song: don't convert id to int
[nephilim.git] / nephilim / song.py
blob0f1a62706c9eea43e2837a76e31ae67a53104510
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 'track' in self._data:
31 try:
32 self._data['track'] = int(self._data['track'])
33 except ValueError:
34 # OGG tracks come in #track/#total format
35 try:
36 self._data['track'] = int(self._data['track'].split('/')[0])
37 except ValueError:
38 self._data['track'] = 0
40 # ensure all string-values are utf-8 encoded
41 for tag in self._data.keys():
42 if isinstance(self._data[tag], str):
43 self._data[tag] = self._data[tag].decode('utf-8')
44 if 'time' in self._data:
45 self._data['time'] = int(self._data['time'])
46 self._data['timems'] = '%i:%i'%(self._data['time'] / 60, self._data['time'] % 60)
47 self._data['length'] = sec2min(self._data['time'])
49 def __eq__(self, other):
50 if not isinstance(other, Song):
51 return NotImplemented
52 return self._data['file'] == other._data['file']
54 def __ne__(self, other):
55 if not isinstance(other, Song):
56 return NotImplemented
57 return self._data['file'] != other._data['file']
59 def id(self):
60 """Get the song's playlist ID. (-1 if not in playlist)."""
61 return self.tag('id', '-1')
63 def title(self):
64 """Get the song's title (or filename if it has no title)."""
65 return self.tag('title', self._data['file'])
67 def artist(self):
68 """Get the song's artist."""
69 return self.tag('artist')
71 def track(self):
72 """Get the song's track number."""
73 return self.tag('track')
75 def album(self):
76 """Get the album."""
77 return self.tag('album')
79 def filepath(self):
80 """Get the filepath."""
81 return self._data['file']
83 def tag(self, tag, default=''):
84 """Get a tag. If it doesn't exist, return default."""
85 if tag in self._data:
86 return self._data[tag]
87 if tag=='song':
88 return self.__str__()
90 return default
92 def expand_tags(self, str):
93 """Expands tags in form $tag in str."""
94 ret = str
95 ret = ret.replace('$title', self.title()) #to ensure that it is set to at least filename
96 for tag in self._data:
97 ret = ret.replace('$' + tag, unicode(self._data[tag]))
98 ret = ret.replace('$songdir', os.path.dirname(self.filepath()))
99 return ret