From: kaliko Date: Thu, 12 Dec 2013 12:44:31 +0000 (+0100) Subject: Add basic musicbrainz ID implementation for Track X-Git-Tag: mpd-sima/0.12.0pr3~20 X-Git-Url: http://git.kaliko.me/?a=commitdiff_plain;h=927b9fcec374c8bca35abcdb666b05a8279facd3;p=mpd-sima.git Add basic musicbrainz ID implementation for Track --- diff --git a/sima/lib/meta.py b/sima/lib/meta.py new file mode 100644 index 0000000..6fb6a0e --- /dev/null +++ b/sima/lib/meta.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- + +from .simastr import SimaStr +from .track import Track + +class MetaException(Exception): + pass + +class NotSameArtist(MetaException): + pass + + +class Meta: + + def __init__(self, **kwargs): + self.name = None + self.mbid = None + if 'name' not in kwargs: + raise MetaException('need at least a "name" argument') + self.__dict__.update(kwargs) + + def __repr__(self): + fmt = '{0}(name="{1.name}", mbid="{1.mbid}")' + return fmt.format(self.__class__.__name__, self) + + def __str__(self): + return str(self.name) + + def __eq__(self, other): + """ + Perform mbid equality test if present, + else fallback on fuzzy equality + """ + if hasattr(other, 'mbid'): + if other.mbid and self.mbid: + return self.mbid == other.mbid + return SimaStr(str(self)) == SimaStr(str(other)) + + +class Artist(Meta): + + def __init__(self, **kwargs): + self._aliases = [] + super().__init__(**kwargs) + + def append(self, name): + self._aliases.append(name) + + @property + def names(self): + return self._aliases + [self.name] + + def __add__(self, other): + if isinstance(other, Artist): + if self.mbid == other.mbid: + res = Artist(**self.__dict__) + res._aliases.extend(other.names) + return res + else: + raise NotSameArtist('different mbids: {0} and {1}'.format(self, other)) + + +class TrackMB(Track): + + def __init__(self, **kwargs): + super().__init__(**kwargs) + if hasattr(self, 'musicbrainz_artistid'): + self.artist = Artist(mbid=self.musicbrainz_artistid, + name=self.artist) + +# vim: ai ts=4 sw=4 sts=4 expandtab +