Browse Source

implemented setter for Track.comment (ID3 only)

Fabian Peter Hammerle 5 years ago
parent
commit
ed5601b99d
4 changed files with 58 additions and 0 deletions
  1. 5 0
      symuid/__init__.py
  2. 12 0
      symuid/tag_interface.py
  3. 32 0
      tests/tag_interface/id3.py
  4. 9 0
      tests/test_track.py

+ 5 - 0
symuid/__init__.py

@@ -65,6 +65,11 @@ class Track:
     def comment(self):
         return self._iface.get_comment()
 
+    @comment.setter
+    def comment(self, comment):
+        self._iface.set_comment(comment)
+        self._iface.save()
+
     def get_uuid(self):
         return self._iface.get_track_uuid()
 

+ 12 - 0
symuid/tag_interface.py

@@ -58,6 +58,15 @@ class ID3(_mutagen):
     def get_comment(self):
         return self._get_single_text('COMM::eng')
 
+    def set_comment(self, comment):
+        tag = mutagen.id3.COMM(
+            encoding=mutagen.id3.Encoding.UTF8,
+            lang='eng',
+            text=[comment],
+        )
+        self._mutagen_file.tags.add(tag)
+        return tag
+
     def get_track_uuid(self):
         for ufid in self._mutagen_file.tags.getall('UFID'):
             if ufid.owner == self._UFID_OWNER_ID:
@@ -149,6 +158,9 @@ class MP4(_mutagen):
     def get_comment(self):
         return self._get_single('\xa9cmt')
 
+    def set_comment(self, comment):
+        raise NotImplemented()
+
     def get_track_uuid(self):
         return self._get_free_uuid(self._UUID_TAG_KEY)
 

+ 32 - 0
tests/tag_interface/id3.py

@@ -7,6 +7,23 @@ import os
 import shutil
 
 
+@pytest.fixture
+def empty_id3_iface(tmpdir, tracks_dir_path):
+    p = tmpdir.join('empty.mp3').strpath
+    shutil.copyfile(
+        src=os.path.join(tracks_dir_path, 'id3v2.4-empty.mp3'),
+        dst=p,
+    )
+    return ID3(mutagen.File(p))
+
+
+@pytest.mark.parametrize('track_name', ['id3v2.4-empty.mp3'])
+def test_get_track_path(tracks_dir_path, track_name):
+    track_path = os.path.join(tracks_dir_path, track_name)
+    iface = ID3(mutagen.File(track_path))
+    assert track_path == iface.track_path
+
+
 @pytest.mark.parametrize(('track_name', 'tag_label', 'expected_text'), [
     ('id3v2.4-empty.mp3', 'TPE1', None),
     ('id3v2.4-typical.mp3', 'TPE1', 'some artist'),
@@ -25,3 +42,18 @@ def test__get_single_text(tracks_dir_path, track_name, tag_label, expected_text)
 def test_get_comment(tracks_dir_path, track_name, expected_comment):
     iface = ID3(mutagen.File(os.path.join(tracks_dir_path, track_name)))
     assert expected_comment == iface.get_comment()
+
+
+def test_set_comment(empty_id3_iface):
+    assert None == empty_id3_iface.get_comment()
+    empty_id3_iface.set_comment('latin')
+    assert 'latin' == empty_id3_iface.get_comment()
+    empty_id3_iface.set_comment('你好')
+    assert '你好' == empty_id3_iface.get_comment()
+    empty_id3_iface.save()
+    tags = mutagen.File(empty_id3_iface.track_path).tags
+    assert len(tags) == 1
+    tag = tags.values()[0]
+    assert isinstance(tag, mutagen.id3.COMM)
+    assert tag.lang == 'eng'
+    assert tag.text == ['你好']

+ 9 - 0
tests/test_track.py

@@ -43,6 +43,15 @@ def test_get_comment(path, expected_comment):
     assert expected_comment == t.comment
 
 
+def test_set_comment(empty_id3_track):
+    assert None == empty_id3_track.comment
+    empty_id3_track.comment = 'note'
+    assert 'note' == empty_id3_track.comment
+    empty_id3_track.comment += 's'
+    assert 'notes' == empty_id3_track.comment
+    assert 'notes' == symuid.Track(empty_id3_track.path).comment
+
+
 @pytest.mark.parametrize(('pc'), [
     symuid.PlayCount('pytest', 'lib', utc_dt(), 7),
 ])