[Spambayes-checkins] spambayes/Outlook2000/sandbox
dump_props.py,1.5,1.6
Mark Hammond
mhammond@users.sourceforge.net
Wed Nov 20 22:06:19 2002
Update of /cvsroot/spambayes/spambayes/Outlook2000/sandbox
In directory sc8-pr-cvs1:/tmp/cvs-serv14510
Modified Files:
dump_props.py
Log Message:
Work better with multiple stores, and re-implement a folder search
instead of using a brain dead MS one.
-s option shows all store and top-level folder names.
Index: dump_props.py
===================================================================
RCS file: /cvsroot/spambayes/spambayes/Outlook2000/sandbox/dump_props.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** dump_props.py 4 Nov 2002 00:49:11 -0000 1.5
--- dump_props.py 20 Nov 2002 22:06:17 -0000 1.6
***************
*** 1,2 ****
--- 1,3 ----
+ from __future__ import generators
# Dump every property we can find for a MAPI item
***************
*** 14,35 ****
session = mapi.MAPILogonEx(0, None, None, logonFlags)
! def _FindDefaultMessageStore():
tab = session.GetMsgStoresTable(0)
- # Restriction for the table: get rows where PR_DEFAULT_STORE is true.
- # There should be only one.
- restriction = (mapi.RES_PROPERTY, # a property restriction
- (mapi.RELOP_EQ, # check for equality
- PR_DEFAULT_STORE, # of the PR_DEFAULT_STORE prop
- (PR_DEFAULT_STORE, True))) # with True
rows = mapi.HrQueryAllRows(tab,
! (PR_ENTRYID,), # columns to retrieve
! restriction, # only these rows
None, # any sort order is fine
0) # any # of results is fine
! # get first entry, a (property_tag, value) pair, for PR_ENTRYID
! row = rows[0]
! eid_tag, eid = row[0]
! # Open the store.
! return session.OpenMsgStore(
0, # no parent window
eid, # msg store to open
--- 15,29 ----
session = mapi.MAPILogonEx(0, None, None, logonFlags)
! def GetMessageStores():
tab = session.GetMsgStoresTable(0)
rows = mapi.HrQueryAllRows(tab,
! (PR_ENTRYID, PR_DISPLAY_NAME_A, PR_DEFAULT_STORE), # columns to retrieve
! None, # all rows
None, # any sort order is fine
0) # any # of results is fine
! for row in rows:
! (eid_tag, eid), (name_tag, name), (def_store_tag, def_store) = row
! # Open the store.
! store = session.OpenMsgStore(
0, # no parent window
eid, # msg store to open
***************
*** 40,67 ****
mapi.MDB_NO_MAIL |
mapi.MAPI_DEFERRED_ERRORS)
! def _FindItemsWithValue(folder, prop_tag, prop_val):
! tab = folder.GetContentsTable(0)
! # Restriction for the table: get rows where our prop values match
! restriction = (mapi.RES_CONTENT, # a property restriction
! (mapi.FL_SUBSTRING | mapi.FL_IGNORECASE | mapi.FL_LOOSE, # fuzz level
! prop_tag, # of the given prop
! (prop_tag, prop_val))) # with given val
! rows = mapi.HrQueryAllRows(tab,
! (PR_ENTRYID,), # columns to retrieve
! restriction, # only these rows
! None, # any sort order is fine
! 0) # any # of results is fine
! # get entry IDs
! return [row[0][1] for row in rows]
! def _FindFolderEID(name):
assert name
! from win32com.mapi import exchange
! if not name.startswith("\\"):
! name = "\\Top Of Personal Folders\\" + name
! store = _FindDefaultMessageStore()
! folder_eid = exchange.HrMAPIFindFolderEx(store, "\\", name)
! return folder_eid
# Also in new versions of mapituil
--- 34,77 ----
mapi.MDB_NO_MAIL |
mapi.MAPI_DEFERRED_ERRORS)
+ yield store, name, def_store
! def _FindSubfolder(store, folder, find_name):
! find_name = find_name.lower()
! table = folder.GetHierarchyTable(0)
! rows = mapi.HrQueryAllRows(table, (PR_ENTRYID, PR_DISPLAY_NAME_A), None, None, 0)
! for (eid_tag, eid), (name_tag, name), in rows:
! if name.lower() == find_name:
! return store.OpenEntry(eid, None, mapi.MAPI_DEFERRED_ERRORS)
! return None
! def FindFolder(name):
assert name
! names = [n.lower() for n in name.split("\\")]
! if names[0]:
! for store, name, is_default in GetMessageStores():
! if is_default:
! store_name = name.lower()
! break
! folder_names = names
! else:
! store_name = names[1]
! folder_names = names[2:]
! # Find the store with the name
! for store, name, is_default in GetMessageStores():
! if name.lower() == store_name:
! folder_store = store
! break
! else:
! raise ValueError, "The store '%s' can not be located" % (store_name,)
!
! hr, data = store.GetProps((PR_IPM_SUBTREE_ENTRYID,), 0)
! subtree_eid = data[0][1]
! folder = folder_store.OpenEntry(subtree_eid, None, mapi.MAPI_DEFERRED_ERRORS)
!
! for name in folder_names:
! folder = _FindSubfolder(folder_store, folder, name)
! if folder is None:
! raise ValueError, "The subfolder '%s' can not be located" % (name,)
! return folder_store, folder
# Also in new versions of mapituil
***************
*** 85,88 ****
--- 95,114 ----
return ret
+ def _FindItemsWithValue(folder, prop_tag, prop_val):
+ tab = folder.GetContentsTable(0)
+ # Restriction for the table: get rows where our prop values match
+ restriction = (mapi.RES_CONTENT, # a property restriction
+ (mapi.FL_SUBSTRING | mapi.FL_IGNORECASE | mapi.FL_LOOSE, # fuzz level
+ prop_tag, # of the given prop
+ (prop_tag, prop_val))) # with given val
+ rows = mapi.HrQueryAllRows(tab,
+ (PR_ENTRYID,), # columns to retrieve
+ restriction, # only these rows
+ None, # any sort order is fine
+ 0) # any # of results is fine
+ # get entry IDs
+ return [row[0][1] for row in rows]
+
+
def DumpItemProps(item, shorten):
for prop_name, prop_val in GetAllProperties(item):
***************
*** 92,100 ****
print "%-20s: %s" % (prop_name, prop_repr)
! def DumpProps(folder_eid, subject, include_attach, shorten):
! mapi_msgstore = _FindDefaultMessageStore()
! mapi_folder = mapi_msgstore.OpenEntry(folder_eid,
! None,
! mapi.MAPI_DEFERRED_ERRORS)
hr, data = mapi_folder.GetProps( (PR_DISPLAY_NAME_A,), 0)
name = data[0][1]
--- 118,122 ----
print "%-20s: %s" % (prop_name, prop_repr)
! def DumpProps(mapi_msgstore, mapi_folder, subject, include_attach, shorten):
hr, data = mapi_folder.GetProps( (PR_DISPLAY_NAME_A,), 0)
name = data[0][1]
***************
*** 117,121 ****
--- 139,160 ----
DumpItemProps(attach, shorten)
+ def DumpTopLevelFolders():
+ print "Top-level folder names are:"
+ for store, name, is_default in GetMessageStores():
+ # Find the folder with the content.
+ hr, data = store.GetProps((PR_IPM_SUBTREE_ENTRYID,), 0)
+ subtree_eid = data[0][1]
+ folder = store.OpenEntry(subtree_eid, None, mapi.MAPI_DEFERRED_ERRORS)
+ # Now the top-level folders in the store.
+ table = folder.GetHierarchyTable(0)
+ rows = mapi.HrQueryAllRows(table, (PR_DISPLAY_NAME_A), None, None, 0)
+ for (name_tag, folder_name), in rows:
+ print " \\%s\\%s" % (name, folder_name)
+
def usage():
+ def_store_name = "<??unknown??>"
+ for store, name, is_def in GetMessageStores():
+ if is_def:
+ def_store_name = name
msg = """\
Usage: %s [-f foldername] subject of the message
***************
*** 123,126 ****
--- 162,166 ----
-s - Shorten long property values.
-a - Include attachments
+ -n - Show top-level folder names and exit
Dumps all properties for all messages that match the subject. Subject
***************
*** 130,140 ****
as the path seperator. If the folder name begins with a
\\, it must be a fully-qualified name, including the message
! store name (eg, "Top of Public Folders"). If the path does not
! begin with a \\, it is assumed to be fully-qualifed from the root
! of the default message store
! Eg, python\\python-dev' will locate a python-dev subfolder in a python
! subfolder in your default store.
! """ % os.path.basename(sys.argv[0])
print msg
--- 170,180 ----
as the path seperator. If the folder name begins with a
\\, it must be a fully-qualified name, including the message
! store name. For example, your Inbox can be specified either as:
! -f "Inbox"
! or
! -f "\\%s\\Inbox"
! Use the -n option to see all top-level folder names from all stores.
! """ % (os.path.basename(sys.argv[0]), def_store_name)
print msg
***************
*** 143,147 ****
import getopt
try:
! opts, args = getopt.getopt(sys.argv[1:], "af:s")
except getopt.error, e:
print e
--- 183,187 ----
import getopt
try:
! opts, args = getopt.getopt(sys.argv[1:], "af:sn")
except getopt.error, e:
print e
***************
*** 150,157 ****
sys.exit(1)
folder_name = ""
- subject = " ".join(args)
- if not subject:
- usage()
- sys.exit(1)
shorten = False
--- 190,193 ----
***************
*** 164,167 ****
--- 200,206 ----
elif opt == "-a":
include_attach = True
+ elif opt == "-n":
+ DumpTopLevelFolders()
+ sys.exit(1)
else:
print "Invalid arg"
***************
*** 171,179 ****
folder_name = "Inbox" # Assume this exists!
! eid = _FindFolderEID(folder_name)
! if eid is None:
! print "*** Cant find folder", folder_name
! return
! DumpProps(eid, subject, include_attach, shorten)
if __name__=='__main__':
--- 210,227 ----
folder_name = "Inbox" # Assume this exists!
! subject = " ".join(args)
! if not subject:
! print "You must specify a subject"
! print
! usage()
! sys.exit(1)
!
! try:
! store, folder = FindFolder(folder_name)
! except ValueError, details:
! print details
! sys.exit(1)
!
! DumpProps(store, folder, subject, include_attach, shorten)
if __name__=='__main__':
More information about the Spambayes-checkins
mailing list