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_win32com_test_testPersist.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  5.8 KB  |  207 lines

  1. import pythoncom
  2. import win32com.server.util
  3. import util
  4. import time
  5.  
  6. import win32com, sys, string, win32api, traceback
  7. import win32com.client.dynamic
  8. import win32com.client
  9. import pythoncom
  10. from win32com.axcontrol import axcontrol
  11. from pywintypes import Unicode
  12. from win32com import storagecon
  13. from win32com.test.util import CheckClean
  14.  
  15. import pywintypes
  16. import win32ui
  17. import win32api, os
  18.  
  19. S_OK = 0
  20.  
  21. class LockBytes:
  22.   _public_methods_ = [ 'ReadAt', 'WriteAt', 'Flush', 'SetSize', 'LockRegion', 'UnlockRegion', 'Stat' ]
  23.   _com_interfaces_ = [ pythoncom.IID_ILockBytes ]
  24.  
  25.   def __init__(self, data = ""):
  26.     self.data = data
  27.     now = pywintypes.Time(time.time())
  28.     self.ctime = now
  29.     self.mtime = now
  30.     self.atime = now
  31.  
  32.   def ReadAt(self, offset, cb):
  33.     print "ReadAt"
  34.     result = self.data[offset:offset + cb]
  35.     return result
  36.  
  37.   def WriteAt(self, offset, data):
  38.     print "WriteAt " +str(offset)
  39.     print "len " + str(len(data))
  40.     print "data:"
  41.     #print data
  42.     if len(self.data) >= offset:
  43.       newdata = self.data[0:offset] + data 
  44.     print len(newdata)
  45.     if len(self.data) >= offset + len(data):
  46.       newdata = newdata + self.data[offset +  len(data):]
  47.     print len(newdata)
  48.     self.data = newdata
  49.     return len(data)
  50.         
  51.   def Flush(self, whatsthis=0):
  52.     print "Flush" + str(whatsthis)
  53.     fname = os.path.join(win32api.GetTempPath(), "persist.doc")
  54.     open(fname, "wb").write(self.data)
  55.     return S_OK
  56.   
  57.   def SetSize(self, size):
  58.     print "Set Size" + str(size)
  59.     if size > len(self.data):      
  60.       self.data = self.data +  "\000" * (size - len(self.data))
  61.     else:
  62.       self.data = self.data[0:size]
  63.     return S_OK
  64.     
  65.   def LockRegion(self, offset, size, locktype):
  66.     print "LockRegion"
  67.     pass
  68.  
  69.   def UnlockRegion(self, offset, size, locktype):
  70.     print "UnlockRegion"
  71.     pass
  72.  
  73.   def Stat(self, statflag):
  74.     print "returning Stat " + str(statflag) 
  75.     return (
  76.       "PyMemBytes",
  77.       storagecon.STGTY_LOCKBYTES,
  78.       len(self.data),
  79.       self.mtime,
  80.       self.ctime,
  81.       self.atime,
  82.       storagecon.STGM_DIRECT|storagecon.STGM_READWRITE|storagecon.STGM_CREATE ,
  83.       storagecon.STGM_SHARE_EXCLUSIVE,
  84.       "{00020905-0000-0000-C000-000000000046}",
  85.       0,   # statebits ?
  86.       0
  87.       )
  88.  
  89.  
  90. class OleClientSite:
  91.   _public_methods_ = [ 'SaveObject', 'GetMoniker', 'GetContainer', 'ShowObject', 'OnShowWindow', 'RequestNewObjectLayout' ]
  92.   _com_interfaces_ = [ axcontrol.IID_IOleClientSite ]
  93.  
  94.   def __init__(self, data = ""):
  95.     self.IPersistStorage = None
  96.     self.IStorage = None
  97.  
  98.   def SetIPersistStorage(self, IPersistStorage):
  99.     self.IPersistStorage = IPersistStorage
  100.   
  101.   def SetIStorage(self, IStorage):
  102.     self.IStorage = IStorage
  103.   
  104.   def SaveObject(self):
  105.     print "SaveObject"
  106.     if self.IPersistStorage != None and self.IStorage != None:
  107.       self.IPersistStorage.Save(self.IStorage,1)
  108.       self.IStorage.Commit(0)
  109.     return S_OK
  110.  
  111.   def GetMoniker(self, dwAssign, dwWhichMoniker):
  112.     print "GetMoniker " + str(dwAssign) + " " + str(dwWhichMoniker)
  113.  
  114.   def GetContainer(self):
  115.     print "GetContainer"
  116.  
  117.   def ShowObject(self):
  118.     print "ShowObject"
  119.  
  120.   def OnShowWindow(self, fShow):
  121.     print "ShowObject" + str(fShow)
  122.     
  123.   def RequestNewObjectLayout(self):
  124.     print "RequestNewObjectLayout"
  125.  
  126.       
  127. def test():
  128.     # create a LockBytes object and 
  129.     #wrap it as a COM object
  130. #    import win32com.server.dispatcher
  131.     lbcom = win32com.server.util.wrap(LockBytes(), pythoncom.IID_ILockBytes) #, useDispatcher=win32com.server.dispatcher.DispatcherWin32trace)
  132.     
  133.     # create a structured storage on the ILockBytes object
  134.     stcom = pythoncom.StgCreateDocfileOnILockBytes(lbcom, storagecon.STGM_DIRECT| storagecon.STGM_CREATE | storagecon.STGM_READWRITE | storagecon.STGM_SHARE_EXCLUSIVE, 0)
  135.     
  136.     # create our ClientSite
  137.     ocs = OleClientSite()
  138.     # wrap it as a COM object
  139.     ocscom = win32com.server.util.wrap(ocs, axcontrol.IID_IOleClientSite)
  140.     
  141.     # create a Word OLE Document, connect it to our site and our storage
  142.     oocom=axcontrol.OleCreate("{00020906-0000-0000-C000-000000000046}",
  143.         axcontrol.IID_IOleObject,
  144.         0,
  145.         (0,),
  146.         ocscom,
  147.         stcom,
  148.         )
  149.  
  150.     mf=win32ui.GetMainFrame()
  151.     hwnd=mf.GetSafeHwnd()
  152.     
  153.     # Set the host and document name
  154.     # for unknown reason document name becomes hostname, and document name
  155.     # is not set, debugged it, but don't know where the problem is?
  156.     oocom.SetHostNames("OTPython", "This is Cool")
  157.     
  158.     # activate the OLE document
  159.     oocom.DoVerb( -1, ocscom, 0, hwnd, mf.GetWindowRect())
  160.     
  161.     # set the hostnames again
  162.     oocom.SetHostNames("OTPython2", "ThisisCool2")
  163.     
  164.     # get IDispatch of Word
  165.     doc=win32com.client.Dispatch(oocom.QueryInterface(pythoncom.IID_IDispatch))
  166.     
  167.     # get IPersistStorage of Word
  168.     dpcom=oocom.QueryInterface(pythoncom.IID_IPersistStorage)
  169.     
  170.     # let our ClientSite know the interfaces
  171.     ocs.SetIPersistStorage(dpcom)
  172.     ocs.SetIStorage(stcom)
  173.     
  174.     # use IDispatch to do the Office Word test
  175.     # pasted from TestOffice.py
  176.     
  177.     wrange = doc.Range()
  178.     for i in range(10):
  179.         wrange.InsertAfter("Hello from Python %d\n" % i)
  180.     paras = doc.Paragraphs
  181.     for i in range(len(paras)):
  182.         paras[i]().Font.ColorIndex = i+1
  183.         paras[i]().Font.Size = 12 + (4 * i)
  184.     # XXX - note that
  185.     # for para in paras:
  186.     #     para().Font...
  187.     # doesnt seem to work - no error, just doesnt work
  188.     # Should check if it works for VB!
  189.  
  190.     
  191.     dpcom.Save(stcom, 0)
  192.     dpcom.HandsOffStorage()
  193. #    oocom.Close(axcontrol.OLECLOSE_NOSAVE) # or OLECLOSE_SAVEIFDIRTY, but it fails???
  194.     
  195.     #Save the ILockBytes data to "persist2.doc"
  196.     lbcom.Flush()
  197.     
  198.     #exiting Winword will automatically update the ILockBytes data
  199.     #and flush it to "%TEMP%\persist.doc"
  200.     doc.Application.Quit()
  201.  
  202. if __name__=='__main__':
  203.     test()
  204.     pythoncom.CoUninitialize()
  205.     CheckClean()
  206.  
  207.