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.

axsite.py 4.0KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import pythoncom
  2. import win32com.axscript.axscript
  3. import winerror
  4. from win32com.axscript import axscript
  5. from win32com.server import exception, util
  6. class AXEngine:
  7. def __init__(self, site, engine):
  8. self.eScript = self.eParse = self.eSafety = None
  9. if type(engine) == type(""):
  10. engine = pythoncom.CoCreateInstance(
  11. engine, None, pythoncom.CLSCTX_SERVER, pythoncom.IID_IUnknown
  12. )
  13. self.eScript = engine.QueryInterface(axscript.IID_IActiveScript)
  14. self.eParse = engine.QueryInterface(axscript.IID_IActiveScriptParse)
  15. self.eSafety = engine.QueryInterface(axscript.IID_IObjectSafety)
  16. self.eScript.SetScriptSite(site)
  17. self.eParse.InitNew()
  18. def __del__(self):
  19. self.Close()
  20. def GetScriptDispatch(self, name=None):
  21. return self.eScript.GetScriptDispatch(name)
  22. def AddNamedItem(self, item, flags):
  23. return self.eScript.AddNamedItem(item, flags)
  24. # Some helpers.
  25. def AddCode(self, code, flags=0):
  26. self.eParse.ParseScriptText(code, None, None, None, 0, 0, flags)
  27. def EvalCode(self, code):
  28. return self.eParse.ParseScriptText(
  29. code, None, None, None, 0, 0, axscript.SCRIPTTEXT_ISEXPRESSION
  30. )
  31. def Start(self):
  32. # Should maybe check state?
  33. # Do I need to transition through?
  34. self.eScript.SetScriptState(axscript.SCRIPTSTATE_STARTED)
  35. # self.eScript.SetScriptState(axscript.SCRIPTSTATE_CONNECTED)
  36. def Close(self):
  37. if self.eScript:
  38. self.eScript.Close()
  39. self.eScript = self.eParse = self.eSafety = None
  40. def SetScriptState(self, state):
  41. self.eScript.SetScriptState(state)
  42. IActiveScriptSite_methods = [
  43. "GetLCID",
  44. "GetItemInfo",
  45. "GetDocVersionString",
  46. "OnScriptTerminate",
  47. "OnStateChange",
  48. "OnScriptError",
  49. "OnEnterScript",
  50. "OnLeaveScript",
  51. ]
  52. class AXSite:
  53. """An Active Scripting site. A Site can have exactly one engine."""
  54. _public_methods_ = IActiveScriptSite_methods
  55. _com_interfaces_ = [axscript.IID_IActiveScriptSite]
  56. def __init__(self, objModel={}, engine=None, lcid=0):
  57. self.lcid = lcid
  58. self.objModel = {}
  59. for name, object in objModel.items():
  60. # Gregs code did string.lower this - I think that is callers job if he wants!
  61. self.objModel[name] = object
  62. self.engine = None
  63. if engine:
  64. self._AddEngine(engine)
  65. def AddEngine(self, engine):
  66. """Adds a new engine to the site.
  67. engine can be a string, or a fully wrapped engine object.
  68. """
  69. if type(engine) == type(""):
  70. newEngine = AXEngine(util.wrap(self), engine)
  71. else:
  72. newEngine = engine
  73. self.engine = newEngine
  74. flags = (
  75. axscript.SCRIPTITEM_ISVISIBLE
  76. | axscript.SCRIPTITEM_NOCODE
  77. | axscript.SCRIPTITEM_GLOBALMEMBERS
  78. | axscript.SCRIPTITEM_ISPERSISTENT
  79. )
  80. for name in self.objModel.keys():
  81. newEngine.AddNamedItem(name, flags)
  82. newEngine.SetScriptState(axscript.SCRIPTSTATE_INITIALIZED)
  83. return newEngine
  84. # B/W compat
  85. _AddEngine = AddEngine
  86. def _Close(self):
  87. self.engine.Close()
  88. self.objModel = {}
  89. def GetLCID(self):
  90. return self.lcid
  91. def GetItemInfo(self, name, returnMask):
  92. if name not in self.objModel:
  93. raise exception.Exception(
  94. scode=winerror.TYPE_E_ELEMENTNOTFOUND, desc="item not found"
  95. )
  96. ### for now, we don't have any type information
  97. if returnMask & axscript.SCRIPTINFO_IUNKNOWN:
  98. return (self.objModel[name], None)
  99. return (None, None)
  100. def GetDocVersionString(self):
  101. return "Python AXHost version 1.0"
  102. def OnScriptTerminate(self, result, excepInfo):
  103. pass
  104. def OnStateChange(self, state):
  105. pass
  106. def OnScriptError(self, errorInterface):
  107. return winerror.S_FALSE
  108. def OnEnterScript(self):
  109. pass
  110. def OnLeaveScript(self):
  111. pass