func(*args, **kwargs)
return wrapper
+def serialize(func):
+ def wrapper(*args, **kwargs):
+ ans = func(*args, **kwargs)
+ if isinstance(ans, set):
+ return {s.replace("'", r"\'") for s in ans}
+ return ans.replace("'", r"\'")
+ return wrapper
+
class Meta:
"""
def __init__(self, **kwargs):
"""Meta(name=<str>[, mbid=UUID4])"""
- self.__name = None #TODO: should be immutable
+ self.__name = None # TODO: should be immutable
self.__mbid = None
self.__aliases = set()
self.log = logging.getLogger(__name__)
def name(self):
return self.__name
+ @property
+ @serialize
+ def name_sz(self):
+ return self.name
+
@property
def mbid(self):
return self.__mbid
def aliases(self):
return self.__aliases
+ @property
+ @serialize
+ def aliases_sz(self):
+ return self.aliases
+
@property
def names(self):
"""aliases + name"""
return self.__aliases | {self.__name,}
+ @property
+ @serialize
+ def names_sz(self):
+ return self.names
+
class Album(Meta):
"""Album object"""
+ @mbidfilter
+ def __init__(self, name=None, mbid=None, **kwargs):
+ super().__init__(name=name, mbid=mbid, **kwargs)
+
@property
def album(self):
return self.name
+
class Artist(Meta):
"""Artist object deriving from :class:`Meta`.
import unittest
-from sima.lib.meta import Meta, Artist, MetaContainer, is_uuid4
+from sima.lib.meta import Meta, Artist, Album, MetaContainer, is_uuid4
from sima.lib.meta import MetaException, SEPARATOR
VALID = '110e8100-e29b-41d1-a716-116655250000'
art10._Meta__mbid = None
self.assertTrue(art01 == art10, 'wrong: %r != %r' % (art00, art01))
+ def test_mpd_serialization(self):
+ """Controls serialization of names"""
+ name = "Heaven's Door"
+ heavens_door = Meta(name=name)
+ target = r"Heaven\'s Door"
+ self.assertEqual(heavens_door.name_sz, target)
+ self.assertEqual(heavens_door.name, name)
+ self.assertEqual(heavens_door.names_sz, {target})
+ heavens_door.add_alias(name+" LP")
+ self.assertEqual(heavens_door.aliases_sz, {target+" LP"})
+ # Controls inheritance
+ heavens_door = Album(name=name)
+ self.assertEqual(heavens_door.name_sz, target)
+ heavens_door = Artist(name=name)
+ self.assertEqual(heavens_door.name_sz, target)
+
class TestArtistObject(unittest.TestCase):