]> kaliko git repositories - mpd-sima.git/blob - sima/lib/meta.py
simaecho get_toptrack set artist mbid if missing
[mpd-sima.git] / sima / lib / meta.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2013, 2014 Jack Kaliko <kaliko@azylum.org>
3 #
4 #  This file is part of sima
5 #
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.
10 #
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.
15 #
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/>.
18 #
19 #
20 """
21 Defines some object to handle audio file metadata
22 """
23
24 import logging
25 import re
26
27 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}$'
28
29 def is_uuid4(uuid):
30     regexp = re.compile(UUID_RE, re.IGNORECASE)
31     if regexp.match(uuid):
32         return True
33     raise WrongUUID4(uuid)
34
35 class MetaException(Exception):
36     """Generic Meta Exception"""
37     pass
38
39 class WrongUUID4(MetaException):
40     pass
41
42 def mbidfilter(func):
43     def wrapper(*args, **kwargs):
44         cls = args[0]
45         if not cls.use_mbid:
46             kwargs.pop('mbid', None)
47             kwargs.pop('musicbrainz_artistid', None)
48             kwargs.pop('musicbrainz_albumartistid', None)
49         func(*args, **kwargs)
50     return wrapper
51
52
53 class Meta:
54     """Generic Class for Meta object
55     Meta(name=<str>[, mbid=UUID4])
56     """
57     use_mbid = True
58
59     def __init__(self, **kwargs):
60         self.__name = None #TODO: should be immutable
61         self.__mbid = None
62         self.__aliases = set()
63         self.log = logging.getLogger(__name__)
64         if 'name' not in kwargs or not kwargs.get('name'):
65             raise MetaException('Need a "name" argument')
66         else:
67             self.__name = kwargs.pop('name')
68         if 'mbid' in kwargs and kwargs.get('mbid'):
69             try:
70                 is_uuid4(kwargs.get('mbid'))
71                 self.__mbid = kwargs.pop('mbid')
72             except WrongUUID4:
73                 self.log.warning('Wrong mbid {}:{}'.format(self.__name,
74                                                          kwargs.get('mbid')))
75             # mbid immutable as hash rests on
76         self.__dict__.update(**kwargs)
77
78     def __repr__(self):
79         fmt = '{0}(name={1.name!r}, mbid={1.mbid!r})'
80         return fmt.format(self.__class__.__name__, self)
81
82     def __str__(self):
83         return self.__name.__str__()
84
85     def __eq__(self, other):
86         """
87         Perform mbid equality test
88         """
89         #if hasattr(other, 'mbid'):  # better isinstance?
90         if isinstance(other, Meta) and self.mbid and other.mbid:
91             if self.mbid and other.mbid:
92                 return self.mbid == other.mbid
93         else:
94             return (other.__str__() == self.__str__() or
95                     other.__str__() in self.__aliases)
96         return False
97
98     def __hash__(self):
99         if self.mbid:
100             return hash(self.mbid)
101         return hash(self.__name)
102
103     def add_alias(self, other):
104         if getattr(other, '__str__', None):
105             if callable(other.__str__):
106                 self.__aliases |= {other.__str__()}
107         elif isinstance(other, Meta):
108             self.__aliases |= other.__aliases
109         else:
110             raise MetaException('No __str__ method found in {!r}'.format(other))
111
112     @property
113     def name(self):
114         return self.__name
115
116     @property
117     def mbid(self):
118         return self.__mbid
119
120     @property
121     def aliases(self):
122         return self.__aliases
123
124     @property
125     def names(self):
126         return self.__aliases | {self.__name,}
127
128
129 class Album(Meta):
130
131     @property
132     def album(self):
133         return self.name
134
135 class Artist(Meta):
136
137     @mbidfilter
138     def __init__(self, name=None, mbid=None, **kwargs):
139         """Artist object built from a mapping dict containing at least an
140         "artist" entry:
141             >>> trk = {'artist':'Art Name',
142             >>>        'albumartist': 'Alb Art Name',           # optional
143             >>>        'musicbrainz_artistid': '<UUID4>'    ,   # optional
144             >>>        'musicbrainz_albumartistid': '<UUID4>',  # optional
145             >>>       }
146             >>> artobj0 = Artist(**trk)
147             >>> artobj1 = Artist(name='Tool')
148         """
149         name = kwargs.get('artist', name)
150         mbid = kwargs.get('musicbrainz_artistid', mbid)
151         if (kwargs.get('albumartist', False) and
152                 kwargs.get('albumartist') != 'Various Artists'):
153             name = kwargs.get('albumartist').split(', ')[0]
154         if (kwargs.get('musicbrainz_albumartistid', False) and
155                 kwargs.get('musicbrainz_albumartistid') != '89ad4ac3-39f7-470e-963a-56509c546377'):
156             mbid = kwargs.get('musicbrainz_albumartistid').split(', ')[0]
157         super().__init__(name=name, mbid=mbid)
158
159 # VIM MODLINE
160 # vim: ai ts=4 sw=4 sts=4 expandtab