Friendly way to decode Envelope fields
Originally reported by: Vic Tor (Bitbucket: maxiimou)
Hello
I've been playing with imap and python, and use IMAPClient for little project. I'm trying to get out of encoding problems in Enveloppe structure. I wish I could have an easier way to access decoded string, here's what i have in mind.
output = parse_fetch_response([envelope_str])
subject = output[76920][b'ENVELOPE'].subject
assert subject == b'=?utf-8?B?VsOpbMO0VG91bG91c2U=?='
assert subject.decoded() == u'VélôToulouse'
One possible way to do that would be by subclassing the bytes type:
class imapbytes(bytes):
def decoded(self, imap_encoding='us-ascii'):
pass
with the following specifications so the current api is not broken:
somebytes = b"hey !"
someimapbytes = imapbytes(somebytes)
assert isinstance(somebytes, bytes)
assert not isinstance(somebytes, imapbytes)
assert isinstance(someimapbytes, bytes)
assert somebytes == someimapbytes
I forked your repo so i can test a bit further the idea. I'm not used to Mercurial so I couldn't push my changes on bitbuket but I managed to push them on github on the decode_imap_bytes branch. It seems to work the expected way on both python 2.7 and 3.4.
Could you tell me what you think of this improvement ? I wish I could contribute here =)
- Bitbucket: https://bitbucket.org/mjs0/imapclient/issue/152
Original comment by Vic Tor (Bitbucket: maxiimou):
Hello Menno, I'm sorry I didn't come back to you earlier. I did continue a bit this feature but I noticed some difficulty using it with (for example with an Envelope object when str method is called). I'm a bit busy for now but I'll explain my problem with few examples and unit test describing it better (some sample might already be visible on my github fork).
Original comment by Vic Tor (Bitbucket: maxiimou):
I was waiting for your feedbacks before going further. I'll look to your comments and will come back to you soon !
Original comment by Menno Smits (Bitbucket: mjs0):
Hi again. Sorry it's taken me so longer to come back to this.
Thanks so much for all your work on this. I like the overall idea and I think the implementation is heading in the right direction. I've made some comments against some of your commits on Github. If you can address those then I think this is pretty close to being OK for merging.
No problems with using git/Github instead of Mercurial. I can take care of merging it from there back into the Bitbucket repo.
Original comment by Menno Smits (Bitbucket: mjs0):
Hi. Thanks for this. I'm in the middle of busy work trip at the moment but will hopefully get back to you properly later in the week.
I'm interested in this issue as well. I have an email from Example, Véronique <[email protected]>, so envelope.from_[0].name looks like this:
b'=?UTF-8?Q?Example,_V=C3=A9ronique?='
Thus, I need to run it through decode_header (from email.header import decode_header)