Browse Source

_tag_interface.OggOpus: implement get/set_comment

Fabian Peter Hammerle 4 years ago
parent
commit
0cf364aa19

+ 19 - 4
symuid/_tag_interface.py

@@ -1,5 +1,6 @@
 import abc
 import re
+import typing
 
 import mutagen.id3
 import mutagen.mp4
@@ -212,16 +213,30 @@ class MP4(_MutagenTagInterface):
 
 class OggOpus(_MutagenTagInterface):
 
+    # https://github.com/cmus/cmus/blob/9a0723f7a90dc7de0898be87963d5105a999aa6c/ip/opus.c#L229
+    # https://github.com/cmus/cmus/blob/17bf542c6b120d9dcf6642b259d78badfc1143eb/comment.c#L224
+    _COMMENT_TAG = 'comment'
+
     def __init__(self, mutagen_file):
         assert isinstance(mutagen_file.tags, mutagen.oggopus.OggOpusVComment), \
             (mutagen_file, mutagen_file.tags)
         super().__init__(mutagen_file)
 
-    def get_comment(self):
-        raise NotImplementedError()
+    def _get_single_text(self, tag_label) -> typing.Optional[str]:
+        tag = self._mutagen_file.get(tag_label, None)  # type: list
+        if tag is None:
+            return None
+        if len(tag) > 1:
+            raise ValueError((self.track_path, tag))
+        if not isinstance(tag[0], str):
+            raise ValueError((self.track_path, tag))
+        return tag[0]
 
-    def set_comment(self, comment):
-        raise NotImplementedError()
+    def get_comment(self) -> typing.Optional[str]:
+        return self._get_single_text(self._COMMENT_TAG)
+
+    def set_comment(self, comment: str) -> None:
+        self._mutagen_file[self._COMMENT_TAG] = comment
 
     def get_track_uuid(self):
         raise NotImplementedError()

+ 10 - 0
tests/conftest.py

@@ -19,3 +19,13 @@ def empty_id3_path(tmpdir, tracks_dir_path):
         dst=path,
     )
     return path
+
+
+@pytest.fixture
+def empty_ogg_opus_path(tmpdir, tracks_dir_path):
+    path = tmpdir.join('empty.opus').strpath
+    shutil.copyfile(
+        src=os.path.join(tracks_dir_path, 'ogg-opus-empty.opus'),
+        dst=path,
+    )
+    return path

+ 24 - 0
tests/tag_interface/test_ogg_vorbis.py

@@ -13,3 +13,27 @@ def test_get_track_path(tracks_dir_path, track_name):
     track_path = os.path.join(tracks_dir_path, track_name)
     iface = OggOpus(mutagen.File(track_path))
     assert track_path == iface.track_path
+
+
+@pytest.mark.parametrize(('track_name', 'expected_comment'), [
+    ('ogg-opus-empty.opus', None),
+    ('ogg-opus-typical.opus', 'some comment'),
+])
+def test_get_comment(tracks_dir_path, track_name, expected_comment):
+    iface = OggOpus(mutagen.File(os.path.join(tracks_dir_path, track_name)))
+    assert expected_comment == iface.get_comment()
+
+
+def test_set_comment(empty_ogg_opus_path):
+    iface = OggOpus(mutagen.File(empty_ogg_opus_path))
+    assert iface.get_comment() is None
+    iface.set_comment('latin')
+    assert iface.get_comment() == 'latin'
+    iface.set_comment('你好')
+    assert iface.get_comment() == '你好'
+    iface.save()
+    iface_reread = OggOpus(mutagen.File(empty_ogg_opus_path))
+    assert iface_reread.get_comment() == '你好'
+    tags = mutagen.File(iface.track_path).tags
+    assert len(tags) == 1
+    assert tags.items()[0] == ('comment', ['你好'])

BIN
tests/tracks/ogg-opus-typical.opus