[Python-checkins] python/dist/src/Lib/email Header.py,1.7,1.8
bwarsaw@users.sourceforge.net
bwarsaw@users.sourceforge.net
Tue, 09 Jul 2002 09:33:50 -0700
Update of /cvsroot/python/python/dist/src/Lib/email
In directory usw-pr-cvs1:/tmp/cvs-serv3358
Modified Files:
Header.py
Log Message:
make_header(): New function to take the output of decode_header() and
create a Header instance. Closes feature request #539481.
Header.__init__(): Allow the initial string to be omitted.
__eq__(), __ne__(): Support rich comparisons for equality of Header
instances withy Header instances or strings.
Also, update a bunch of docstrings.
Index: Header.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/email/Header.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** Header.py 3 Jul 2002 05:04:04 -0000 1.7
--- Header.py 9 Jul 2002 16:33:47 -0000 1.8
***************
*** 94,105 ****
class Header:
! def __init__(self, s, charset=None, maxlinelen=None, header_name=None,
continuation_ws=' '):
"""Create a MIME-compliant header that can contain many languages.
! Specify the initial header value in s. Specify its character set as a
! Charset object in the charset argument. If None, a default Charset
! instance will be used.
You can later append to the header with append(s, charset) below;
--- 94,132 ----
+ def make_header(decoded_seq, maxlinelen=None, header_name=None,
+ continuation_ws=' '):
+ """Create a Header from a sequence of pairs as returned by decode_header()
+
+ decode_header() takes a header value string and returns a sequence of
+ pairs of the format (decoded_string, charset) where charset is the string
+ name of the character set.
+
+ This function takes one of those sequence of pairs and returns a Header
+ instance. Optional maxlinelen, header_name, and continuation_ws are as in
+ the Header constructor.
+ """
+ h = Header(maxlinelen=maxlinelen, header_name=header_name,
+ continuation_ws=continuation_ws)
+ for s, charset in decoded_seq:
+ if not isinstance(charset, Charset):
+ charset = Charset(charset)
+ h.append(s, charset)
+ return h
+
+
+
class Header:
! def __init__(self, s=None, charset=None, maxlinelen=None, header_name=None,
continuation_ws=' '):
"""Create a MIME-compliant header that can contain many languages.
! Specify the initial header value in s. If None, the initial header
! value is not set.
!
! Specify both s's character set, and the default character set by
! setting the charset argument to a Charset object (not a character set
! name string!). If None, a us-ascii Charset is used as both s's
! initial charset and as the default character set for subsequent
! .append() calls.
You can later append to the header with append(s, charset) below;
***************
*** 124,128 ****
# BAW: I believe `chunks' and `maxlinelen' should be non-public.
self._chunks = []
! self.append(s, charset)
if maxlinelen is None:
maxlinelen = MAXLINELEN
--- 151,156 ----
# BAW: I believe `chunks' and `maxlinelen' should be non-public.
self._chunks = []
! if s is not None:
! self.append(s, charset)
if maxlinelen is None:
maxlinelen = MAXLINELEN
***************
*** 149,157 ****
return u''.join(uchunks)
def append(self, s, charset=None):
"""Append string s with Charset charset to the MIME header.
charset defaults to the one given in the class constructor. If
! charset is given, it should be an instance of email.Charset.Charset.
"""
if charset is None:
--- 177,196 ----
return u''.join(uchunks)
+ # Rich comparison operators for equality only. BAW: does it make sense to
+ # have or explicitly disable <, <=, >, >= operators?
+ def __eq__(self, other):
+ # other may be a Header or a string. Both are fine so coerce
+ # ourselves to a string, swap the args and do another comparison.
+ return other == self.encode()
+
+ def __ne__(self, other):
+ return not self == other
+
def append(self, s, charset=None):
"""Append string s with Charset charset to the MIME header.
charset defaults to the one given in the class constructor. If
! charset is given, it should be an instance of Charset (not a character
! set name string!).
"""
if charset is None: