From 8e24cbc7c50e74232f4dc08d6a96423bd0e6dc8e Mon Sep 17 00:00:00 2001 From: scrat Date: Sun, 25 May 2008 17:02:00 +0000 Subject: [PATCH] added Attachments and Attachment classes to winapi.py to get a more comprehensive doctest. In course of this change some small glitches have been found in outlook.py and mail.py, which concerned adressing of the Attachment items in outlook.py (createResource) and mail.py where the createMetadata Method now has **kw arguments for adding the filePath as a Metadata in case the mail has an attachment. The outlook.txt doctest has been changed to adopt the new test. winapi now contains only one extended example but it will get two more as soon as possible. git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2631 fd906abe-77d9-0310-91a1-e0d9ade77398 --- agent/crawl/mail.py | 4 +- agent/crawl/outlook.py | 9 ++-- agent/crawl/outlook.txt | 4 ++ agent/testing/winapi.py | 99 ++++++++++++++++++++++++++++++++++++----- 4 files changed, 101 insertions(+), 15 deletions(-) diff --git a/agent/crawl/mail.py b/agent/crawl/mail.py index 90a4436..0163787 100644 --- a/agent/crawl/mail.py +++ b/agent/crawl/mail.py @@ -57,8 +57,10 @@ class MailCrawler(Crawler): metadata=metadata) self.result.append(resource) - def createMetadata(self, metadata): + def createMetadata(self, metadata, **kw): metadata = Metadata(metadata) + for k, v in kw.items(): + metadata[k] = v return metadata def login(self): diff --git a/agent/crawl/outlook.py b/agent/crawl/outlook.py index b2a60e0..24eb2e3 100644 --- a/agent/crawl/outlook.py +++ b/agent/crawl/outlook.py @@ -25,6 +25,7 @@ $Id$ import re from email import MIMEMultipart import tempfile +import os from zope.interface import implements from twisted.internet import defer @@ -203,11 +204,13 @@ class OutlookCrawler(MailCrawler): attachedElems = getattr(mail, 'Attachments') for item in range(1, len(attachedElems)+1): fileHandle, filePath = tempfile.mkstemp(prefix="outlook") - item.SaveAsFile(fileHandle.name) - fileHandle.close() + attachedItem = attachedElems.Item(item) + attachedItem.SaveAsFile(fileHandle) + os.close(fileHandle) + metadat = self.createMetadata({}, filename=filePath) fileRes = FileResource(data=None, path=filePath, - metadata=self.createMetadata(filename=filePath)) + metadata=metadat) attachments.append(fileRes) resource = MailResource(data=mailContent, contentType=textType, diff --git a/agent/crawl/outlook.txt b/agent/crawl/outlook.txt index 800acee..975e4fb 100644 --- a/agent/crawl/outlook.txt +++ b/agent/crawl/outlook.txt @@ -46,4 +46,8 @@ the twisted reactor first. Namespace MAPI retrieved retrieving Outlook default folder collecting Mails from folder + Attachment: Invitation.pdf + Attachment: 21.pdf + Attachment saved + Attachment saved Job 00001 completed; result: [<...MailResource...>, <...MailResource...>, <...MailResource...>]; diff --git a/agent/testing/winapi.py b/agent/testing/winapi.py index 51eeb92..e565660 100644 --- a/agent/testing/winapi.py +++ b/agent/testing/winapi.py @@ -29,34 +29,111 @@ class com_error(Exception): pass +class Attachments(list): + + elemCount = 0 + data = [] + + def __init__(self, files=[]): + for elem in files: + fileitem = Attachment(filename=elem) + self.data.append(fileitem) + print "Attachment: ", fileitem.FileName + + @property + def Application(self): + print "Outlook application instance" + return "Outlook application instance" + + def Item(self, index): + return self.data[index-1] + + @property + def count(self): + print self.elemCount + return self.elemCount + +## @property +## def Data(self): +## return self.elemList + + def __len__(self): + return len(self.data) + + def __iter__(self): + yield self.data + + def __getitem__(self, idx): + return self.data[idx] + + +class Attachment(object): + + File = "" + + def __init__(self, filename=""): + self.File = filename + + def SaveAsFile(self, path=""): + print "Attachment saved" + + @property + def Parent(self): + # return value of Attribute Parent is of type _MailItem + pass + + @property + def Type(self): + pass + + @property + def Size(self): + # the size property is not available in Outlook 2000 + pass + + @property + def Application(self): + # Actual instance of Outlook application + pass + + @property + def FileName(self): + return self.File + + class Mail(object): #this is just a guess what a Outlook Mail Object Probably returns #Class = client.constants.olMail - def __init__(self): + def __init__(self, subj="", sendName="", to="", body="", **kw): self.Class = client.constants.olMail - self.Subject = 'dummy' - self.SenderName = 'dummy' - self.To = 'dummy' - self.Body = 'dummy' + self.Subject = subj + self.SenderName = sendName + self.To = to + self.Body = body + for k, v in kw.items(): + setattr(self, k, v) @property def _prop_map_get_(self): #here it is necessary of what attributes (called keys in outlok.py) #an Outlook Mail typically has - return {'Subject': "Testsubject",\ - 'SenderName': "TestSender",\ - 'To': "TestRecipient",\ - 'Body': "TestBody" - } + return self.__dict__ class Items(object): def __init__(self): self.data = {} - self.data[0] = Mail() + self.data[0] = Mail(Subject="Python Training", + SenderName="Mark Pilgrim", + To="allPythonics@python.org", + Body="The training will take place on Wed, 21st Dec.\ + Kindly check the enclosed invitation.", + BodyFormat=1, + Attachments=Attachments(["Invitation.pdf", "21.pdf"]) + ) self.data[1] = Mail() self.data[2] = Mail()