1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2013, 2014 Jack Kaliko <kaliko@azylum.org>
4 # This file is part of sima
6 # sima is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # sima is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with sima. If not, see <http://www.gnu.org/licenses/>.
21 Defines some object to handle audio file metadata
24 from .simastr import SimaStr
25 from .track import Track
27 class MetaException(Exception):
28 """Generic Meta Exception"""
31 class NotSameArtist(MetaException):
36 """Generic Class for Meta object"""
38 def __init__(self, **kwargs):
41 if 'name' not in kwargs:
42 raise MetaException('need at least a "name" argument')
43 self.__dict__.update(kwargs)
46 fmt = '{0}(name="{1.name}", mbid="{1.mbid}")'
47 return fmt.format(self.__class__.__name__, self)
52 def __eq__(self, other):
54 Perform mbid equality test if present,
55 else fallback on fuzzy equality
57 if hasattr(other, 'mbid'):
58 if other.mbid and self.mbid:
59 return self.mbid == other.mbid
60 return SimaStr(str(self)) == SimaStr(str(other))
63 if self.mbid is not None:
64 return hash(self.mbid)
70 __hash__ = Meta.__hash__
72 def __init__(self, **kwargs):
73 super().__init__(**kwargs)
75 def __eq__(self, other):
77 Perform mbid equality test if present,
78 else fallback on self.name equality
80 if hasattr(other, 'mbid'):
81 if other.mbid and self.mbid:
82 return self.mbid == other.mbid
83 return str(self) == str(other)
92 def __init__(self, **kwargs):
94 super().__init__(**kwargs)
96 def append(self, name):
97 self._aliases.append(name)
101 return self._aliases + [self.name]
103 def __add__(self, other):
104 if isinstance(other, Artist):
105 if self.mbid == other.mbid:
106 res = Artist(**self.__dict__)
107 res._aliases.extend(other.names)
110 raise NotSameArtist('different mbids: {0} and {1}'.format(self, other))
113 class TrackMB(Track):
115 def __init__(self, **kwargs):
116 super().__init__(**kwargs)
117 if hasattr(self, 'musicbrainz_artistid'):
118 self.artist = Artist(mbid=self.musicbrainz_artistid,
121 # vim: ai ts=4 sw=4 sts=4 expandtab