##
## Copyright (c) 2006 Jason Dillon
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##
## $Id: //guest/jason_dillon/p4spam/main/pylib/p4spam/message.py#2 $ $Date: 2006/04/12 $
##
from email.MIMEText import MIMEText
from perforce import logging
##
## HACK: Need config to be fixed!!!
##
from p4spam.config import *
##
## MessageBuffer
##
from cStringIO import StringIO
class MessageBuffer:
NEWLINE = '\n'
def __init__(this):
this.buff = StringIO()
def close(this):
this.buff.close()
def write(this, *args):
for arg in args:
this.buff.write(arg)
def writeln(this, *args):
for arg in args:
this.buff.write(arg)
this.buff.write(this.NEWLINE)
def toString(this):
return this.buff.getvalue()
##
## MessageBuilder
##
class MessageBuilder:
def __init__(this, info):
this.log = logging.Logger(this)
this.info = info
this.p4 = info.p4
this.contentType = 'plain'
def getTitle(this):
return this.getSubject()
def getSubject(this):
change = this.info.getChange()
comment = this.info.getComment()
# Strip newlines
comment = comment.replace("\n", ', ')
subject = "[P4] #%s - %s" % (change, comment)
l = len(subject)
if l > MAX_SUBJECT_LINE_LENGTH:
this.log.warn("Subject line longer than %s; truncating %s remaining characters" % (MAX_SUBJECT_LINE_LENGTH, l - MAX_SUBJECT_LINE_LENGTH))
subject = subject[:MAX_SUBJECT_LINE_LENGTH]
return subject
def writeDocument(this, buff):
raise "Abstract method"
def getPayload(this):
buff = MessageBuffer()
this.writeDocument(buff)
payload = buff.toString()
buff.close()
return payload
def getMessage(this):
this.log.info("Building email message...")
payload = this.getPayload()
msg = MIMEText(payload, this.contentType)
msg['Subject'] = this.getSubject()
return msg