home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Shareware / Comunicatii / jyte / jyte.exe / scriptdispatch.py < prev    next >
Text File  |  1999-11-25  |  3KB  |  89 lines

  1. """dynamic dispatch objects for AX Script.
  2.  
  3.  This is an IDispatch object that a scripting host may use to
  4.  query and invoke methods on the main script.  Not may hosts use
  5.  this yet, so it is not well tested!
  6. """
  7.  
  8. import winerror
  9. import types
  10. from win32com.server.exception import COMException
  11. import win32com.server.policy
  12. import win32com.server.util
  13. from win32com.client import Dispatch
  14. import pythoncom
  15. from win32com.axscript import axscript
  16.  
  17. debugging = 0
  18.  
  19. PyIDispatchType = pythoncom.TypeIIDs[pythoncom.IID_IDispatch]
  20.  
  21. def _is_callable(obj):
  22.     return type(obj) in [types.FunctionType, types.MethodType]
  23.     # ignore hasattr(obj, "__call__") as this means all COM objects!
  24.  
  25. class ScriptDispatch:
  26.     _public_methods_ = []
  27.     def __init__(self, engine, scriptNamespace):
  28.         self.engine = engine
  29.         self.scriptNamespace = scriptNamespace
  30.         
  31.     def _dynamic_(self, name, lcid, wFlags, args):
  32.         if wFlags & pythoncom.INVOKE_FUNC:
  33.             # attempt to call a function
  34.             try:
  35.                 func = getattr(self.scriptNamespace, name)
  36.                 if not _is_callable(func):
  37.                     raise AttributeError, name # Not a function.
  38.                 realArgs = []
  39.                 for arg in args:
  40.                     if type(arg)==PyIDispatchType:
  41.                         realArgs.append(Dispatch(arg))
  42.                     else:
  43.                         realArgs.append(arg)
  44.                 try:
  45.                     # xxx - todo - work out what code block to pass???
  46.                     return self.engine.ApplyInScriptedSection(None, func, tuple(realArgs))
  47.                 except COMException, (hr, msg, exc, arg):
  48.                     raise
  49.  
  50.             except AttributeError:
  51.                 if not wFlags & pythoncom.DISPATCH_PROPERTYGET:
  52.                     raise COMException(scode=winerror.DISP_E_MEMBERNOTFOUND)
  53.         if wFlags & pythoncom.DISPATCH_PROPERTYGET:
  54.             # attempt to get a property
  55.             try:
  56.                 ret =  getattr(self.scriptNamespace, name)
  57.                 if _is_callable(ret):
  58.                     raise AttributeError, name # Not a property.
  59.             except AttributeError:
  60.                 raise COMException(scode=winerror.DISP_E_MEMBERNOTFOUND)
  61.             except COMException, instance:
  62.                 raise
  63.             except:
  64.                 ret = self.engine.HandleException()
  65.             return ret
  66.  
  67.         raise COMException(scode=winerror.DISP_E_MEMBERNOTFOUND)
  68.  
  69. class StrictDynamicPolicy(win32com.server.policy.DynamicPolicy):
  70.     def _getdispid_(self, name, fdex):
  71.         try:
  72.             func = getattr(self._obj_.scriptNamespace, str(name))
  73.         except AttributeError:
  74.             raise COMException(scode=winerror.DISP_E_MEMBERNOTFOUND)
  75. #        if not _is_callable(func):
  76.         return win32com.server.policy.DynamicPolicy._getdispid_(self, name, fdex)
  77.  
  78. def _wrap_debug(obj):
  79.     return win32com.server.util.wrap(obj, usePolicy=StrictDynamicPolicy, useDispatcher=win32com.server.policy.DispatcherWin32trace)
  80. def _wrap_nodebug(obj):
  81.     return win32com.server.util.wrap(obj, usePolicy=StrictDynamicPolicy)
  82.  
  83. if debugging:
  84.     _wrap = _wrap_debug
  85. else:
  86.     _wrap = _wrap_nodebug
  87.  
  88. def MakeScriptDispatch(engine, namespace):
  89.     return _wrap(ScriptDispatch(engine, namespace))