PyXR

c:\python24\lib\site-packages\win32 \ com \ demos \ trybag.py



0001 import pythoncom
0002 from win32com.server import util
0003 from win32com.server import exception
0004 
0005 VT_EMPTY = pythoncom.VT_EMPTY
0006 
0007 class Bag:
0008   _public_methods_ = [ 'Read', 'Write' ]
0009   _com_interfaces_ = [ pythoncom.IID_IPropertyBag ]
0010 
0011   def __init__(self):
0012     self.data = { }
0013 
0014   def Read(self, propName, varType, errorLog):
0015     print "read: name=", propName, "type=", varType
0016     if not self.data.has_key(propName):
0017       if errorLog:
0018         hr = 0x80070057
0019         errorLog.AddError(propName, (0, "Bag.Read", "no such item", None, 0, hr))
0020       raise exception.Exception(scode=hr)
0021     return self.data[propName]
0022 
0023   def Write(self, propName, value):
0024     print "write: name=", propName, "value=", value
0025     self.data[propName] = value
0026 
0027 
0028 class Target:
0029   _public_methods_ = [ 'GetClassID', 'InitNew', 'Load', 'Save' ]
0030   _com_interfaces_ = [ pythoncom.IID_IPersist,
0031                        pythoncom.IID_IPersistPropertyBag ]
0032 
0033   def GetClassID(self):
0034     raise exception.Exception(scode=0x80004005)        # E_FAIL
0035 
0036   def InitNew(self):
0037     pass
0038 
0039   def Load(self, bag, log):
0040     print bag.Read('prop1', VT_EMPTY, log)
0041     print bag.Read('prop2', VT_EMPTY, log)
0042     try:
0043       print bag.Read('prop3', VT_EMPTY, log)
0044     except exception.Exception:
0045       pass
0046 
0047   def Save(self, bag, clearDirty, saveAllProps):
0048     bag.Write('prop1', 'prop1.hello')
0049     bag.Write('prop2', 'prop2.there')
0050 
0051 class Log:
0052   _public_methods_ = [ 'AddError' ]
0053   _com_interfaces_ = [ pythoncom.IID_IErrorLog ]
0054 
0055   def AddError(self, propName, excepInfo):
0056     print "error: propName=", propName, "error=", excepInfo
0057 
0058 def test():
0059   bag = Bag()
0060   target = Target()
0061   log = Log()
0062 
0063   target.Save(bag, 1, 1)
0064   target.Load(bag, log)
0065 
0066   comBag = util.wrap(bag, pythoncom.IID_IPropertyBag)
0067   comTarget = util.wrap(target, pythoncom.IID_IPersistPropertyBag)
0068   comLog = util.wrap(log, pythoncom.IID_IErrorLog)
0069 
0070   comTarget.Save(comBag, 1, 1)
0071   comTarget.Load(comBag, comLog)
0072 
0073 if __name__ == '__main__':
0074   test()
0075 

Generated by PyXR 0.9.4
SourceForge.net Logo