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
26 UUID_RE = r'^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}$'
29 regexp = re.compile(UUID_RE, re.IGNORECASE)
30 if regexp.match(uuid):
32 raise WrongUUID4(uuid)
34 class MetaException(Exception):
35 """Generic Meta Exception"""
38 class WrongUUID4(MetaException):
43 """Generic Class for Meta object
44 Meta(name=<str>[, mbid=UUID4])
47 def __init__(self, **kwargs):
50 if 'name' not in kwargs or not kwargs.get('name'):
51 raise MetaException('Need a "name" argument')
52 if 'mbid' in kwargs and kwargs.get('mbid'):
53 is_uuid4(kwargs.get('mbid'))
54 # mbid immutable as hash rests on
55 self.__mbid = kwargs.pop('mbid')
56 self.__dict__.update(**kwargs)
59 fmt = '{0}(name={1.name!r}, mbid={1.mbid!r})'
60 return fmt.format(self.__class__.__name__, self)
63 return self.name.__str__()
65 def __eq__(self, other):
67 Perform mbid equality test
69 #if hasattr(other, 'mbid'): # better isinstance?
70 if isinstance(other, Meta) and self.mbid and other.mbid:
71 if self.mbid and other.mbid:
72 return self.mbid == other.mbid
74 return other.__str__() == self.__str__()
79 return hash(self.mbid)
95 def __init__(self, name=None, **kwargs):
96 """Artist object built from a mapping dict containing at least an
98 >>> trk = {'artist':'Art Name',
99 >>> 'albumartist': 'Alb Art Name', # optional
100 >>> 'musicbrainz_artistid': '<UUID4>' , # optional
101 >>> 'musicbrainz_albumartistid': '<UUID4>', # optional
103 >>> artobj0 = Artist(**trk)
104 >>> artobj1 = Artist(name='Tool')
106 self.__aliases = set()
107 name = kwargs.get('artist', name)
108 mbid = kwargs.get('musicbrainz_artistid', None)
109 if (kwargs.get('albumartist', False) and
110 kwargs.get('albumartist') != 'Various Artists'):
111 name = kwargs.get('albumartist').split(', ')[0]
112 if (kwargs.get('musicbrainz_albumartistid', False) and
113 kwargs.get('musicbrainz_albumartistid') != '89ad4ac3-39f7-470e-963a-56509c546377'):
114 mbid = kwargs.get('musicbrainz_albumartistid').split(', ')[0]
115 super().__init__(name=name, mbid=mbid)
117 def add_alias(self, other):
118 if getattr(other, '__str__', None):
119 if callable(other.__str__):
120 self.__aliases |= {other.__str__()}
121 elif isinstance(other, Artist):
122 self.__aliases |= other._Artist__aliases
124 raise MetaException('No __str__ method found in {!r}'.format(other))
128 return self.__aliases | {self.name,}
131 # vim: ai ts=4 sw=4 sts=4 expandtab