testStorage.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from win32com import storagecon
  2. import pythoncom, os, win32api
  3. import win32com.test.util
  4. import unittest
  5. class TestEnum(win32com.test.util.TestCase):
  6. def testit(self):
  7. fname, tmp = win32api.GetTempFileName(win32api.GetTempPath(),'stg')
  8. m=storagecon.STGM_READWRITE | storagecon.STGM_SHARE_EXCLUSIVE
  9. ## file, mode, format, attrs (always 0), IID (IStorage or IPropertySetStorage, storage options(only used with STGFMT_DOCFILE)
  10. pss=pythoncom.StgOpenStorageEx(fname, m, storagecon.STGFMT_FILE, 0 , pythoncom.IID_IPropertySetStorage)
  11. ### {"Version":2,"reserved":0,"SectorSize":512,"TemplateFile":u'somefilename'})
  12. ## FMTID_SummaryInformation FMTID_DocSummaryInformation FMTID_UserDefinedProperties
  13. psuser=pss.Create(pythoncom.FMTID_UserDefinedProperties,
  14. pythoncom.IID_IPropertySetStorage,
  15. storagecon.PROPSETFLAG_DEFAULT,
  16. storagecon.STGM_READWRITE|storagecon.STGM_CREATE|storagecon.STGM_SHARE_EXCLUSIVE) ## its very picky about flag combinations!
  17. psuser.WriteMultiple((3,4),('hey','bubba'))
  18. psuser.WritePropertyNames((3,4),('property3','property4'))
  19. expected_summaries = []
  20. expected_summaries.append( ('property3', 3, pythoncom.VT_BSTR))
  21. expected_summaries.append( ('property4', 4, pythoncom.VT_BSTR))
  22. psuser=None
  23. pssum=pss.Create(pythoncom.FMTID_SummaryInformation,
  24. pythoncom.IID_IPropertySetStorage,
  25. storagecon.PROPSETFLAG_DEFAULT,
  26. storagecon.STGM_READWRITE|storagecon.STGM_CREATE|storagecon.STGM_SHARE_EXCLUSIVE)
  27. pssum.WriteMultiple((storagecon.PIDSI_AUTHOR,storagecon.PIDSI_COMMENTS),('me', 'comment'))
  28. pssum=None
  29. pss=None ## doesn't seem to be a close or release method, and you can't even reopen it from the same process until previous object is gone
  30. pssread=pythoncom.StgOpenStorageEx(fname, storagecon.STGM_READ|storagecon.STGM_SHARE_EXCLUSIVE, storagecon.STGFMT_FILE, 0 , pythoncom.IID_IPropertySetStorage)
  31. found_summaries = []
  32. for psstat in pssread:
  33. ps=pssread.Open(psstat[0],storagecon.STGM_READ|storagecon.STGM_SHARE_EXCLUSIVE)
  34. for p in ps:
  35. p_val = ps.ReadMultiple((p[1],))[0]
  36. if (p[1]==storagecon.PIDSI_AUTHOR and p_val=='me') or \
  37. (p[1]==storagecon.PIDSI_COMMENTS and p_val=='comment'):
  38. pass
  39. else:
  40. self.fail("Uxexpected property %s/%s" % (p, p_val))
  41. ps=None
  42. ## FMTID_UserDefinedProperties can't exist without FMTID_DocSummaryInformation, and isn't returned independently from Enum
  43. ## also can't be open at same time
  44. if psstat[0]==pythoncom.FMTID_DocSummaryInformation:
  45. ps=pssread.Open(pythoncom.FMTID_UserDefinedProperties,storagecon.STGM_READ|storagecon.STGM_SHARE_EXCLUSIVE)
  46. for p in ps:
  47. found_summaries.append(p)
  48. ps=None
  49. psread=None
  50. expected_summaries.sort()
  51. found_summaries.sort()
  52. self.assertEqual(expected_summaries, found_summaries)
  53. if __name__=='__main__':
  54. unittest.main()