From be9c9f045ac1575e24492589f898b5551ba4f93b Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Sat, 11 Feb 2017 12:27:25 +0000 Subject: [PATCH 1/2] Cleanup middleware decorator --- sanic/sanic.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/sanic/sanic.py b/sanic/sanic.py index 951632be..e3a2ea8e 100644 --- a/sanic/sanic.py +++ b/sanic/sanic.py @@ -137,14 +137,12 @@ class Sanic: return response # Decorator - def middleware(self, *args, **kwargs): + def middleware(self, middleware_or_request): """ Decorates and registers middleware to be called before a request can either be called as @app.middleware or @app.middleware('request') """ - attach_to = 'request' - - def register_middleware(middleware): + def register_middleware(middleware, attach_to='request'): if attach_to == 'request': self.request_middleware.append(middleware) if attach_to == 'response': @@ -152,11 +150,11 @@ class Sanic: return middleware # Detect which way this was called, @middleware or @middleware('AT') - if len(args) == 1 and len(kwargs) == 0 and callable(args[0]): - return register_middleware(args[0]) + if callable(middleware_or_request): + return register_middleware(middleware_or_request) + else: - attach_to = args[0] - return register_middleware + return partial(register_middleware, attach_to=middleware_or_request) # Static Files def static(self, uri, file_or_directory, pattern='.+', From e00c9d0ee0148bcc5e83ad983f36658d357e1ad1 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Sat, 11 Feb 2017 12:39:04 +0000 Subject: [PATCH 2/2] Fix line length --- sanic/sanic.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sanic/sanic.py b/sanic/sanic.py index e3a2ea8e..5ab98202 100644 --- a/sanic/sanic.py +++ b/sanic/sanic.py @@ -154,7 +154,8 @@ class Sanic: return register_middleware(middleware_or_request) else: - return partial(register_middleware, attach_to=middleware_or_request) + return partial(register_middleware, + attach_to=middleware_or_request) # Static Files def static(self, uri, file_or_directory, pattern='.+',