1 # -*- coding: utf-8 -*-
3 # Copyright (c) 2009, 2010, 2011, 2013, 2014 Jack Kaliko <kaliko@azylum.org>
4 # Copyright (c) 2009 J. Alexander Treuman (Tag collapse method)
5 # Copyright (c) 2008 Rick van Hattem
7 # This file is part of sima
9 # 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.
14 # 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.
19 # You should have received a copy of the GNU General Public License
20 # along with sima. If not, see <http://www.gnu.org/licenses/>.
26 from .meta import Artist
31 Instanciate with Player replies.
34 def __init__(self, file=None, time=0, pos=-1, **kwargs):
35 self.title = self.artist = self.album = self.albumartist = ''
36 self.musicbrainz_artistid = self.musicbrainz_albumartistid = None
43 self.__dict__.update(**kwargs)
44 self.tags_to_collapse = ['artist', 'album', 'title', 'date',
45 'genre', 'albumartist']
46 # have tags been collapsed?
47 self.collapse_tags_bool = False
48 self.collapsed_tags = list()
49 # Needed for multiple tags which returns a list instead of a string
52 def collapse_tags(self):
54 Necessary to deal with tags defined multiple times.
55 These entries are set as lists instead of strings.
57 for tag, value in self.__dict__.items():
58 if tag not in self.tags_to_collapse:
60 if isinstance(value, list):
61 self.collapse_tags_bool = True
62 self.collapsed_tags.append(tag)
63 self.__dict__.update({tag: ', '.join(set(value))})
66 return '%s(artist="%s", album="%s", title="%s", filename="%s")' % (
67 self.__class__.__name__,
75 return '{artist} - {album} - {title} ({duration})'.format(
76 duration=self.duration,
83 def __add__(self, other):
84 return Track(time=self.time + other.time)
86 def __sub__(self, other):
87 return Track(time=self.time - other.time)
91 return hash(self.file)
95 def __eq__(self, other):
96 return hash(self) == hash(other)
98 def __ne__(self, other):
99 return hash(self) != hash(other)
102 return not self._empty
106 """file is an immutable attribute that's used for the hash method"""
110 """get time property"""
113 def set_time(self, value):
114 """set time property"""
115 self._time = int(value)
117 time = property(get_time, set_time, doc='song duration in seconds')
121 """Compute fancy duration"""
122 temps = time.gmtime(int(self.time))
127 return time.strftime(fmt, temps)
129 def get_artist(self):
130 """Get artist object from track"""
132 mbid = self.musicbrainz_artistid
133 if self.albumartist != 'Various Artists':
134 name = self.albumartist
135 if self.musicbrainz_albumartistid != '89ad4ac3-39f7-470e-963a-56509c546377':
136 mbid = self.musicbrainz_albumartistid
137 return Artist(name=name, mbid=mbid)
140 # vim: ai ts=4 sw=4 sts=4 expandtab