Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.038 X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; 'else:': 0.03; 'resulting': 0.04; '%r"': 0.16; '(other': 0.16; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'inverse': 0.16; 'order)': 0.16; 'preserved': 0.16; 'thanks,': 0.17; 'copied': 0.24; 'headers': 0.24; 'source': 0.25; 'gets': 0.27; 'points': 0.29; 'getting': 0.31; 'minor': 0.31; '(including': 0.33; "i'd": 0.34; 'something': 0.35; 'there': 0.35; 'charset:us-ascii': 0.36; 'handle': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'new': 0.61; 'making': 0.63; 'skip:n 10': 0.64; 'here': 0.66; 'obvious': 0.74; 'skip:n 40': 0.81; 'friends?': 0.84; 'received:50.22': 0.84; 'subject:Best': 0.91 Date: Wed, 3 Sep 2014 20:59:58 -0500 From: Tim Chase To: python-list@python.org Subject: Best way to filter parts of a email.message.Message X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409796109 news.xs4all.nl 2859 [2001:888:2000:d::a6]:39843 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77518 I'd like to do something like the following pseudocode existing_message = mailbox[key] # an email.message.Message new_message = email.message.Message() for part in existing_message.walk(): if passes_test(part): new_message.add(part) # need proper call here else: log("skipping %r" % part) or alternatively something like this pseudocode inverse new_message = copy.copy(existing_message) for part in new_message.walk(): if fails_test(part): new_message.magic_delete_part_method(part) However, I want to make sure that just the selected mime-parts get eliminated and that everything else (other mime-parts as well as headers) gets copied over. Points giving me minor fits: - mime-parts can be nested, so I need to recursively handle them - making sure that if the source is/isn't multipart, that the resulting new message is the same - ensuring that headers get preserved (including the order) Is there an obvious way to do this without rolling up my sleeves and getting into the source for email.message and friends? Thanks, -tkc