From 54b47c4a3eae6385a04e230c2df0e631c2c1670a Mon Sep 17 00:00:00 2001 From: Unrud Date: Fri, 10 Jun 2016 14:30:58 +0200 Subject: [PATCH] Refactor: Move response code into function --- radicale/__init__.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/radicale/__init__.py b/radicale/__init__.py index 51bb34c..8d36ee7 100644 --- a/radicale/__init__.py +++ b/radicale/__init__.py @@ -218,6 +218,15 @@ class Application: def __call__(self, environ, start_response): """Manage a request.""" + def response(status, headers={}, answer=None): + # Start response + status = "%i %s" % (status, + client.responses.get(status, "Unknown")) + self.logger.debug("Answer status: %s" % status) + start_response(status, list(headers.items())) + # Return response content + return [answer] if answer else [] + self.logger.info("%s request at %s received" % ( environ["REQUEST_METHOD"], environ["PATH_INFO"])) headers = pprint.pformat(self.headers_log(environ)) @@ -234,9 +243,7 @@ class Application: # Request path not starting with base_prefix, not allowed self.logger.debug( "Path not starting with prefix: %s", environ["PATH_INFO"]) - status, headers, _ = NOT_ALLOWED - start_response(status, list(headers.items())) - return [] + return response(*NOT_ALLOWED) # Sanitize request URI environ["PATH_INFO"] = storage.sanitize_path( @@ -275,10 +282,7 @@ class Application: status = client.SEE_OTHER self.logger.info("/.well-known/ redirection to: %s" % redirect) headers = {"Location": redirect} - status = "%i %s" % ( - status, client.responses.get(status, "Unknown")) - start_response(status, list(headers.items())) - return [] + return response(status, headers) is_authenticated = self.is_authenticated(user, password) is_valid_user = is_authenticated or not user @@ -342,13 +346,7 @@ class Application: for key in self.configuration.options("headers"): headers[key] = self.configuration.get("headers", key) - # Start response - status = "%i %s" % (status, client.responses.get(status, "Unknown")) - self.logger.debug("Answer status: %s" % status) - start_response(status, list(headers.items())) - - # Return response content - return [answer] if answer else [] + return response(status, headers, answer) # All these functions must have the same parameters, some are useless # pylint: disable=W0612,W0613,R0201