-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_outlook.py
More file actions
138 lines (113 loc) · 3.56 KB
/
_outlook.py
File metadata and controls
138 lines (113 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""
Ilari Shafer
4/30/07
Interface to Outlook libraries
"""
import win32com.client
#convert a comma-separated list to a list with elements of type typ
def csv2lst(str, typ):
return [typ(item) for item in str.split(",")]
#convert a list to a csv string
def lst2csv(lst):
str = ""
for dex in range(len(lst) - 1):
str += lst[dex] + ","
if len(lst) >= 1:
str += lst[-1]
return str
#defines publicly accessible methods for Outlook
class OutlookInterface:
def __init__(self):
self.out = Outlook()
#Get the root list of names
def GetNames(self):
return lst2csv(self.out.root.childNames())
#Get the list of folder names on the selected message path
def GetNamesOnPath(self,lst):
return lst2csv(self.out.GetFolder(csv2lst(lst, int)).childNames())
#Get the number of items in the selected message path
def GetLenItemsOnPath(self,lst):
return self.out.GetFolder(csv2lst(lst, int)).LenItems()
#Get the small preview box contents for the item on path lst at index dex
def GetItemPreviewOnPath(self,lst,dex):
return self.out.GetFolder(csv2lst(lst, int)).GetItem(int(dex)).Preview()
#Get the main window contents for the item on path lst at index dex
def GetItemContentsOnPath(self,lst,dex):
return self.out.GetFolder(csv2lst(lst, int)).GetItem(int(dex)).Contents()
#Internal Outlook interface
class Outlook:
def __init__(self):
self.comobj = win32com.client.Dispatch("Outlook.Application")
self.ns = self.comobj.GetNamespace("MAPI")
self.root = Folder(self.ns, "root")
def GetFolder(self, lst):
curfol = self.root
for dex in lst:
curfol = curfol.childFolder(dex)
return curfol
#Represents a Folder in the Outlook folder hierarchy
class Folder:
#Create a new Folder with the given MAPIFolder object
def __init__(self, obj, name=None):
self.obj = obj
if name is not None:
self.name = name
else:
self.name = obj.Name
self.assigned = False
self.enumFolders = list()
def GetName(self):
return self.name
def assignFolders(self):
for dex in range(len(self.obj.Folders)):
self.enumFolders.append(Folder(self.obj.Folders[dex+1]))
#return a subfolder with the specified index
def childFolder(self, dex):
if not self.assigned:
self.assignFolders()
self.assigned = True
return self.enumFolders[dex]
#return a list of names of child folders in order
def childNames(self):
if not self.assigned:
self.assignFolders()
self.assigned = True
names = []
for fol in self.enumFolders:
names.append(fol.GetName())
return names
#return the number of items contained in this Folder
def LenItems(self):
return len(self.obj.Items)
#return a polymorphic Item representing
def GetItem(self, dex):
iobj = self.obj.Items[dex+1]
typ = iobj.__class__.__name__
if typ == "_MailItem":
return MailItem(iobj)
else:
return Item(iobj)
class Item:
def __init__(self, obj):
self.obj = obj;
def Preview(self):
return "unknown"
def Contents(self):
return str(self.obj);
#represents a MailItem
class MailItem(Item):
#exemplifies how one might toss back an HTML string
def Preview(self):
return \
"<div class='preview subject'>" + str(self.obj.Subject) + "</div>" + \
"<div class='preview sender'>" + str(self.obj.SentOnBehalfOfName) + "</div>" + \
"<div class='preview date'>" + str(self.obj.SentOn) + "</div>"
def Contents(self):
return self.obj.HTMLBody
if __name__ == "__main__":
out = OutlookInterface()
fol = out.out.root.childFolder(0).childFolder(5)
print fol.GetName()
#fol = out.out.root.childFolder(1)
print "length:", out.GetLenItemsOnPath("0,5")
print out.GetItemPreviewOnPath("0,5","5")