summaryrefslogtreecommitdiff
path: root/nephilim/song.py
blob: df111cd6da80a64d1a31fc349a6c73c4efa3a80c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#
#    Copyright (C) 2008 jerous <jerous@gmail.com>
#    Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
#
#    Nephilim is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Nephilim is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Nephilim.  If not, see <http://www.gnu.org/licenses/>.
#

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('0') or 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 __eq__(self, other):
        if not isinstance(other, Song):
            return NotImplemented
        return self._data['file'] == other._data['file']

    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