Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

trybag.py 2.0KB

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import pythoncom
  2. from win32com.server import exception, util
  3. VT_EMPTY = pythoncom.VT_EMPTY
  4. class Bag:
  5. _public_methods_ = ["Read", "Write"]
  6. _com_interfaces_ = [pythoncom.IID_IPropertyBag]
  7. def __init__(self):
  8. self.data = {}
  9. def Read(self, propName, varType, errorLog):
  10. print("read: name=", propName, "type=", varType)
  11. if propName not in self.data:
  12. if errorLog:
  13. hr = 0x80070057
  14. exc = pythoncom.com_error(0, "Bag.Read", "no such item", None, 0, hr)
  15. errorLog.AddError(propName, exc)
  16. raise exception.Exception(scode=hr)
  17. return self.data[propName]
  18. def Write(self, propName, value):
  19. print("write: name=", propName, "value=", value)
  20. self.data[propName] = value
  21. class Target:
  22. _public_methods_ = ["GetClassID", "InitNew", "Load", "Save"]
  23. _com_interfaces_ = [pythoncom.IID_IPersist, pythoncom.IID_IPersistPropertyBag]
  24. def GetClassID(self):
  25. raise exception.Exception(scode=0x80004005) # E_FAIL
  26. def InitNew(self):
  27. pass
  28. def Load(self, bag, log):
  29. print(bag.Read("prop1", VT_EMPTY, log))
  30. print(bag.Read("prop2", VT_EMPTY, log))
  31. try:
  32. print(bag.Read("prop3", VT_EMPTY, log))
  33. except exception.Exception:
  34. pass
  35. def Save(self, bag, clearDirty, saveAllProps):
  36. bag.Write("prop1", "prop1.hello")
  37. bag.Write("prop2", "prop2.there")
  38. class Log:
  39. _public_methods_ = ["AddError"]
  40. _com_interfaces_ = [pythoncom.IID_IErrorLog]
  41. def AddError(self, propName, excepInfo):
  42. print("error: propName=", propName, "error=", excepInfo)
  43. def test():
  44. bag = Bag()
  45. target = Target()
  46. log = Log()
  47. target.Save(bag, 1, 1)
  48. target.Load(bag, log)
  49. comBag = util.wrap(bag, pythoncom.IID_IPropertyBag)
  50. comTarget = util.wrap(target, pythoncom.IID_IPersistPropertyBag)
  51. comLog = util.wrap(log, pythoncom.IID_IErrorLog)
  52. comTarget.Save(comBag, 1, 1)
  53. comTarget.Load(comBag, comLog)
  54. if __name__ == "__main__":
  55. test()