def AddNamedItem(self, name, flags): if self.scriptSite is None: raise Exception(scode=winerror.E_INVALIDARG) try: unknown = self.scriptSite.GetItemInfo(name, axscript.SCRIPTINFO_IUNKNOWN)[0] dispatch = unknown.QueryInterface(pythoncom.IID_IDispatch) except pythoncom.com_error: raise Exception(scode=winerror.E_NOINTERFACE, desc="Object has no dispatch interface available.") newItem = self.subItems[name] = self.GetNamedItemClass()(self, name, dispatch, flags) if newItem.IsGlobal(): newItem.CreateConnections()
def SetScriptState(self, state): # print "SetScriptState with %s - currentstate = %s" % (state_map.get(state),state_map.get(self.scriptState)) if state == self.scriptState: return # If closed, allow no other state transitions if self.scriptState == axscript.SCRIPTSTATE_CLOSED: raise Exception(scode=winerror.E_INVALIDARG) if state == axscript.SCRIPTSTATE_INITIALIZED: # Re-initialize - shutdown then reset. if self.scriptState in [ axscript.SCRIPTSTATE_CONNECTED, axscript.SCRIPTSTATE_STARTED, ]: self.Stop() elif state == axscript.SCRIPTSTATE_STARTED: if self.scriptState == axscript.SCRIPTSTATE_CONNECTED: self.Disconnect() if self.scriptState == axscript.SCRIPTSTATE_DISCONNECTED: self.Reset() self.Run() self.ChangeScriptState(axscript.SCRIPTSTATE_STARTED) elif state == axscript.SCRIPTSTATE_CONNECTED: if self.scriptState in [ axscript.SCRIPTSTATE_UNINITIALIZED, axscript.SCRIPTSTATE_INITIALIZED, ]: self.ChangeScriptState( axscript.SCRIPTSTATE_STARTED ) # report transition through started self.Run() if self.scriptState == axscript.SCRIPTSTATE_STARTED: self.Connect() self.ChangeScriptState(state) elif state == axscript.SCRIPTSTATE_DISCONNECTED: if self.scriptState == axscript.SCRIPTSTATE_CONNECTED: self.Disconnect() elif state == axscript.SCRIPTSTATE_CLOSED: self.Close() elif state == axscript.SCRIPTSTATE_UNINITIALIZED: if self.scriptState == axscript.SCRIPTSTATE_STARTED: self.Stop() if self.scriptState == axscript.SCRIPTSTATE_CONNECTED: self.Disconnect() if self.scriptState == axscript.SCRIPTSTATE_DISCONNECTED: self.Reset() self.ChangeScriptState(state) else: raise Exception(scode=winerror.E_INVALIDARG)
def GetCreateSubItem(self, parentItem, name, dispatch, flags): keyName = name.lower() try: rc = self.subItems[keyName] # No changes allowed to existing flags. if not rc.flags is None and not flags is None and rc.flags != flags: raise Exception(scode=winerror.E_INVALIDARG) # Existing item must not have a dispatch. if not rc.dispatch is None and not dispatch is None: raise Exception(scode=winerror.E_INVALIDARG) rc.flags = flags # Setup the real flags. rc.dispatch = dispatch except KeyError: rc = self.subItems[keyName] = self.GetSubItemClass()(parentItem, name, dispatch, flags) return rc
def Eval(self, exp): """Evaluate an expression. """ if type(exp) not in [str, str]: raise Exception(desc="Must be a string", scode=winerror.DISP_E_TYPEMISMATCH) return eval(str(exp), self.dict)
def Exec(self, exp): """Execute a statement. """ if type(exp) not in [str, unicode]: raise Exception(desc="Must be a string", scode=winerror.DISP_E_TYPEMISMATCH) exec str(exp) in self.dict
def HandleException(self, codeBlock): # NOTE - Never returns - raises a ComException exc_type, exc_value, exc_traceback = sys.exc_info() # If a SERVER exception, re-raise it. If a client side COM error, it is # likely to have originated from the script code itself, and therefore # needs to be reported like any other exception. if IsCOMServerException(exc_type): # Ensure the traceback doesnt cause a cycle. exc_traceback = None raise # It could be an error by another script. if issubclass(pythoncom.com_error, exc_type) and exc_value[0]==axscript.SCRIPT_E_REPORTED: # Ensure the traceback doesnt cause a cycle. exc_traceback = None raise Exception(scode=exc_value[0]) exception = error.AXScriptException(self, \ codeBlock, exc_type, exc_value, exc_traceback) # Ensure the traceback doesnt cause a cycle. exc_traceback = None result_exception = error.ProcessAXScriptException(self.scriptSite, self.debugManager, exception) if result_exception is not None: try: self.scriptSite.OnScriptTerminate(None, result_exception) except pythoncom.com_error: pass # Ignore errors telling engine we stopped. # reset ourselves to 'connected' so further events continue to fire. self.SetScriptState(axscript.SCRIPTSTATE_CONNECTED) raise result_exception # I think that in some cases this should just return - but the code # that could return None above is disabled, so it never happens. RaiseAssert(winerror.E_UNEXPECTED, "Don't have an exception to raise to the caller!")
def write(self, *args): if len(args)==0: raise Exception(scode=winerror.DISP_E_BADPARAMCOUNT) # Probably call as PROPGET. for arg in args[:-1]: print str(arg), print str(args[-1])
def GetInterfaceSafetyOptions(self, iid): if iid in [pythoncom.IID_IPersist, pythoncom.IID_IPersistStream, pythoncom.IID_IPersistStreamInit, axscript.IID_IActiveScript, axscript.IID_IActiveScriptParse]: supported = self._GetSupportedInterfaceSafetyOptions() return supported, self.safetyOptions else: raise Exception(scode=winerror.E_NOINTERFACE)
def Exec(self, exp): """Execute a statement. """ if type(exp) != str: raise Exception(desc="Must be a string", scode=winerror.DISP_E_TYPEMISMATCH) exec(str(exp), self.dict)
def _invoke_(self, dispid, lcid, wFlags, args): try: event = self.events[dispid] except: raise Exception(scode=winerror.DISP_E_MEMBERNOTFOUND) # print "Invoke for ", event, "on", self.myScriptItem, " - calling", self.myInvokeMethod return self.myInvokeMethod(self.myScriptItem, event, lcid, wFlags, args)
def Run(self): # trace("AXScript running...") if self.scriptState != axscript.SCRIPTSTATE_INITIALIZED and self.scriptState != axscript.SCRIPTSTATE_STARTED: raise Exception(scode=winerror.E_UNEXPECTED) # self._DumpNamedItems_() self.ExecutePendingScripts() self.DoRun()
def _dynamic_(self, name, lcid, wFlags, args): if wFlags & pythoncom.DISPATCH_METHOD: return getattr(self, name)(*args) if wFlags & pythoncom.DISPATCH_PROPERTYGET: try: # to avoid problems with byref param handling, tuple results are converted to lists. ret = self.__dict__[name] if type(ret) == type(()): ret = list(ret) return ret except KeyError: # Probably a method request. raise Exception(scode=winerror.DISP_E_MEMBERNOTFOUND) if wFlags & (pythoncom.DISPATCH_PROPERTYPUT | pythoncom.DISPATCH_PROPERTYPUTREF): setattr(self, name, args[0]) return raise Exception(scode=winerror.E_INVALIDARG, desc="invalid wFlags")
def _dynamic_(self, name, lcID, wFlags, args): if wFlags & pythoncom.DISPATCH_METHOD: return getattr(self, name)(*args) if wFlags & pythoncom.DISPATCH_PROPERTYGET: try: # converting tuple results to list helps with byref params or something ret = self.__dict__[name] if type(ret) == type(()): ret = list(ret) return ret except KeyError: # "probably a method request" raise Exception(scode=winerror.DISP_E_MEMBERNOTFOUND) if wFlags & (pythoncom.DISPATCH_PROPERTYPUT | pythoncom.DISPATCH_PROPERTYPUTREF): setattr(self, name, args[0]) return raise Exception(scode=winerror.E_INVALIDARG, desc="invalid wFlags")
def GetPathName(self): # Result must be (string, int) where the int is a BOOL # - TRUE if the path refers to the original file for the document. # - FALSE if the path refers to a newly created temporary file. # - raise Exception(scode=E_FAIL) if no source file can be created/determined. trace("GetPathName") try: return win32api.GetFullPathName(self.module.__file__), 1 except (AttributeError, win32api.error): raise Exception(scode == E_FAIL)
def GetLineOfPosition(self, charPos): self.GetText() # Prime us. lastOffset = 0 lineNo = 0 for lineOffset in self.lineOffsets[1:]: if lineOffset > charPos: break lastOffset = lineOffset lineNo = lineNo + 1 else: # for not broken. # print "Cant find", charPos, "in", self.lineOffsets raise Exception(scode=winerror.S_FALSE) # print "GLOP ret=",lineNo, (charPos-lastOffset) return lineNo, (charPos - lastOffset)
def RaiseNotImpl(who=None): if who is not None: print "********* Function %s Raising E_NOTIMPL ************" % (who) # Print a sort-of "traceback", dumping all the frames leading to here. try: 1 / 0 except: frame = sys.exc_info()[2].tb_frame while frame: print "File: %s, Line: %d" % (frame.f_code.co_filename, frame.f_lineno) frame = frame.f_back # and raise the exception for COM raise Exception(scode=winerror.E_NOTIMPL)
def SetInterfaceSafetyOptions(self, iid, optionsMask, enabledOptions): # trace ("SetInterfaceSafetyOptions", iid, optionsMask, enabledOptions) if optionsMask & enabledOptions == 0: return # See comments above. # if (optionsMask & enabledOptions & \ # ~(axscript.INTERFACESAFE_FOR_UNTRUSTED_DATA | axscript.INTERFACESAFE_FOR_UNTRUSTED_CALLER)): # # request for options we don't understand # RaiseAssert(scode=winerror.E_FAIL, desc="Unknown safety options") if iid in [pythoncom.IID_IPersist, pythoncom.IID_IPersistStream, pythoncom.IID_IPersistStreamInit, axscript.IID_IActiveScript, axscript.IID_IActiveScriptParse]: supported = self._GetSupportedInterfaceSafetyOptions() self.safetyOptions = supported & optionsMask & enabledOptions else: raise Exception(scode=winerror.E_NOINTERFACE)
def GetName(self, dnt): name = self.module.__name__ try: fname = win32api.GetFullPathName(self.module.__file__) except win32api.error: fname = self.module.__file__ except AttributeError: fname = name if dnt == axdebug.DOCUMENTNAMETYPE_APPNODE: return name.split(".")[-1] elif dnt == axdebug.DOCUMENTNAMETYPE_TITLE: return fname elif dnt == axdebug.DOCUMENTNAMETYPE_FILE_TAIL: return os.path.split(fname)[1] elif dnt == axdebug.DOCUMENTNAMETYPE_URL: return "file:%s" % fname else: raise Exception(scode=winerror.E_UNEXPECTED)
def EndScriptedSection(self): if self.scriptSite is None: raise Exception(scode=winerror.E_UNEXPECTED) self.scriptSite.OnLeaveScript()
def FindConnectionPoint(self, iid): # Find a connection we support. Only support the single event interface. if iid == axdebug.IID_IDebugDocumentTextEvents: return _wrap(self) raise Exception(scode=winerror.E_NOINTERFACE) # ??
def Unadvise(self, cookie): # Destroy a connection - simply delete interface from the map. try: del self.connections[cookie] except KeyError: return Exception(scode=winerror.E_UNEXPECTED)
def Clone(self): raise Exception("Not Implemented", scode=winerror.E_NOTIMPL)
def GetScriptThreadState(self, scriptThreadId): if self.baseThreadId == -1: raise Exception(scode=winerror.E_UNEXPECTED) if scriptThreadId != self.baseThreadId: raise Exception(scode=winerror.E_INVALIDARG) return self.threadState
def GetScriptDispatch(self, name): # Base classes should override. raise Exception(scode=winerror.E_NOTIMPL)
def GetScriptSite(self, iid): if self.scriptSite is None: raise Exception(scode=winerror.S_FALSE) return self.scriptSite.QueryInterface(iid)
def GetNamedItem(self, name): try: return self.subItems[name] except KeyError: raise Exception(scode=winerror.E_INVALIDARG)
def OnCreateDocumentContext(self): # Result must be a PyIUnknown trace("OnCreateDocumentContext") raise Exception(scode=winerror.E_NOTIMPL)
def BeginScriptedSection(self): if self.scriptSite is None: raise Exception(scode=winerror.E_UNEXPECTED) self.scriptSite.OnEnterScript()
def RaiseAssert(scode, desc): """A debugging function that raises an exception considered an "Assertion". """ print "**************** ASSERTION FAILED *******************" print desc raise Exception(desc, scode)
def GetScriptTextAttributes(self, codeText, delimterText, flags): # Result must be an attribute sequence of same "length" as the code. trace("GetScriptTextAttributes", delimterText, flags) raise Exception(scode=winerror.E_NOTIMPL)