| 1 |
"""Tests the facility that lets plugins add custom field to MediaFile.""" |
| 2 |
|
| 3 |
import shutil |
| 4 |
|
| 5 |
import mediafile |
| 6 |
import pytest |
| 7 |
|
| 8 |
from beets.library import Item |
| 9 |
from beets.plugins import BeetsPlugin |
| 10 |
from beets.test import _common |
| 11 |
from beets.test.helper import BeetsTestCase |
| 12 |
from beets.util import syspath |
| 13 |
|
| 14 |
field_extension = mediafile.MediaField( |
| 15 |
mediafile.MP3DescStorageStyle("customtag"), |
| 16 |
mediafile.MP4StorageStyle("----:com.apple.iTunes:customtag"), |
| 17 |
mediafile.StorageStyle("customtag"), |
| 18 |
mediafile.ASFStorageStyle("customtag"), |
| 19 |
) |
| 20 |
|
| 21 |
list_field_extension = mediafile.ListMediaField( |
| 22 |
mediafile.MP3ListDescStorageStyle("customlisttag"), |
| 23 |
mediafile.MP4ListStorageStyle("----:com.apple.iTunes:customlisttag"), |
| 24 |
mediafile.ListStorageStyle("customlisttag"), |
| 25 |
mediafile.ASFStorageStyle("customlisttag"), |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
class ExtendedFieldTestMixin(BeetsTestCase): |
| 30 |
def _mediafile_fixture(self, name, extension="mp3"): |
| 31 |
name = f"{name}.{extension}" |
| 32 |
src = _common.RSRC / name |
| 33 |
target = self.temp_path / name |
| 34 |
shutil.copy(src, syspath(target)) |
| 35 |
return mediafile.MediaFile(target) |
| 36 |
|
| 37 |
def test_extended_field_write(self): |
| 38 |
plugin = BeetsPlugin() |
| 39 |
plugin.add_media_field("customtag", field_extension) |
| 40 |
|
| 41 |
try: |
| 42 |
mf = self._mediafile_fixture("empty") |
| 43 |
mf.customtag = "F#" |
| 44 |
mf.save() |
| 45 |
|
| 46 |
mf = mediafile.MediaFile(mf.path) |
| 47 |
assert mf.customtag == "F#" |
| 48 |
|
| 49 |
finally: |
| 50 |
delattr(mediafile.MediaFile, "customtag") |
| 51 |
Item._media_fields.remove("customtag") |
| 52 |
|
| 53 |
def test_extended_list_field_write(self): |
| 54 |
plugin = BeetsPlugin() |
| 55 |
plugin.add_media_field("customlisttag", list_field_extension) |
| 56 |
|
| 57 |
try: |
| 58 |
mf = self._mediafile_fixture("empty") |
| 59 |
mf.customlisttag = ["a", "b"] |
| 60 |
mf.save() |
| 61 |
|
| 62 |
mf = mediafile.MediaFile(mf.path) |
| 63 |
assert mf.customlisttag == ["a", "b"] |
| 64 |
|
| 65 |
finally: |
| 66 |
delattr(mediafile.MediaFile, "customlisttag") |
| 67 |
Item._media_fields.remove("customlisttag") |
| 68 |
|
| 69 |
def test_write_extended_tag_from_item(self): |
| 70 |
plugin = BeetsPlugin() |
| 71 |
plugin.add_media_field("customtag", field_extension) |
| 72 |
|
| 73 |
try: |
| 74 |
mf = self._mediafile_fixture("empty") |
| 75 |
assert mf.customtag is None |
| 76 |
|
| 77 |
item = Item(path=mf.path, customtag="Gb") |
| 78 |
item.write() |
| 79 |
mf = mediafile.MediaFile(mf.path) |
| 80 |
assert mf.customtag == "Gb" |
| 81 |
|
| 82 |
finally: |
| 83 |
delattr(mediafile.MediaFile, "customtag") |
| 84 |
Item._media_fields.remove("customtag") |
| 85 |
|
| 86 |
def test_read_flexible_attribute_from_file(self): |
| 87 |
plugin = BeetsPlugin() |
| 88 |
plugin.add_media_field("customtag", field_extension) |
| 89 |
|
| 90 |
try: |
| 91 |
mf = self._mediafile_fixture("empty") |
| 92 |
mf.update({"customtag": "F#"}) |
| 93 |
mf.save() |
| 94 |
|
| 95 |
item = Item.from_path(mf.path) |
| 96 |
assert item["customtag"] == "F#" |
| 97 |
|
| 98 |
finally: |
| 99 |
delattr(mediafile.MediaFile, "customtag") |
| 100 |
Item._media_fields.remove("customtag") |
| 101 |
|
| 102 |
def test_invalid_descriptor(self): |
| 103 |
with pytest.raises( |
| 104 |
ValueError, match="must be an instance of MediaField" |
| 105 |
): |
| 106 |
mediafile.MediaFile.add_field("somekey", True) |
| 107 |
|
| 108 |
def test_overwrite_property(self): |
| 109 |
with pytest.raises( |
| 110 |
ValueError, match='property "artist" already exists' |
| 111 |
): |
| 112 |
mediafile.MediaFile.add_field("artist", mediafile.MediaField()) |