axsite.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import win32com.axscript.axscript
  2. import winerror
  3. from win32com.axscript import axscript
  4. from win32com.server import exception, util
  5. import pythoncom
  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(engine,
  11. None,
  12. pythoncom.CLSCTX_SERVER,
  13. pythoncom.IID_IUnknown)
  14. self.eScript = engine.QueryInterface(axscript.IID_IActiveScript)
  15. self.eParse = engine.QueryInterface(axscript.IID_IActiveScriptParse)
  16. self.eSafety = engine.QueryInterface(axscript.IID_IObjectSafety)
  17. self.eScript.SetScriptSite(site)
  18. self.eParse.InitNew()
  19. def __del__(self):
  20. self.Close()
  21. def GetScriptDispatch(self, name = None):
  22. return self.eScript.GetScriptDispatch(name)
  23. def AddNamedItem(self, item, flags):
  24. return self.eScript.AddNamedItem(item, flags)
  25. # Some helpers.
  26. def AddCode(self, code, flags=0):
  27. self.eParse.ParseScriptText(code, None, None, None, 0, 0, flags)
  28. def EvalCode(self, code):
  29. return self.eParse.ParseScriptText(code, None, None, None, 0, 0, axscript.SCRIPTTEXT_ISEXPRESSION)
  30. def Start(self):
  31. # Should maybe check state?
  32. # Do I need to transition through?
  33. self.eScript.SetScriptState(axscript.SCRIPTSTATE_STARTED)
  34. # self.eScript.SetScriptState(axscript.SCRIPTSTATE_CONNECTED)
  35. def Close(self):
  36. if self.eScript:
  37. self.eScript.Close()
  38. self.eScript = self.eParse = self.eSafety = None
  39. def SetScriptState(self, state):
  40. self.eScript.SetScriptState(state)
  41. IActiveScriptSite_methods = [
  42. 'GetLCID',
  43. 'GetItemInfo',
  44. 'GetDocVersionString',
  45. 'OnScriptTerminate',
  46. 'OnStateChange',
  47. 'OnScriptError',
  48. 'OnEnterScript',
  49. 'OnLeaveScript',
  50. ]
  51. class AXSite:
  52. """An Active Scripting site. A Site can have exactly one engine.
  53. """
  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 = axscript.SCRIPTITEM_ISVISIBLE | axscript.SCRIPTITEM_NOCODE | axscript.SCRIPTITEM_GLOBALMEMBERS | axscript.SCRIPTITEM_ISPERSISTENT
  75. for name in self.objModel.keys():
  76. newEngine.AddNamedItem(name, flags)
  77. newEngine.SetScriptState(axscript.SCRIPTSTATE_INITIALIZED)
  78. return newEngine
  79. # B/W compat
  80. _AddEngine = AddEngine
  81. def _Close(self):
  82. self.engine.Close()
  83. self.objModel = {}
  84. def GetLCID(self):
  85. return self.lcid
  86. def GetItemInfo(self, name, returnMask):
  87. if name not in self.objModel:
  88. raise exception.Exception(scode=winerror.TYPE_E_ELEMENTNOTFOUND, desc='item not found')
  89. ### for now, we don't have any type information
  90. if returnMask & axscript.SCRIPTINFO_IUNKNOWN:
  91. return (self.objModel[name], None)
  92. return (None, None)
  93. def GetDocVersionString(self):
  94. return 'Python AXHost version 1.0'
  95. def OnScriptTerminate(self, result, excepInfo):
  96. pass
  97. def OnStateChange(self, state):
  98. pass
  99. def OnScriptError(self, errorInterface):
  100. return winerror.S_FALSE
  101. def OnEnterScript(self):
  102. pass
  103. def OnLeaveScript(self):
  104. pass