]> granicus.if.org Git - python/commitdiff
Complete a merge of the mimelib project and the Python cvs codebases
authorBarry Warsaw <barry@python.org>
Sun, 19 May 2002 23:44:19 +0000 (23:44 +0000)
committerBarry Warsaw <barry@python.org>
Sun, 19 May 2002 23:44:19 +0000 (23:44 +0000)
for the email package.  The former is now just a shell project that
has some extra files for packaging for independent use (e.g. setup.py
and README).

Added a compatibility layer so that the same API can be used in Python
2.1 and 2.2/2.3 with the major differences shuffled off into helper
modules (_compat21.py and _compat22.py).

Also bumped the package version number to 2.0.3 for some fixes to be
checked in momentarily.

Lib/email/Encoders.py
Lib/email/Iterators.py
Lib/email/Message.py
Lib/email/Utils.py
Lib/email/__init__.py
Lib/email/_compat21.py [new file with mode: 0644]
Lib/email/_compat22.py [new file with mode: 0644]

index f09affac39986270414054656db24a22d3de0086..c369a24f23a141bf341809231b29c6d5c3736725 100644 (file)
@@ -5,15 +5,34 @@
 """
 
 import base64
-from quopri import encodestring as _encodestring
 
 
 \f
 # Helpers
-def _qencode(s):
-    enc = _encodestring(s, quotetabs=1)
-    # Must encode spaces, which quopri.encodestring() doesn't do
-    return enc.replace(' ', '=20')
+try:
+    from quopri import encodestring as _encodestring
+
+    def _qencode(s):
+        enc = _encodestring(s, quotetabs=1)
+        # Must encode spaces, which quopri.encodestring() doesn't do
+        return enc.replace(' ', '=20')
+except ImportError:
+    # Python 2.1 doesn't have quopri.encodestring()
+    from cStringIO import StringIO
+    import quopri as _quopri
+
+    def _qencode(s):
+        if not s:
+            return s
+        hasnewline = (s[-1] == '\n')
+        infp = StringIO(s)
+        outfp = StringIO()
+        _quopri.encode(infp, outfp, quotetabs=1)
+        # Python 2.x's encode() doesn't encode spaces even when quotetabs==1
+        value = outfp.getvalue().replace(' ', '=20')
+        if not hasnewline and value[-1] == '\n':
+            return value[:-1]
+        return value
 
 
 def _bencode(s):
index 515bac90796bc5011814e914622788f7df042c96..a6ddceb053d751c44cb5c72010418014672e0ff4 100644 (file)
@@ -4,30 +4,8 @@
 """Various types of useful iterators and generators.
 """
 
-from __future__ import generators
-from cStringIO import StringIO
-from types import StringType
-
-
-\f
-def body_line_iterator(msg):
-    """Iterate over the parts, returning string payloads line-by-line."""
-    for subpart in msg.walk():
-        payload = subpart.get_payload()
-        if type(payload) is StringType:
-            for line in StringIO(payload):
-                yield line
-
-
-\f
-def typed_subpart_iterator(msg, maintype='text', subtype=None):
-    """Iterate over the subparts with a given MIME type.
-
-    Use `maintype' as the main MIME type to match against; this defaults to
-    "text".  Optional `subtype' is the MIME subtype to match against; if
-    omitted, only the main type is matched.
-    """
-    for subpart in msg.walk():
-        if subpart.get_main_type('text') == maintype:
-            if subtype is None or subpart.get_subtype('plain') == subtype:
-                yield subpart
+try:
+    from email._compat22 import body_line_iterator, typed_subpart_iterator
+except SyntaxError:
+    # Python 2.1 doesn't have generators
+    from email._compat21 import body_line_iterator, typed_subpart_iterator
index 71d10c4af15a360f90ec0efdd2e6b626716379fa..6cb659cd762890bf1979f7044f142de543cd0d7d 100644 (file)
@@ -607,17 +607,11 @@ class Message:
                 newheaders.append((h, v))
         self._headers = newheaders
 
-    def walk(self):
-        """Walk over the message tree, yielding each subpart.
-
-        The walk is performed in depth-first order.  This method is a
-        generator.
-        """
-        yield self
-        if self.is_multipart():
-            for subpart in self.get_payload():
-                for subsubpart in subpart.walk():
-                    yield subsubpart
+    try:
+        from email._compat22 import walk
+    except SyntaxError:
+        # Must be using Python 2.1
+        from email._compat21 import walk
 
     def get_charsets(self, failobj=None):
         """Return a list containing the charset(s) used in this message.
index 927d67eccca22380f76f7c611861790b84893460..a2b6c8747e2ff2d22f3215e1f51c36e6e8c55d51 100644 (file)
@@ -21,7 +21,24 @@ from rfc822 import mktime_tz
 from rfc822 import parsedate as _parsedate
 from rfc822 import parsedate_tz as _parsedate_tz
 
