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_testStreams.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  1.7 KB  |  83 lines

  1. import pythoncom
  2. import win32com.server.util
  3. import util
  4.  
  5. class Persists:
  6.   _public_methods_ = [ 'GetClassID', 'IsDirty', 'Load', 'Save',
  7.                        'GetSizeMax', 'InitNew' ]
  8.   _com_interfaces_ = [ pythoncom.IID_IPersistStreamInit ]
  9.  
  10.   def GetClassID(self):
  11.     return pythoncom.IID_NULL
  12.  
  13.   def IsDirty(self):
  14.     return 1
  15.  
  16.   def Load(self, stream):
  17.     print "loaded:", stream.Read(26)
  18.  
  19.   def Save(self, stream, clearDirty):
  20.     stream.Write('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
  21.     print "(saved state)"
  22.  
  23.   def GetSizeMax(self):
  24.     return 26
  25.  
  26.   def InitNew(self):
  27.     pass
  28.  
  29.  
  30. class Stream:
  31.   _public_methods_ = [ 'Read', 'Write' ]
  32.   _com_interfaces_ = [ pythoncom.IID_IStream ]
  33.  
  34.   def __init__(self, data):
  35.     self.data = data
  36.     self.index = 0
  37.  
  38.   def Read(self, amount):
  39.     result = self.data[self.index : self.index + amount]
  40.     self.index = self.index + amount
  41.     return result
  42.  
  43.   def Write(self, data):
  44.     self.data = data
  45.     self.index = 0
  46.     return len(data)
  47.  
  48.  
  49. def test():
  50.     mydata = 'abcdefghijklmnopqrstuvwxyz'
  51.  
  52.     # First test the objects just as Python objects...
  53.     s = Stream(mydata)
  54.     p = Persists()
  55.  
  56.     p.Load(s)
  57.     p.Save(s, 0)
  58.     print "new state:", s.data
  59.  
  60.     # reset the stream
  61.     s.Write(mydata)
  62.  
  63.     # Wrap the Python objects as COM objects, and make the calls as if
  64.     # they were non-Python COM objects.
  65.     s2 = win32com.server.util.wrap(s, pythoncom.IID_IStream)
  66.     p2 = win32com.server.util.wrap(p, pythoncom.IID_IPersistStreamInit)
  67.  
  68.     print "read:", s2.Read(26)
  69.     s2.Write("kilroy was here")
  70.     print "new state:", s.data
  71.  
  72.     # reset the stream
  73.     s.Write(mydata)
  74.  
  75.     p2.Load(s2)
  76.     p2.Save(s2, 0)
  77.     print "new state:", s.data
  78.  
  79. if __name__=='__main__':
  80.     test()
  81.     util.CheckClean()
  82.  
  83.