1 # Simpler (but far more limited) API for ID3 editing
2 # Copyright 2006 Joe Wreschnig
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of version 2 of the GNU General Public License as
6 # published by the Free Software Foundation.
8 # $Id: id3.py 3086 2006-04-04 02:13:21Z piman $
10 """Easier access to ID3 tags.
12 EasyID3 is a wrapper around mutagen.id3.ID3 to make ID3 tags appear
13 more like Vorbis or APEv2 tags.
17 from mutagen import Metadata
18 from mutagen._util import DictMixin
19 from mutagen.id3 import ID3, error, delete
21 __all__ = ['EasyID3', 'Open', 'delete']
23 class EasyID3(DictMixin, Metadata):
24 """A file with an ID3 tag.
26 Like Vorbis comments, EasyID3 keys are case-insensitive ASCII
27 values. Only a subset of ID3 frames (those with simple text keys)
28 are supported; EasyID3.valid_keys maps human-readable EasyID3
29 names to ID3 frame IDs.
31 To use an EasyID3 class with mutagen.mp3.MP3:
32 from mutagen.mp3 import MP3
33 from mutagen.easyid3 import EasyID3
34 MP3(filename, ID3=EasyID3)
46 "tracknumber": "TRCK",
48 """Valid keys for EasyID3 instances."""
50 def __init__(self, filename=None):
52 self.load = self.__id3.load
53 self.save = self.__id3.save
54 self.delete = self.__id3.delete
55 if filename is not None:
58 filename = property(lambda s: s.__id3.filename,
59 lambda s, fn: setattr(s.__id3, 'filename', fn))
61 _size = property(lambda s: s._id3.size,
62 lambda s, fn: setattr(s.__id3, '_size', fn))
64 def __TCON_get(self, frame):
67 def __TCON_set(self, frame, value):
69 if not isinstance(value, list):
73 def __TDRC_get(self, frame):
74 return [stamp.text for stamp in frame.text]
76 def __TDRC_set(self, frame, value):
77 self.__id3.add(mutagen.id3.TDRC(encoding=3, text=value))
79 def __text_get(self, frame):
82 def __text_set(self, frame, value):
84 if not isinstance(value, list):
88 def __getitem__(self, key):
90 if key in self.valid_keys:
91 frame = self.valid_keys[key]
92 getter = self.__mungers.get(frame, self.__default)[0]
93 return getter(self, self.__id3[frame])
94 else: raise ValueError("%r is not a valid key" % key)
96 def __setitem__(self, key, value):
98 if key in self.valid_keys:
99 frame = self.valid_keys[key]
100 setter = self.__mungers.get(frame, self.__default)[1]
101 if frame not in self.__id3:
102 frame = mutagen.id3.Frames[frame](encoding=3, text=value)
103 self.__id3.loaded_frame(frame)
105 setter(self, self.__id3[frame], value)
106 else: raise ValueError("%r is not a valid key" % key)
108 def __delitem__(self, key):
110 if key in self.valid_keys:
111 del(self.__id3[self.valid_keys[key]])
112 else: raise ValueError("%r is not a valid key" % key)
115 return [k for (k, v) in self.valid_keys.items() if v in self.__id3]
118 """Print tag key=value pairs."""
120 for key in self.keys():
123 strings.append("%s=%s" % (key, value))
124 return "\n".join(strings)
127 "TCON": (__TCON_get, __TCON_set),
128 "TDRC": (__TDRC_get, __TDRC_set),
131 __default = (__text_get, __text_set)