def autoRegisterComponents(self, when, directory): directory_path = directory.path self.num_modules_this_register = 0 logger.debug("Auto-registering all Python components in '%s'", directory_path) # ToDo - work out the right thing here # eg - do we recurse? # - do we support packages? entries = directory.directoryEntries while entries.HasMoreElements(): entry = entries.GetNext(components.interfaces.nsIFile) if os.path.splitext(entry.path)[1] == ".py": try: self.autoRegisterComponent(when, entry) # Handle some common user errors except xpcom.COMException, details: from xpcom import nsError # If the interface name does not exist, suppress the traceback if details.errno == nsError.NS_ERROR_NO_INTERFACE: logger.error("Registration of '%s' failed\n %s", entry.leafName, details.message) else: logger.exception("Registration of '%s' failed!", entry.leafName) except SyntaxError, details: # Syntax error in source file - no useful traceback here either. logger.error("Registration of '%s' failed\n %s", entry.leafName, details) except:
def _doHandleException(self, func_name, exc_info): exc_val = exc_info[1] is_server_exception = isinstance(exc_val, ServerException) if is_server_exception: # When a component raised an explicit COM exception, it is # considered 'normal' - however, we still write a debug log # record to help track these otherwise silent exceptions. # Note that Python 2.3 does not allow an explicit exc_info tuple # and passing 'True' will not work as there is no exception pending. # Trick things! if logger.isEnabledFor(logging.DEBUG): try: raise exc_info[0], exc_info[1], exc_info[2] except: logger.debug("'%s' raised COM Exception %s", func_name, exc_val, exc_info=1) return exc_val.errno # Unhandled exception - always print a warning and the traceback. # As above, trick the logging module to handle Python 2.3 try: raise exc_info[0], exc_info[1], exc_info[2] except: logger.exception("Unhandled exception calling '%s'", func_name) return nsError.NS_ERROR_FAILURE
def _MakeInterfaceParam_(self, interface, iid, method_index, mi, param_index): # Wrap a "raw" interface object in a nice object. The result of this # function will be passed to one of the gateway methods. if iid is None: # look up the interface info - this will be true for all xpcom called interfaces. if self._interface_info_ is None: import xpcom.xpt self._interface_info_ = xpcom.xpt.Interface(self._iid_) iid = self._interface_iid_map_.get((method_index, param_index)) if iid is None: iid = self._interface_info_.GetIIDForParam( method_index, param_index) self._interface_iid_map_[(method_index, param_index)] = iid # handle nsIVariant if iid == IID_nsIVariant: interface = interface.QueryInterface(iid) dt = interface.dataType if dt in VARIANT_INT_TYPES: return interface.getAsInt32() if dt in VARIANT_LONG_TYPES: return interface.getAsInt64() if dt in VARIANT_FLOAT_TYPES: return interface.getAsFloat() if dt in VARIANT_STRING_TYPES: return interface.getAsStringWithSize() if dt in VARIANT_UNICODE_TYPES: return interface.getAsWStringWithSize() if dt == xpcom_consts.VTYPE_BOOL: return interface.getAsBool() if dt == xpcom_consts.VTYPE_INTERFACE: return interface.getAsISupports() if dt == xpcom_consts.VTYPE_INTERFACE_IS: return interface.getAsInterface() if dt == xpcom_consts.VTYPE_EMPTY or dt == xpcom_consts.VTYPE_VOID: return None if dt == xpcom_consts.VTYPE_ARRAY: return interface.getAsArray() if dt == xpcom_consts.VTYPE_EMPTY_ARRAY: return [] if dt == xpcom_consts.VTYPE_ID: return interface.getAsID() # all else fails... logger.warning( "Warning: nsIVariant type %d not supported - returning a string", dt) try: return interface.getAsString() except COMException: logger.exception( "Error: failed to get Variant as a string - returning variant object" ) return interface return client.Component(interface, iid)
def autoRegisterComponent(self, when, componentFile): # bool return # Check if we actually need to do anything modtime = componentFile.lastModifiedTime loader_mgr = components.manager.queryInterface( components.interfaces.nsIComponentLoaderManager) if not loader_mgr.hasFileChanged(componentFile, None, modtime): return 1 if self.num_modules_this_register == 0: # New components may have just installed new Python # modules into the main python directory (including new .pth files) # So we ask Python to re-process our site directory. # Note that the pyloader does the equivalent when loading. try: from xpcom import _xpcom import site NS_XPCOM_CURRENT_PROCESS_DIR = "XCurProcD" dirname = _xpcom.GetSpecialDirectory( NS_XPCOM_CURRENT_PROCESS_DIR) dirname.append("python") site.addsitedir(dirname.path) except: logger.exception( "PyXPCOM loader failed to process site directory before component registration" ) self.num_modules_this_register += 1 # auto-register via the module. m = self._getCOMModuleForLocation(componentFile) m.registerSelf(components.manager, componentFile, None, self._reg_component_type_) loader_mgr = components.manager.queryInterface( components.interfaces.nsIComponentLoaderManager) loader_mgr.saveFileInfo(componentFile, None, modtime) return 1
except xpcom.COMException, details: from xpcom import nsError # If the interface name does not exist, suppress the traceback if details.errno == nsError.NS_ERROR_NO_INTERFACE: logger.error("Registration of '%s' failed\n %s", entry.leafName, details.message) else: logger.exception("Registration of '%s' failed!", entry.leafName) except SyntaxError, details: # Syntax error in source file - no useful traceback here either. logger.error("Registration of '%s' failed\n %s", entry.leafName, details) except: # All other exceptions get the full traceback. logger.exception("Registration of '%s' failed.", entry.leafName) def autoRegisterComponent(self, when, componentFile): # bool return # Check if we actually need to do anything modtime = componentFile.lastModifiedTime loader_mgr = components.manager.queryInterface( components.interfaces.nsIComponentLoaderManager) if not loader_mgr.hasFileChanged(componentFile, None, modtime): return 1 if self.num_modules_this_register == 0: # New components may have just installed new Python # modules into the main python directory (including new .pth files) # So we ask Python to re-process our site directory.