[Spambayes-checkins] spambayes/spambayes message.py,1.52,1.53
Tony Meyer
anadelonbrin at users.sourceforge.net
Wed Aug 4 10:18:20 CEST 2004
Update of /cvsroot/spambayes/spambayes/spambayes
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8575/spambayes
Modified Files:
message.py
Log Message:
The StringIO import is meant to fall back to StringIO if cStringIO is not available, I believe.
Add a insert_exception_header utility function. Both sb_server and sb_imapfilter do this, so
it makes sense to factor the code out to here. (This can't be a method of the Message object
itself, because we use it when we can't make a Message object).
Index: message.py
===================================================================
RCS file: /cvsroot/spambayes/spambayes/spambayes/message.py,v
retrieving revision 1.52
retrieving revision 1.53
diff -C2 -d -r1.52 -r1.53
*** message.py 3 Aug 2004 11:54:22 -0000 1.52
--- message.py 4 Aug 2004 08:18:18 -0000 1.53
***************
*** 95,98 ****
--- 95,99 ----
import shelve
import pickle
+ import traceback
import email
***************
*** 105,109 ****
from spambayes.tokenizer import tokenize
! from cStringIO import StringIO
CRLF_RE = re.compile(r'\r\n|\r|\n')
--- 106,113 ----
from spambayes.tokenizer import tokenize
! try:
! import cStringIO as StringIO
! except ImportError:
! import StringIO
CRLF_RE = re.compile(r'\r\n|\r|\n')
***************
*** 248,252 ****
Use *_from_string as described above."""
prs = email.Parser.Parser()
! fp = StringIO(payload)
# this is kindof a hack, due to the fact that the parser creates a
# new message object, and we already have the message object
--- 252,256 ----
Use *_from_string as described above."""
prs = email.Parser.Parser()
! fp = StringIO.StringIO(payload)
# this is kindof a hack, due to the fact that the parser creates a
# new message object, and we already have the message object
***************
*** 482,483 ****
--- 486,515 ----
def sbheadermessage_from_string(s, _class=SBHeaderMessage, strict=False):
return email.message_from_string(s, _class, strict)
+
+ # Utility function to insert an exception header into the given RFC822 text.
+ # This is used by both sb_server and sb_imapfilter, so it's handy to have
+ # it available separately.
+ def insert_exception_header(string_msg):
+ """Insert an exception header into the given RFC822 message (as text).
+
+ Returns a tuple of the new message text and the exception details."""
+ stream = StringIO.StringIO()
+ traceback.print_exc(None, stream)
+ details = stream.getvalue()
+
+ # Build the header. This will strip leading whitespace from
+ # the lines, so we add a leading dot to maintain indentation.
+ detailLines = details.strip().split('\n')
+ dottedDetails = '\n.'.join(detailLines)
+ headerName = 'X-Spambayes-Exception'
+ header = email.Header.Header(dottedDetails, header_name=headerName)
+
+ # Insert the exception header, and also insert the id header,
+ # otherwise we might keep doing this message over and over again.
+ # We also ensure that the line endings are /r/n as RFC822 requires.
+ headers, body = re.split(r'\n\r?\n', string_msg, 1)
+ header = re.sub(r'\r?\n', '\r\n', str(header))
+ headers += "\n%s: %s\r\n%s: %s\r\n\r\n" % \
+ (headerName, header,
+ options["Headers", "mailid_header_name"], self.id)
+ return (headers + body, details)
More information about the Spambayes-checkins
mailing list