]> kaliko git repositories - mpd-sima.git/blob - sima/lib/track.py
Initial import
[mpd-sima.git] / sima / lib / track.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009, 2010, 2011, 2013 Jack Kaliko <efrim@azylum.org>
4 # Copyright (c) 2009 J. Alexander Treuman (Tag collapse method)
5 # Copyright (c) 2008 Rick van Hattem
6 #
7 #  This file is part of MPD_sima
8 #
9 #  MPD_sima is free software: you can redistribute it and/or modify
10 #  it under the terms of the GNU General Public License as published by
11 #  the Free Software Foundation, either version 3 of the License, or
12 #  (at your option) any later version.
13 #
14 #  MPD_sima is distributed in the hope that it will be useful,
15 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #  GNU General Public License for more details.
18 #
19 #  You should have received a copy of the GNU General Public License
20 #  along with MPD_sima.  If not, see <http://www.gnu.org/licenses/>.
21 #
22 #
23
24 import time
25
26
27 class Track(object):
28     """
29     Track object.
30     Instanciate with mpd replies.
31     """
32
33     def __init__(self, file=None, time=0, pos=0, **kwargs):
34         self.title = self.artist = self.album = self.albumartist = ''
35         self._pos = pos
36         self.empty = False
37         self._file = file
38         if not kwargs:
39             self.empty = True
40         self.time = time
41         self.__dict__.update(**kwargs)
42         self.tags_to_collapse = list(['artist', 'album', 'title', 'date',
43             'genre', 'albumartist'])
44         #  have tags been collapsed?
45         self.collapse_tags_bool = False
46         self.collapsed_tags = list()
47         # Needed for multiple tags which returns a list instead of a string
48         self.collapse_tags()
49
50     def collapse_tags(self):
51         """
52         Necessary to deal with tags defined multiple times.
53         These entries are set as lists instead of strings.
54         """
55         for tag, value in self.__dict__.items():
56             if tag not in self.tags_to_collapse:
57                 continue
58             if isinstance(value, list):
59                 self.collapse_tags_bool = True
60                 self.collapsed_tags.append(tag)
61                 self.__dict__.update({tag: ', '.join(set(value))})
62
63     def get_filename(self):
64         """return filename"""
65         if not self.file:
66             return None
67         return self.file
68
69     def __repr__(self):
70         return '%s(artist="%s", album="%s", title="%s", filename="%s")' % (
71             self.__class__.__name__,
72             self.artist,
73             self.album,
74             self.title,
75             self.file,
76         )
77
78     def __str__(self):
79         return '{artist} - {album} - {title} ({duration})'.format(
80                 duration=self.duration,
81                 **self.__dict__
82                 )
83
84     def __int__(self):
85         return self.time
86
87     def __add__(self, other):
88         return Track(time=self.time + other.time)
89
90     def __sub__(self, other):
91         return Track(time=self.time - other.time)
92
93     def __hash__(self):
94         if self.file:
95             return hash(self.file)
96         else:
97             return id(self)
98
99     def __eq__(self, other):
100         return hash(self) == hash(other)
101
102     def __ne__(self, other):
103         return hash(self) != hash(other)
104
105     def __bool__(self):
106         return not self.empty
107
108     @property
109     def pos(self):
110         """return position of track in the playlist"""
111         return int(self._pos)
112
113     @property
114     def file(self):
115         """file is an immutable attribute that's used for the hash method"""
116         return self._file
117
118     def get_time(self):
119         """get time property"""
120         return self._time
121
122     def set_time(self, value):
123         """set time property"""
124         self._time = int(value)
125
126     time = property(get_time, set_time, doc='song duration in seconds')
127
128     @property
129     def duration(self):
130         """Compute fancy duration"""
131         temps = time.gmtime(int(self.time))
132         if temps.tm_hour:
133             fmt = '%H:%M:%S'
134         else:
135             fmt = '%M:%S'
136         return time.strftime(fmt, temps)
137
138
139 def main():
140     pass
141
142 # Script starts here
143 if __name__ == '__main__':
144     main()
145
146 # VIM MODLINE
147 # vim: ai ts=4 sw=4 sts=4 expandtab