Debian Codegraph @

mediafile/storage/flac.py

raw
1 import mutagen.flac
2
3 from mediafile.utils import Image
4
5 from .base import ListStorageStyle
6
7
8 class FlacImageStorageStyle(ListStorageStyle):
9 """Converts between ``mutagen.flac.Picture`` and ``Image`` instances."""
10
11 formats = ["FLAC"]
12
13 def __init__(self):
14 super().__init__(key="")
15
16 def fetch(self, mutagen_file):
17 return mutagen_file.pictures
18
19 def deserialize(self, flac_picture):
20 return Image(
21 data=flac_picture.data, desc=flac_picture.desc, type=flac_picture.type
22 )
23
24 def store(self, mutagen_file, pictures):
25 """``pictures`` is a list of mutagen.flac.Picture instances."""
26 mutagen_file.clear_pictures()
27 for pic in pictures:
28 mutagen_file.add_picture(pic)
29
30 def serialize(self, image):
31 """Turn a Image into a mutagen.flac.Picture."""
32 pic = mutagen.flac.Picture()
33 pic.data = image.data
34 pic.type = image.type_index
35 pic.mime = image.mime_type
36 pic.desc = image.desc or ""
37 return pic
38
39 def delete(self, mutagen_file):
40 """Remove all images from the file."""
41 mutagen_file.clear_pictures()