From: kaliko Date: Mon, 11 May 2020 16:47:25 +0000 (+0200) Subject: Add Meta properties to expose serialized strings X-Git-Tag: 0.16.0.dev0~15 X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=c562e5def0073de216785ee57085658b0ee441b5;p=mpd-sima.git Add Meta properties to expose serialized strings --- diff --git a/sima/lib/meta.py b/sima/lib/meta.py index f79ed1d..b196584 100644 --- a/sima/lib/meta.py +++ b/sima/lib/meta.py @@ -58,6 +58,14 @@ def mbidfilter(func): 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: """ @@ -77,7 +85,7 @@ class Meta: def __init__(self, **kwargs): """Meta(name=[, 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__) @@ -142,6 +150,11 @@ class Meta: def name(self): return self.__name + @property + @serialize + def name_sz(self): + return self.name + @property def mbid(self): return self.__mbid @@ -150,19 +163,34 @@ class Meta: 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`. diff --git a/tests/test_meta.py b/tests/test_meta.py index a615a18..ed241cf 100644 --- a/tests/test_meta.py +++ b/tests/test_meta.py @@ -2,7 +2,7 @@ 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' @@ -102,6 +102,22 @@ class TestMetaObject(unittest.TestCase): 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):