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: aper...@example.com
X-Header: aardvark
To: bper...@example.com
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

Attachment: signature.asc
Description: PGP signature

_______________________________________________
Email-SIG mailing list
Email-SIG@python.org
Your options: 
http://mail.python.org/mailman/options/email-sig/archive%40mail-archive.com

Reply via email to