1 # -*- coding: utf-8 -*-
3 # Copyright (c) 2009, 2010, 2011, 2013, 2014, 2015, 2016, 2017 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, SEPARATOR
31 Instantiate with Player replies.
33 :param str file: media file, defaults to ``None``
34 :param int time: duration in second, defaults to 0
35 :param int pos: position in queue, defaults to -1
36 :param str title|artist|album: defaults to ""
37 :param str musicbrainz_artistid: MusicBrainz IDs, defaults to ``None``
38 :param str musicbrainz_albumartistid: MusicBrainz IDs, defaults to ``None``
41 def __init__(self, file=None, time=0, pos=-1, **kwargs):
42 self.title = self.artist = self.album = self.albumartist = ''
43 self.musicbrainz_artistid = self.musicbrainz_albumartistid = None
50 self.__dict__.update(**kwargs)
51 self.tags_to_collapse = ['artist', 'album', 'title', 'date',
52 'genre', 'albumartist',
53 'musicbrainz_artistid',
54 'musicbrainz_albumartistid']
55 # have tags been collapsed?
56 self.collapsed_tags = list()
57 # Needed for multiple tags which returns a list instead of a string
60 def _collapse_tags(self):
62 Necessary to deal with tags defined multiple times.
63 These entries are set as lists instead of strings.
65 for tag, value in self.__dict__.items():
66 if tag not in self.tags_to_collapse:
68 if isinstance(value, list):
69 self.collapsed_tags.append(tag)
70 self.__dict__.update({tag: SEPARATOR.join(set(value))})
73 return '%s(artist="%s", album="%s", title="%s", file="%s")' % (
74 self.__class__.__name__,
82 return '{artist} - {album} - {title} ({length})'.format(
90 def __add__(self, other):
91 return Track(time=self.time + other.time)
93 def __sub__(self, other):
94 return Track(time=self.time - other.time)
97 if self.musicbrainz_artistid and self.title:
98 return hash(self.musicbrainz_artistid + self.title)
99 if self.artist and self.title:
100 return hash(self.artist + self.title)
102 return hash(self.file)
106 def __eq__(self, other):
107 return hash(self) == hash(other)
109 def __ne__(self, other):
110 return hash(self) != hash(other)
115 return not self._empty
119 """file is an immutable attribute that's used for the hash method"""
123 """get time property"""
126 def set_time(self, value):
127 """set time property"""
128 self._time = int(value)
130 time = property(get_time, set_time, doc='song duration in seconds (use :attr:`length` for human readable time)')
134 """Get a fancy duration as ``%H:%M:%S`` (use :attr:`time` to get duration in second only)"""
135 temps = time.gmtime(int(self.time)) #TODO: returns a date not a duration
140 return time.strftime(fmt, temps)
144 """Get the :class:`sima.lib.meta.Artist` associated to this track"""
146 if not self.musicbrainz_artistid:
147 return Artist(name='[unknown]',
148 mbid='125ec42a-7229-4250-afc5-e057484327fe')
149 return Artist(name='[unknown]', **self.__dict__)
150 return Artist(**self.__dict__)
153 # vim: ai ts=4 sw=4 sts=4 expandtab