[Python-checkins] python/dist/src/Lib/email _compat21.py,NONE,1.1 _compat22.py,NONE,1.1 Encoders.py,1.5,1.6 Iterators.py,1.6,1.7 Message.py,1.10,1.11 Utils.py,1.12,1.13 __init__.py,1.5,1.6
bwarsaw@sourceforge.net
bwarsaw@sourceforge.net
2002年5月19日 16:44:22 -0700
Update of /cvsroot/python/python/dist/src/Lib/email
In directory usw-pr-cvs1:/tmp/cvs-serv26214/Lib/email
Modified Files:
Encoders.py Iterators.py Message.py Utils.py __init__.py
Added Files:
_compat21.py _compat22.py
Log Message:
Complete a merge of the mimelib project and the Python cvs codebases
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.
--- NEW FILE: _compat21.py ---
# 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
# 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
# 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
--- NEW FILE: _compat22.py ---
# 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
# 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
# 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
Index: Encoders.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/email/Encoders.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** Encoders.py 10 Apr 2002 21:01:30 -0000 1.5
--- Encoders.py 19 May 2002 23:44:19 -0000 1.6
***************
*** 6,18 ****
import base64
- from quopri import encodestring as _encodestring
# Helpers
! def _qencode(s):
! enc = _encodestring(s, quotetabs=1)
! # Must encode spaces, which quopri.encodestring() doesn't do
! return enc.replace(' ', '=20')
--- 6,37 ----
import base64
# Helpers
! 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
Index: Iterators.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/email/Iterators.py,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** Iterators.py 10 Apr 2002 21:01:30 -0000 1.6
--- Iterators.py 19 May 2002 23:44:19 -0000 1.7
***************
*** 5,33 ****
"""
! from __future__ import generators
! from cStringIO import StringIO
! from types import StringType
!
!
!
! 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
!
!
!
! 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
--- 5,11 ----
"""
! 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: Message.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/email/Message.py,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** Message.py 10 Apr 2002 21:01:30 -0000 1.10
--- Message.py 19 May 2002 23:44:19 -0000 1.11
***************
*** 608,622 ****
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
def get_charsets(self, failobj=None):
--- 608,616 ----
self._headers = newheaders
! 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):
Index: Utils.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/email/Utils.py,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** Utils.py 15 Apr 2002 22:00:25 -0000 1.12
--- Utils.py 19 May 2002 23:44:19 -0000 1.13
***************
*** 22,26 ****
from rfc822 import parsedate_tz as _parsedate_tz
! from quopri import decodestring as _qdecode
import base64
--- 22,43 ----
from rfc822 import parsedate_tz as _parsedate_tz
! 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
Index: __init__.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/email/__init__.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** __init__.py 10 Apr 2002 21:01:30 -0000 1.5
--- __init__.py 19 May 2002 23:44:19 -0000 1.6
***************
*** 5,9 ****
"""
! __version__ = '2.0'
__all__ = ['Charset',
--- 5,9 ----
"""
! __version__ = '2.0.3'
__all__ = ['Charset',