Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Allow passing encoding to EasyID3.RegisterTXXXKey #263
Now the default encoding when you register a custom text field in EasyID3 is Latin1, except if the value you set for the first time requires UTF8.
This leads to an error when you first set the field with a non UTF8 value and then later you want to update the field with a UTF8 value. This is because the frame was added with latin1 encoding at the first time.
I guess there could be reasons to keep the current way working, but I think that developers should be able to specify the encoding of the new frame.
I think passing an optional parameter to the method should be enough to achieve that. If no encoding is passed old way will be used.
I already implemented it an it would be ready to push if you want.
Thats how the method will look:
@classmethod def RegisterTXXXKey(cls, key, desc, enc=None): """Register a user-defined text frame key. Some ID3 tags are stored in TXXX frames, which allow a freeform 'description' which acts as a subkey, e.g. TXXX:BARCODE.:: EasyID3.RegisterTXXXKey('barcode', 'BARCODE'). """ frameid = "TXXX:" + desc encoding = enc def getter(id3, key): return list(id3[frameid]) def setter(id3, key, value): try: frame = id3[frameid] except KeyError: if not encoding: _enc = 0 # Store 8859-1 if we can, per MusicBrainz spec. for v in value: if v and max(v) > u'\x7f': _enc = 3 break else: _enc = encoding id3.add(mutagen.id3.TXXX(encoding=_enc, text=value, desc=desc)) else: frame.text = value def deleter(id3, key): del(id3[frameid]) cls.RegisterKey(key, getter, setter, deleter)
Regards and thanks for your great work.