]> kaliko git repositories - mpd-sima.git/commitdiff
Add basic musicbrainz ID implementation for Track
authorkaliko <efrim@azylum.org>
Thu, 12 Dec 2013 12:44:31 +0000 (13:44 +0100)
committerkaliko <efrim@azylum.org>
Mon, 27 Jan 2014 10:14:41 +0000 (11:14 +0100)
sima/lib/meta.py [new file with mode: 0644]

diff --git a/sima/lib/meta.py b/sima/lib/meta.py
new file mode 100644 (file)
index 0000000..6fb6a0e
--- /dev/null
@@ -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
+