-from quopri import decodestring as _qdecode
+try:
+    from quopri import decodestring as _qdecode
+except ImportError:
+    # Python 2.1 doesn't have quopri.decodestring()
+    def _qdecode(s):
+        import quopri as _quopri
+
+        if not s:
+            return s
+        hasnewline = (s[-1] == '\n')
+        infp = StringIO(s)
+        outfp = StringIO()
+        _quopri.decode(infp, outfp)
+        value = outfp.getvalue()
+        if not hasnewline and value[-1] =='\n':
+            return value[:-1]
+        return value
+
 import base64
 
 # Intrapackage imports
index f4a5b76359807bc9f971f92361faf5fc601e4df2..c29e7f7f73b7d24efda8c149b0a84755579acf35 100644 (file)
@@ -4,7 +4,7 @@
 """A package for parsing, handling, and generating email messages.
 """
 
-__version__ = '2.0'
+__version__ = '2.0.3'
 
 __all__ = ['Charset',
            'Encoders',
diff --git a/Lib/email/_compat21.py b/Lib/email/_compat21.py
new file mode 100644 (file)
index 0000000..932de48
--- /dev/null
@@ -0,0 +1,59 @@
+# Copyright (C) 2002 Python Software Foundation
+# Author: barry@zope.com
+
+"""Module containing compatibility functions for Python 2.1.
+"""
+
+from cStringIO import StringIO
+from types import StringType, UnicodeType
+
+
+\f
+# This function will become a method of the Message class
+def walk(self):
+    """Walk over the message tree, yielding each subpart.
+
+    The walk is performed in depth-first order.  This method is a
+    generator.
+    """
+    parts = []
+    parts.append(self)
+    if self.is_multipart():
+        for subpart in self.get_payload():
+            parts.extend(subpart.walk())
+    return parts
+
+
+# Used internally by the Header class
+def _intdiv2(i):
+    """Do an integer divide by 2."""
+    return i / 2
+
+
+\f
+# These two functions are imported into the Iterators.py interface module.
+# The Python 2.2 version uses generators for efficiency.
+def body_line_iterator(msg):
+    """Iterate over the parts, returning string payloads line-by-line."""
+    lines = []
+    for subpart in msg.walk():
+        payload = subpart.get_payload()
+        if isinstance(payload, StringType) or isinstance(payload, UnicodeType):
+            for line in StringIO(payload).readlines():
+                lines.append(line)
+    return lines
+
+
+def typed_subpart_iterator(msg, maintype='text', subtype=None):
+    """Iterate over the subparts with a given MIME type.
+
+    Use `maintype' as the main MIME type to match against; this defaults to
+    "text".  Optional `subtype' is the MIME subtype to match against; if
+    omitted, only the main type is matched.
+    """
+    parts = []
+    for subpart in msg.walk():
+        if subpart.get_main_type('text') == maintype:
+            if subtype is None or subpart.get_subtype('plain') == subtype:
+                parts.append(subpart)
+    return parts
diff --git a/Lib/email/_compat22.py b/Lib/email/_compat22.py
new file mode 100644 (file)
index 0000000..d505c96
--- /dev/null
@@ -0,0 +1,56 @@
+# Copyright (C) 2002 Python Software Foundation
+# Author: barry@zope.com
+
+"""Module containing compatibility functions for Python 2.1.
+"""
+
+from __future__ import generators
+from __future__ import division
+from cStringIO import StringIO
+from types import StringTypes
+
+
+\f
+# This function will become a method of the Message class
+def walk(self):
+    """Walk over the message tree, yielding each subpart.
+
+    The walk is performed in depth-first order.  This method is a
+    generator.
+    """
+    yield self
+    if self.is_multipart():
+        for subpart in self.get_payload():
+            for subsubpart in subpart.walk():
+                yield subsubpart
+
+
+# Used internally by the Header class
+def _intdiv2(i):
+    """Do an integer divide by 2."""
+    return i // 2
+
+
+\f
+# These two functions are imported into the Iterators.py interface module.
+# The Python 2.2 version uses generators for efficiency.
+def body_line_iterator(msg):
+    """Iterate over the parts, returning string payloads line-by-line."""
+    for subpart in msg.walk():
+        payload = subpart.get_payload()
+        if isinstance(payload, StringTypes):
+            for line in StringIO(payload):
+                yield line
+
+
+def typed_subpart_iterator(msg, maintype='text', subtype=None):
+    """Iterate over the subparts with a given MIME type.
+
+    Use `maintype' as the main MIME type to match against; this defaults to
+    "text".  Optional `subtype' is the MIME subtype to match against; if
+    omitted, only the main type is matched.
+    """
+    for subpart in msg.walk():
+        if subpart.get_main_type('text') == maintype:
+            if subtype is None or subpart.get_subtype('plain') == subtype:
+                yield subpart