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
This commit is contained in:
parent
665e9bf650
commit
8e24cbc7c5
4 changed files with 101 additions and 15 deletions
|
@ -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):
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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...>];
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue