Barry Warsaw | 8c1aac2 | 2002-05-19 23:44:19 +0000 | [diff] [blame] | 1 | # Copyright (C) 2002 Python Software Foundation |
| 2 | # Author: barry@zope.com |
| 3 | |
Barry Warsaw | cd7051f | 2003-03-26 17:57:25 +0000 | [diff] [blame] | 4 | """Module containing compatibility functions for Python 2.2. |
Barry Warsaw | 8c1aac2 | 2002-05-19 23:44:19 +0000 | [diff] [blame] | 5 | """ |
| 6 | |
| 7 | from __future__ import generators |
| 8 | from __future__ import division |
| 9 | from cStringIO import StringIO |
| 10 | from types import StringTypes |
| 11 | |
Barry Warsaw | 7ba256f | 2003-04-02 04:51:33 +0000 | [diff] [blame] | 12 | # Python 2.2.x where x < 1 lacks True/False |
Barry Warsaw | cd7051f | 2003-03-26 17:57:25 +0000 | [diff] [blame] | 13 | try: |
| 14 | True, False |
| 15 | except NameError: |
| 16 | True = 1 |
| 17 | False = 0 |
| 18 | |
Barry Warsaw | 8c1aac2 | 2002-05-19 23:44:19 +0000 | [diff] [blame] | 19 | |
| 20 | |
| 21 | # This function will become a method of the Message class |
| 22 | def walk(self): |
| 23 | """Walk over the message tree, yielding each subpart. |
| 24 | |
| 25 | The walk is performed in depth-first order. This method is a |
| 26 | generator. |
| 27 | """ |
| 28 | yield self |
| 29 | if self.is_multipart(): |
| 30 | for subpart in self.get_payload(): |
| 31 | for subsubpart in subpart.walk(): |
| 32 | yield subsubpart |
| 33 | |
| 34 | |
Barry Warsaw | ff49279 | 2002-06-02 18:59:06 +0000 | [diff] [blame] | 35 | # Python 2.2 spells floor division // |
| 36 | def _floordiv(i, j): |
| 37 | """Do a floor division, i/j.""" |
| 38 | return i // j |
Barry Warsaw | 8c1aac2 | 2002-05-19 23:44:19 +0000 | [diff] [blame] | 39 | |
| 40 | |
Barry Warsaw | 356afac | 2002-09-10 16:09:06 +0000 | [diff] [blame] | 41 | def _isstring(obj): |
| 42 | return isinstance(obj, StringTypes) |
| 43 | |
| 44 | |
Barry Warsaw | 8c1aac2 | 2002-05-19 23:44:19 +0000 | [diff] [blame] | 45 | |
| 46 | # These two functions are imported into the Iterators.py interface module. |
| 47 | # The Python 2.2 version uses generators for efficiency. |
Barry Warsaw | 12dc230 | 2003-03-11 04:41:35 +0000 | [diff] [blame] | 48 | def body_line_iterator(msg, decode=False): |
| 49 | """Iterate over the parts, returning string payloads line-by-line. |
| 50 | |
| 51 | Optional decode (default False) is passed through to .get_payload(). |
| 52 | """ |
Barry Warsaw | 8c1aac2 | 2002-05-19 23:44:19 +0000 | [diff] [blame] | 53 | for subpart in msg.walk(): |
Barry Warsaw | 12dc230 | 2003-03-11 04:41:35 +0000 | [diff] [blame] | 54 | payload = subpart.get_payload(decode=decode) |
Barry Warsaw | 356afac | 2002-09-10 16:09:06 +0000 | [diff] [blame] | 55 | if _isstring(payload): |
Barry Warsaw | 8c1aac2 | 2002-05-19 23:44:19 +0000 | [diff] [blame] | 56 | for line in StringIO(payload): |
| 57 | yield line |
| 58 | |
| 59 | |
| 60 | def typed_subpart_iterator(msg, maintype='text', subtype=None): |
| 61 | """Iterate over the subparts with a given MIME type. |
| 62 | |
| 63 | Use `maintype' as the main MIME type to match against; this defaults to |
| 64 | "text". Optional `subtype' is the MIME subtype to match against; if |
| 65 | omitted, only the main type is matched. |
| 66 | """ |
| 67 | for subpart in msg.walk(): |
Barry Warsaw | cd7051f | 2003-03-26 17:57:25 +0000 | [diff] [blame] | 68 | if subpart.get_content_maintype() == maintype: |
| 69 | if subtype is None or subpart.get_content_subtype() == subtype: |
Barry Warsaw | 8c1aac2 | 2002-05-19 23:44:19 +0000 | [diff] [blame] | 70 | yield subpart |