[Commits] (bkirsch) Updated message parsing logic to be two way from kind and to kind

commits at osafoundation.org commits at osafoundation.org
Mon Aug 9 17:46:12 PDT 2004


Commit by: bkirsch
Modified files:
chandler/parcels/osaf/mail/message.py 1.3 1.4

Log message:
Updated message parsing logic to be two way from kind and to kind

ViewCVS links:
http://cvs.osafoundation.org/index.cgi/chandler/parcels/osaf/mail/message.py.diff?r1=text&tr1=1.3&r2=text&tr2=1.4

Index: chandler/parcels/osaf/mail/message.py
diff -u chandler/parcels/osaf/mail/message.py:1.3 chandler/parcels/osaf/mail/message.py:1.4
--- chandler/parcels/osaf/mail/message.py:1.3	Tue Aug  3 16:47:57 2004
+++ chandler/parcels/osaf/mail/message.py	Mon Aug  9 17:46:11 2004
@@ -1,5 +1,5 @@
-__revision__  = "$Revision: 1.3 $"
-__date__      = "$Date: 2004/08/03 23:47:57 $"
+__revision__  = "$Revision: 1.4 $"
+__date__      = "$Date: 2004/08/10 00:46:11 $"
 __copyright__ = "Copyright (c) 2004 Open Source Applications Foundation"
 __license__   = "http://osafoundation.org/Chandler_0.1_license_terms.htm"
 
@@ -9,20 +9,24 @@
 import email.Message as Message
 import email.Utils as Utils
 
+
+# XXX: Relook at this logic
 def format_addr(addr):
     """
     This method formats an email address
 
-    @param addr: The email address to format
-    @type addr: list
+    @param addr: The email address list to format
+    @type addr: C{list}
     @return: C{string}
     """
-
     str = addr[0]
-    if str != '':
+    if str != None and str != '':
         str = str + ' '
-    str = str + '<' + addr[1] + '>'
-    return str
+
+        str = str + '<' + addr[1] + '>'
+        return str
+
+    return addr[1]
 
 def messageTextToKind(messageText):
     """
@@ -55,9 +59,14 @@
     m = Mail.MailMessage()
 
     if m is None:
-        raise Exception("Repository returned a MailMessage that was None")
+        raise TypeError("Repository returned a MailMessage that was None")
+
+    if messageObject['Date'] is not None:
+        m.dateSent = DateTime.mktime(Utils.parsedate(messageObject['Date']))
+
+    else:
+        m.dateSent = None
 
-    m.dateSent = DateTime.mktime(Utils.parsedate(messageObject['Date']))
     m.dateReceived = DateTime.now()
 
     if messageObject['Subject'] is None:
@@ -81,9 +90,15 @@
         ea.emailAddress = format_addr(addr)
         m.ccAddress.append(ea)
 
+    m.bccAddress = []
+    for addr in Utils.getaddresses(messageObject.get_all('Bcc', [])):
+        ea = Mail.EmailAddress()
+        ea.emailAddress = format_addr(addr)
+        m.bccAddress.append(ea)
+
     return m
 
-def KindToMessageObject(mailMessage):
+def kindToMessageObject(mailMessage):
     """
     This method converts a email message string to
     a Chandler C{Mail.MailMessage} object
@@ -93,11 +108,61 @@
     @return: C{Mail.MailMessage}
     """
 
-    ### Check that the kind is a mail message
     if not isinstance(mailMessage, Mail.MailMessage):
-        raise Exception("mailMessage must be an instance of Kind Mail.MailMessage")
+        raise TypeError("mailMessage must be an instance of Kind Mail.MailMessage")
 
 
     messageObject = Message.Message()
 
+    messageObject['Date'] = Utils.formatdate(mailMessage.dateSent.ticks(), True)
+    messageObject['Date Received'] = Utils.formatdate(mailMessage.dateReceived.ticks(), True)
+    messageObject['Subject'] = mailMessage.subject
+
+    messageObject['From'] = mailMessage.replyAddress.emailAddress
+
+    to = []
+
+    for address in mailMessage.toAddress:
+        to.append(address.emailAddress)
+
+    messageObject['To'] = ", ".join(to)
+
+    del to
+
+    cc = []
+
+    for address in mailMessage.ccAddress:
+        cc.append(address.emailAddress)
+
+    messageObject['Cc'] = ", ".join(cc)
+
+    del cc
+
+    bcc = []
+
+    for address in mailMessage.bccAddress:
+        bcc.append(address.emailAddress)
+
+    messageObject['Bcc'] = ", ".join(bcc)
+
+    del bcc
+
     return messageObject
+
+
+def kindToMessageText(mailMessage):
+    """
+    This method converts a email message string to
+    a Chandler C{Mail.MailMessage} object
+
+    @param messageObject: A C{email.Message} object representation of a mail message
+    @type messageObject: C{email.Message}
+    @return: C{Mail.MailMessage}
+    """
+
+    if not isinstance(mailMessage, Mail.MailMessage):
+        raise TypeError("mailMessage must be an instance of Kind Mail.MailMessage")
+
+    messageObject = kindToMessageObject(mailMessage)
+
+    return messageObject.as_string()



More information about the Commits mailing list