]> kaliko git repositories - mpd-sima.git/blob - sima/lib/meta.py
Add basic musicbrainz ID implementation for Track
[mpd-sima.git] / sima / lib / meta.py
1 # -*- coding: utf-8 -*-
2
3 from .simastr import SimaStr
4 from .track import Track
5
6 class MetaException(Exception):
7     pass
8
9 class NotSameArtist(MetaException):
10     pass
11
12
13 class Meta:
14
15     def __init__(self, **kwargs):
16         self.name = None
17         self.mbid = None
18         if 'name' not in kwargs:
19             raise MetaException('need at least a "name" argument')
20         self.__dict__.update(kwargs)
21
22     def __repr__(self):
23         fmt = '{0}(name="{1.name}", mbid="{1.mbid}")'
24         return fmt.format(self.__class__.__name__, self)
25
26     def __str__(self):
27         return str(self.name)
28
29     def __eq__(self, other):
30         """
31         Perform mbid equality test if present,
32         else fallback on fuzzy equality
33         """
34         if hasattr(other, 'mbid'):
35             if other.mbid and self.mbid:
36                 return self.mbid == other.mbid
37         return SimaStr(str(self)) == SimaStr(str(other))
38
39
40 class Artist(Meta):
41
42     def __init__(self, **kwargs):
43         self._aliases = []
44         super().__init__(**kwargs)
45
46     def append(self, name):
47         self._aliases.append(name)
48
49     @property
50     def names(self):
51         return self._aliases + [self.name]
52
53     def __add__(self, other):
54         if isinstance(other, Artist):
55             if self.mbid == other.mbid:
56                 res = Artist(**self.__dict__)
57                 res._aliases.extend(other.names)
58                 return res
59             else:
60                 raise NotSameArtist('different mbids: {0} and {1}'.format(self, other))
61
62
63 class TrackMB(Track):
64
65     def __init__(self, **kwargs):
66         super().__init__(**kwargs)
67         if hasattr(self, 'musicbrainz_artistid'):
68             self.artist = Artist(mbid=self.musicbrainz_artistid,
69                                  name=self.artist)
70
71 # vim: ai ts=4 sw=4 sts=4 expandtab
72