Re: API thoughts

Barry Warsaw <[email protected]> Tue, 8 Mar 2011 12:10:51 -0500
Newsgroups gmane.comp.python.mime.devel
Organization Damn Crazy Followers of the Horn
Message-ID <[email protected]>
On Mar 08, 2011, at 03:32 PM, Steffen Daode Nurpmeso wrote:

>msg[f] is indeed and really an elegant and understand-at-a-glance 
>way to access headers. 
>(Possible restriction: it would be graceful if it would return and 
>take a list.)

Actually, I disagree. :)  From experience, look at .get_payload().  It tries
to manage both scalar payloads and list payloads (for multiparts), and it
sucks.  In hindsight (and email6) I hope that .get_payload() will be split
into separate API methods, one for simple payloads like image or audio data,
and another for multipart access.

So for headers, I think setitem/getitem/delitem should be reserved for simple
manipulation with well defined semantics (as it currently is <wink>), and new
API methods should be added for full access to headers when multiple ones are
present.

>... and give me a way to also delete just one body of a field and 
>i'll be lucky. 

That's a good idea too.

>Maybe simply 'Message._headers = {normalized_field = [bodies]}'? 

I'm not sure what that means, but yeah, you definitely don't want to be
messing with that private attribute.

>But, why not .delete_all_of(0, 2, 5), realized by a walk in equal 
>spirit to .get_all().
>
>(My thought was that a new Proxy class can be added very easily, 
>requiring only one new method in Message and 
>without affecting the remaining interface, 
>whatever status David's local EMAIL 6 branch is currently in and 
>whatever approach he will have chosen in the end.

It's an interesting idea.  Why don't you flesh that out and propose something
concrete, with a working implementation if possible?

Anyway, rewriting headers is not that hard:

#! /usr/bin/env python3
from email import message_from_string as mfs

msg = mfs("""\
From: [email protected]
X-Header: aardvark
To: [email protected]
X-Header: beaver
Subject: foo
X-Header: cougar
X-Header: dingo

""")

def yummy_toppings():
    for topping in ('duck', 'cheese', 'black olive', 'anchovy'):
        yield topping
toppings = yummy_toppings()


new_headers = []

for header, value in msg.items():
    if header.lower() == 'x-header':
        new_headers.append(('X-Header', toppings.__next__()))
    else:
        new_headers.append((header, value))


for header in msg:
    del msg[header]

for header, value in new_headers:
    msg[header] = value

print(msg)

Cheers,
-Barry
signature.asc (application/pgp-signature, 836 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iQIcBAEBCAAGBQJNdmMbAAoJEBJutWOnSwa/h6IP/1zoQYRZff5bSb6glWXOiFkx
vcq8cuynXak2A5vBLVYSRUZ1zmiktqXKl24YLhhy/OFnbazV84dVC+TGJ3kv5cdY
AJ8MjUmgjZCa4j2k7LWbMwidb0ksN/26CU/S/x/A7a8fcWvcLhagTpYHWfDjjA/L
5CIn5mw/DKolfcuOc2SPf/GQ7dbYT3/08eWHRCQJHWHTxiIa78fjFkuTmINeKGtm
BQ70i785sBCB4yvSO/kF16SiGFcj+SAebtVnyEovCRsyfZ+mhr1dhn7IrJ7CjHzO
nDpdtTuLvNDFsV9QmLv1ya7Jq1R3FhLIctzKp8TT6AnLvKtNHMMTb9S5ukatvgtl
Pij3wxxiB9StPcNsugc/n0OnUhxo/Em0Iu7FUbNsfADQAbKJTmY7wF4wP0sqknZK
srsV2WYsS+khDfSDsgKY9af8SSmd05GfA72Gsjb8MXZBNuqvi1UcW3nx+Eg31cd1
2/Q+DolSe+ZhvWqRDfT+v4eb2A7eozQBvdHU2a9YLLqyqtLpdoJu2T5Qf/o6VamJ
7Iv0bEWhvHBabUtho4/NGg7h5NwaiQdg3thA6r085SmPgQm5DyadMYgMXKwuOzrH
4Ccjk3BFxuPXrgOxrj6loUAlCdNTqVE97pZFGtmJlqhfUL9iqrjfSQRk4FriK1r9
QDTvHvJeVICrwYZSmL2p
=vRSK
-----END PGP SIGNATURE-----