home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_win32comext_mapi_mapiutil.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  3.5 KB  |  115 lines

  1. # General utilities for MAPI and MAPI objects.
  2. from types import TupleType, ListType, IntType, StringType
  3. from pywintypes import UnicodeType, TimeType
  4. import pythoncom
  5. import mapi, mapitags
  6.  
  7. prTable = {}
  8. def GetPropTagName(pt):
  9.     if not prTable:
  10.         for name, value in mapitags.__dict__.items():
  11.             if name[:3] == 'PR_':
  12.                 prTable[value] = name
  13.     return prTable.get(pt)
  14.  
  15. mapiErrorTable = {}
  16. def GetScodeString(hr):
  17.     if not mapiErrorTable:
  18.         for name, value in mapi.__dict__.items():
  19.             if name[:7] in ['MAPI_E_', 'MAPI_W_']:
  20.                 mapiErrorTable[value] = name
  21.     return mapiErrorTable.get(hr, pythoncom.GetScodeString(hr))
  22.  
  23.  
  24. ptTable = {}
  25. def GetMapiTypeName(propType):
  26.     """Given a mapi type flag, return a string description of the type"""
  27.     if not ptTable:
  28.         for name, value in mapitags.__dict__.items():
  29.             if name[:3] == 'PT_':
  30.                 ptTable[value] = name
  31.  
  32.     rawType = propType & ~mapitags.MV_FLAG
  33.     return ptTable.get(rawType, str(hex(rawType)))
  34.  
  35.  
  36. def GetProperties(obj, propList):
  37.     """Given a MAPI object and a list of properties, return a list of property values.
  38.     
  39.     Allows a single property to be passed, and the result is a single object.
  40.     
  41.     Each request property can be an integer or a string.  Of a string, it is 
  42.     automatically converted to an integer via the GetIdsFromNames function.
  43.     
  44.     If the property fetch fails, the result is None.
  45.     """
  46.     bRetList = 1
  47.     if type(propList) not in [TupleType, ListType]:
  48.         bRetList = 0
  49.         propList = (propList,)
  50.     realPropList = []
  51.     rc = []
  52.     for prop in propList:
  53.         if type(prop)!=IntType:    # Integer
  54.             props = ( (mapi.PS_PUBLIC_STRINGS, prop), )
  55.             propIds = obj.GetIDsFromNames(props, 0)
  56.             prop = mapitags.PROP_TAG( mapitags.PT_UNICODE, mapitags.PROP_ID(propIds[0]))
  57.         realPropList.append(prop)
  58.         
  59.     hr, data = obj.GetProps(realPropList,0)
  60.     if hr != 0:
  61.         data = None
  62.         return None
  63.     if bRetList:
  64.         return map( lambda(v): v[1], data )
  65.     else:
  66.         return data[0][1]
  67.  
  68. def SetPropertyValue(obj, prop, val):
  69.     if type(prop)!=IntType:
  70.         props = ( (mapi.PS_PUBLIC_STRINGS, prop), )
  71.         propIds = obj.GetIDsFromNames(props, mapi.MAPI_CREATE)
  72.         prop = mapitags.PROP_TAG( mapitags.PT_UNICODE, mapitags.PROP_ID(propIds[0]))
  73.     if val is None:
  74.         # Delete the property
  75.         obj.DeleteProps((prop,))
  76.     else:
  77.         obj.SetProps(((prop,val),),0)
  78.  
  79. def SetProperties( msg, propDict):
  80.     """ Given a Python dictionary, set the objects properties.
  81.     
  82.     If the dictionary key is a string, then a property ID is queried
  83.     otherwise the ID is assumed native.
  84.     
  85.     Coded for maximum efficiency wrt server calls - ie, maximum of
  86.     2 calls made to the object, regardless of the dictionary contents
  87.     (only 1 if dictionary full of int keys)
  88.     """
  89.  
  90.     newProps = []
  91.     # First pass over the properties we should get IDs for.
  92.     for key, val in propDict.items():
  93.         if type(key) in [StringType, UnicodeType]:
  94.             newProps.append((mapi.PS_PUBLIC_STRINGS, key))
  95.     # Query for the new IDs
  96.     if newProps: newIds = msg.GetIDsFromNames(newProps, mapi.MAPI_CREATE)
  97.     newIdNo = 0
  98.     newProps = []
  99.     for key, val in propDict.items():
  100.         if type(key) in [StringType, UnicodeType]:
  101.             type_val=type(val)
  102.             if type_val in [StringType, pywintypes.UnicodeType]:
  103.                 tagType = mapitags.PT_UNICODE
  104.             elif type_val==IntType:
  105.                 tagType = mapitags.PT_I4
  106.             elif type_val==TimeType:
  107.                 tagType = mapitags.PT_SYSTIME
  108.             else:
  109.                 raise ValueError, "The type of object %s(%s) can not be written" % (`val`,type_val)
  110.             key = mapitags.PROP_TAG(tagType, mapitags.PROP_ID(newIds[newIdNo]))
  111.             newIdNo = newIdNo + 1
  112.         newProps.append( (key, val) )
  113.     msg.SetProps(newProps)
  114.  
  115.