def __extract_help(method): """ Formats the help string for the given method :param method: The method to document :return: A tuple: (arguments list, documentation line) """ if method is None: return "(No associated method)" # Get the arguments arg_spec = get_method_arguments(method) # Ignore the session argument start_arg = 1 # Compute the number of arguments with default value if arg_spec.defaults is not None: nb_optional = len(arg_spec.defaults) # Let the mandatory arguments as they are args = [ "<{0}>".format(arg) for arg in arg_spec.args[start_arg:-nb_optional] ] # Add the other arguments for name, value in zip( arg_spec.args[-nb_optional:], arg_spec.defaults[-nb_optional:] ): if value is not None: args.append("[<{0}>={1}]".format(name, value)) else: args.append("[<{0}>]".format(name)) else: # All arguments are mandatory args = ["<{0}>".format(arg) for arg in arg_spec.args[start_arg:]] # Extra arguments if arg_spec.keywords: args.append("[<property=value> ...]") if arg_spec.varargs: args.append("[<{0} ...>]".format(arg_spec.varargs)) # Get the documentation string doc = inspect.getdoc(method) or "(Documentation missing)" return " ".join(args), " ".join(doc.split())
def _rest_dispatch(self, request, response): # type: (AbstractHTTPServletRequest, AbstractHTTPServletResponse) -> None """ Dispatches the request :param request: Request bean :param response: Response bean """ # Extract request information http_verb = request.get_command() sub_path = request.get_sub_path() # Find the best matching method, according to the number of # readable arguments max_valid_args = -1 best_method = None best_args = None best_match = None for route, method in self.__routes.get(http_verb, {}).items(): # Parse the request path match = route.match(sub_path) if not match: continue # Count the number of valid arguments method_args = self.__methods_args[method] nb_valid_args = 0 for name in method_args: try: match.group(name) nb_valid_args += 1 except IndexError: # Argument not found pass if nb_valid_args > max_valid_args: # Found a better match max_valid_args = nb_valid_args best_method = method best_args = method_args best_match = match if best_method is None: # No match: return a 404 plain text error response.send_content( 404, "No method to handle path {0}".format(sub_path), "text/plain", ) else: # Found a method # ... convert arguments kwargs = {} if best_args: for name, converter in best_args.items(): try: str_value = best_match.group(name) except IndexError: # Argument is missing: do nothing pass else: if str_value: # Keep the default value when an argument is # missing, i.e. don't give it in kwargs if converter is not None: # Convert the argument kwargs[name] = converter(str_value) else: # Use the string value as is kwargs[name] = str_value # Prepare positional arguments extra_pos_args = [] if kwargs: # Ignore the first two parameters (request and response) method_args = get_method_arguments(best_method).args[:2] for pos_arg in method_args: try: extra_pos_args.append(kwargs.pop(pos_arg)) except KeyError: pass # ... call the method (exceptions will be handled by the server) best_method(request, response, *extra_pos_args, **kwargs)