--- a/src/eric7/RemoteServer/EricServer.py Mon Jan 29 19:50:44 2024 +0100 +++ b/src/eric7/RemoteServer/EricServer.py Fri Feb 02 11:29:08 2024 +0100 @@ -35,17 +35,12 @@ @param useIPv6 flag indicating to use IPv6 protocol (defaults to False) @type bool (optional) """ - self.__requestCategoryHandlerRegistry = { - # Dictionary containing the defined and registered request category - # handlers. The key is the request category and the value is the respective - # handler method. This method must have the signature: - # handler(request:str, params:dict, reqestUuid:str) -> None - EricRequestCategory.Debugger: None, # TODO: not implemented yet - EricRequestCategory.Echo: self.__handleEchoRequest, - EricRequestCategory.FileSystem: None, # TODO: not implemented yet - EricRequestCategory.Project: None, # TODO: not implemented yet - EricRequestCategory.Server: self.__handleServerRequest - } + self.__requestCategoryHandlerRegistry = {} + # Dictionary containing the defined and registered request category + # handlers. The key is the request category and the value is the respective + # handler method. This method must have the signature: + # handler(request:str, params:dict, reqestUuid:str) -> None + self.__registerInternalHandlers() self.__connection = None @@ -257,6 +252,57 @@ return cleanExit ####################################################################### + ## Methods for registering and unregistering handlers. + ####################################################################### + + def registerRequestHandler(self, requestCategory, handler): + """ + Public method to register a request handler method for the given request + category. + + @param requestCategory request category to be registered + @type EricRequestCategory or int (>= EricRequestCategory.UserCategory) + @param handler reference to the handler method. This handler must accept + the parameters 'request', 'params', and 'requestUuid' + @type function(request:str, params:dict, requestUuid:str) + @exception ValueError raised to signal a request category collision + """ + if requestCategory in self.__requestCategoryHandlerRegistry: + raise ValueError(f"Request category '{requestCategory} already registered.") + + self.__requestCategoryHandlerRegistry[requestCategory] = handler + + def unregisterRequestHandler(self, requestCategory, ignoreError=False): + """ + Public method to unregister a handler for the given request category. + + Note: This method will raise a KeyError exception in case the request + category has not been registered and ignoreError is False (the default). + + @param requestCategory request category to be unregistered + @type EricRequestCategory or int (>= EricRequestCategory.UserCategory) + @param ignoreError flag indicating to ignore errors (defaults to False) + @type bool (optional) + """ + try: + del self.__requestCategoryHandlerRegistry[requestCategory] + except KeyError: + if not ignoreError: + raise + + def __registerInternalHandlers(self): + """ + Private method to register request handler categories of this class. + """ + self.registerRequestHandler(EricRequestCategory.Echo, self.__handleEchoRequest) + self.registerRequestHandler( + EricRequestCategory.Server, self.__handleServerRequest + ) + self.registerRequestHandler(EricRequestCategory.Error, None) + # Register a None handler to indicate we are not expecting a request of the + # 'Error' category. + + ####################################################################### ## Request handler methods. ####################################################################### @@ -271,15 +317,39 @@ @param params request parameters @type dict @param reqestUuid UUID of the associated request as sent by the eric IDE - (defaults to "", i.e. no UUID received) @type str + @exception ValueError raised to indicate an invalid or unsupported request + handler catehory """ try: handler = self.__requestCategoryHandlerRegistry[category] - if handler is None: - raise ValueError("invalid handler function") + except KeyError: + if ( + category < EricRequestCategory.UserCategory + and category in EricRequestCategory + ): + # it is an internally supported category + if category == EricRequestCategory.FileSystem: + from .EricServerFileSystemRequestHandler import ( + EricServerFileSystemRequestHandler, + ) + handler = EricServerFileSystemRequestHandler(self).handleRequest + + elif category == EricRequestCategory.Project: + # TODO: 'Project' handler not implemented yet + handler = None + + elif category == EricRequestCategory.Debugger: + # TODO: 'Debugger' handler not implemented yet + handler = None + + self.registerRequestHandler(category, handler) + else: + handler = None + + if handler is not None: handler(request=request, params=params, reqestUuid=reqestUuid) - except (KeyError, ValueError): + else: self.sendJson( category=EricRequestCategory.Error, reply="UnsupportedServiceCategory",