]> kaliko git repositories - mpd-sima.git/blob - sima/lib/album.py
Add unit test for Track module
[mpd-sima.git] / sima / lib / album.py
1 # -*- coding: utf-8 -*-
2
3 from .track import Track
4
5 class MetaException(Exception):
6     pass
7
8 class Meta:
9
10     def __init__(self, **kwargs):
11         self.name = None
12         self.mbid = None
13         if 'name' not in kwargs:
14             raise MetaException('need at least a "name" argument')
15         self.__dict__.update(kwargs)
16
17     def __repr__(self):
18         fmt = '{0}(name="{1.name}", mbid="{1.mbid}")'
19         return fmt.format(self.__class__.__name__, self)
20
21     def __str__(self):
22         return str(self.name)
23
24     def __hash__(self):
25         if self.mbid is not None:
26             return hash(self.mbid)
27         else:
28             return id(self)
29
30
31 class Album(Meta):
32     __hash__ = Meta.__hash__
33
34     def __init__(self, **kwargs):
35         super().__init__(**kwargs)
36
37     def __eq__(self, other):
38         """
39         Perform mbid equality test if present,
40         else fallback on self.name equality
41         """
42         if hasattr(other, 'mbid'):
43             if other.mbid and self.mbid:
44                 return self.mbid == other.mbid
45         return str(self) == str(other)
46
47     @property
48     def album(self):
49         return self.name
50
51 # vim: ai ts=4 sw=4 sts=4 expandtab