2017-01-30 01:39:55 +00:00
|
|
|
from collections import defaultdict, namedtuple
|
2016-10-21 12:11:18 +01:00
|
|
|
|
2017-02-14 05:23:22 +00:00
|
|
|
from sanic.constants import HTTP_METHODS
|
2017-02-14 08:15:38 +00:00
|
|
|
from sanic.views import CompositionView
|
2016-10-21 12:11:18 +01:00
|
|
|
|
2017-03-24 01:37:06 +00:00
|
|
|
FutureRoute = namedtuple('Route',
|
|
|
|
['handler', 'uri', 'methods',
|
|
|
|
'host', 'strict_slashes'])
|
2017-01-30 01:39:55 +00:00
|
|
|
FutureListener = namedtuple('Listener', ['handler', 'uri', 'methods', 'host'])
|
|
|
|
FutureMiddleware = namedtuple('Route', ['middleware', 'args', 'kwargs'])
|
|
|
|
FutureException = namedtuple('Route', ['handler', 'args', 'kwargs'])
|
2017-01-30 01:44:46 +00:00
|
|
|
FutureStatic = namedtuple('Route',
|
|
|
|
['uri', 'file_or_directory', 'args', 'kwargs'])
|
2016-10-16 09:48:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Blueprint:
|
2017-01-11 02:07:58 +00:00
|
|
|
def __init__(self, name, url_prefix=None, host=None):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""Create a new blueprint
|
|
|
|
|
|
|
|
:param name: unique name of the blueprint
|
2016-10-21 12:11:18 +01:00
|
|
|
:param url_prefix: URL to be prefixed before all route URLs
|
|
|
|
"""
|
2016-10-15 20:53:51 +01:00
|
|
|
self.name = name
|
|
|
|
self.url_prefix = url_prefix
|
2017-01-11 02:07:58 +00:00
|
|
|
self.host = host
|
2016-10-15 20:53:51 +01:00
|
|
|
|
2017-01-30 01:39:55 +00:00
|
|
|
self.routes = []
|
2017-02-21 06:41:53 +00:00
|
|
|
self.websocket_routes = []
|
2017-01-30 01:39:55 +00:00
|
|
|
self.exceptions = []
|
|
|
|
self.listeners = defaultdict(list)
|
|
|
|
self.middlewares = []
|
|
|
|
self.statics = []
|
2016-10-15 20:53:51 +01:00
|
|
|
|
|
|
|
def register(self, app, options):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""Register the blueprint to the sanic app."""
|
2017-01-30 01:39:55 +00:00
|
|
|
|
|
|
|
url_prefix = options.get('url_prefix', self.url_prefix)
|
|
|
|
|
|
|
|
# Routes
|
|
|
|
for future in self.routes:
|
2017-02-02 17:21:14 +00:00
|
|
|
# attach the blueprint name to the handler so that it can be
|
|
|
|
# prefixed properly in the router
|
|
|
|
future.handler.__blueprintname__ = self.name
|
2017-01-30 01:39:55 +00:00
|
|
|
# Prepend the blueprint URI prefix if available
|
|
|
|
uri = url_prefix + future.uri if url_prefix else future.uri
|
|
|
|
app.route(
|
2017-02-23 16:17:43 +00:00
|
|
|
uri=uri[1:] if uri.startswith('//') else uri,
|
2017-01-30 01:39:55 +00:00
|
|
|
methods=future.methods,
|
2017-03-24 01:37:06 +00:00
|
|
|
host=future.host or self.host,
|
|
|
|
strict_slashes=future.strict_slashes
|
2017-01-30 01:39:55 +00:00
|
|
|
)(future.handler)
|
|
|
|
|
2017-02-21 06:41:53 +00:00
|
|
|
for future in self.websocket_routes:
|
|
|
|
# attach the blueprint name to the handler so that it can be
|
|
|
|
# prefixed properly in the router
|
|
|
|
future.handler.__blueprintname__ = self.name
|
|
|
|
# Prepend the blueprint URI prefix if available
|
|
|
|
uri = url_prefix + future.uri if url_prefix else future.uri
|
|
|
|
app.websocket(
|
|
|
|
uri=uri,
|
2017-03-24 01:37:06 +00:00
|
|
|
host=future.host or self.host,
|
|
|
|
strict_slashes=future.strict_slashes
|
2017-02-21 06:41:53 +00:00
|
|
|
)(future.handler)
|
|
|
|
|
2017-01-30 01:39:55 +00:00
|
|
|
# Middleware
|
|
|
|
for future in self.middlewares:
|
|
|
|
if future.args or future.kwargs:
|
2017-01-30 01:44:46 +00:00
|
|
|
app.middleware(*future.args,
|
|
|
|
**future.kwargs)(future.middleware)
|
2017-01-30 01:39:55 +00:00
|
|
|
else:
|
|
|
|
app.middleware(future.middleware)
|
|
|
|
|
|
|
|
# Exceptions
|
|
|
|
for future in self.exceptions:
|
|
|
|
app.exception(*future.args, **future.kwargs)(future.handler)
|
|
|
|
|
|
|
|
# Static Files
|
|
|
|
for future in self.statics:
|
|
|
|
# Prepend the blueprint URI prefix if available
|
|
|
|
uri = url_prefix + future.uri if url_prefix else future.uri
|
2017-01-30 01:44:46 +00:00
|
|
|
app.static(uri, future.file_or_directory,
|
|
|
|
*future.args, **future.kwargs)
|
2016-10-15 20:53:51 +01:00
|
|
|
|
2017-02-11 14:30:17 +00:00
|
|
|
# Event listeners
|
|
|
|
for event, listeners in self.listeners.items():
|
|
|
|
for listener in listeners:
|
|
|
|
app.listener(event)(listener)
|
|
|
|
|
2017-03-24 01:37:06 +00:00
|
|
|
def route(self, uri, methods=frozenset({'GET'}), host=None,
|
|
|
|
strict_slashes=False):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""Create a blueprint route from a decorated function.
|
|
|
|
|
|
|
|
:param uri: endpoint at which the route will be accessible.
|
|
|
|
:param methods: list of acceptable HTTP methods.
|
2016-10-15 20:53:51 +01:00
|
|
|
"""
|
|
|
|
def decorator(handler):
|
2017-03-24 01:37:06 +00:00
|
|
|
route = FutureRoute(handler, uri, methods, host, strict_slashes)
|
2017-01-30 01:39:55 +00:00
|
|
|
self.routes.append(route)
|
2016-10-15 20:53:51 +01:00
|
|
|
return handler
|
|
|
|
return decorator
|
|
|
|
|
2017-03-24 01:37:06 +00:00
|
|
|
def add_route(self, handler, uri, methods=frozenset({'GET'}), host=None,
|
|
|
|
strict_slashes=False):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""Create a blueprint route from a function.
|
|
|
|
|
|
|
|
:param handler: function for handling uri requests. Accepts function,
|
2017-02-14 08:20:39 +00:00
|
|
|
or class instance with a view_class method.
|
2017-02-14 19:10:19 +00:00
|
|
|
:param uri: endpoint at which the route will be accessible.
|
|
|
|
:param methods: list of acceptable HTTP methods.
|
2017-02-14 05:23:22 +00:00
|
|
|
:return: function or class instance
|
2016-11-25 07:10:25 +00:00
|
|
|
"""
|
2017-02-14 05:23:22 +00:00
|
|
|
# Handle HTTPMethodView differently
|
|
|
|
if hasattr(handler, 'view_class'):
|
2017-02-14 08:15:38 +00:00
|
|
|
methods = set()
|
|
|
|
|
|
|
|
for method in HTTP_METHODS:
|
|
|
|
if getattr(handler.view_class, method.lower(), None):
|
|
|
|
methods.add(method)
|
|
|
|
|
|
|
|
# handle composition view differently
|
|
|
|
if isinstance(handler, CompositionView):
|
|
|
|
methods = handler.handlers.keys()
|
|
|
|
|
2017-03-24 01:37:06 +00:00
|
|
|
self.route(uri=uri, methods=methods, host=host,
|
|
|
|
strict_slashes=strict_slashes)(handler)
|
2016-11-25 07:10:25 +00:00
|
|
|
return handler
|
|
|
|
|
2017-03-24 01:37:06 +00:00
|
|
|
def websocket(self, uri, host=None, strict_slashes=False):
|
2017-02-21 06:41:53 +00:00
|
|
|
"""Create a blueprint websocket route from a decorated function.
|
|
|
|
|
|
|
|
:param uri: endpoint at which the route will be accessible.
|
|
|
|
"""
|
|
|
|
def decorator(handler):
|
2017-03-24 01:37:06 +00:00
|
|
|
route = FutureRoute(handler, uri, [], host, strict_slashes)
|
2017-02-21 06:41:53 +00:00
|
|
|
self.websocket_routes.append(route)
|
|
|
|
return handler
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
def add_websocket_route(self, handler, uri, host=None):
|
|
|
|
"""Create a blueprint websocket route from a function.
|
|
|
|
|
|
|
|
:param handler: function for handling uri requests. Accepts function,
|
|
|
|
or class instance with a view_class method.
|
|
|
|
:param uri: endpoint at which the route will be accessible.
|
|
|
|
:return: function or class instance
|
|
|
|
"""
|
|
|
|
self.websocket(uri=uri, host=host)(handler)
|
|
|
|
return handler
|
|
|
|
|
2016-10-21 12:11:18 +01:00
|
|
|
def listener(self, event):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""Create a listener from a decorated function.
|
|
|
|
|
2016-12-25 05:47:51 +00:00
|
|
|
:param event: Event to listen to.
|
2016-10-21 12:11:18 +01:00
|
|
|
"""
|
|
|
|
def decorator(listener):
|
|
|
|
self.listeners[event].append(listener)
|
|
|
|
return listener
|
|
|
|
return decorator
|
|
|
|
|
2016-10-16 09:48:51 +01:00
|
|
|
def middleware(self, *args, **kwargs):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""Create a blueprint middleware from a decorated function."""
|
2017-01-30 01:39:55 +00:00
|
|
|
def register_middleware(_middleware):
|
|
|
|
future_middleware = FutureMiddleware(_middleware, args, kwargs)
|
|
|
|
self.middlewares.append(future_middleware)
|
|
|
|
return _middleware
|
2016-10-16 09:48:51 +01:00
|
|
|
|
|
|
|
# Detect which way this was called, @middleware or @middleware('AT')
|
|
|
|
if len(args) == 1 and len(kwargs) == 0 and callable(args[0]):
|
2016-11-06 16:08:55 +00:00
|
|
|
middleware = args[0]
|
2016-10-16 09:48:51 +01:00
|
|
|
args = []
|
2016-11-06 16:08:55 +00:00
|
|
|
return register_middleware(middleware)
|
2016-10-16 09:48:51 +01:00
|
|
|
else:
|
|
|
|
return register_middleware
|
|
|
|
|
|
|
|
def exception(self, *args, **kwargs):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""Create a blueprint exception from a decorated function."""
|
2016-10-16 09:48:51 +01:00
|
|
|
def decorator(handler):
|
2017-01-30 01:39:55 +00:00
|
|
|
exception = FutureException(handler, args, kwargs)
|
|
|
|
self.exceptions.append(exception)
|
2016-10-16 09:48:51 +01:00
|
|
|
return handler
|
|
|
|
return decorator
|
2016-10-24 09:21:06 +01:00
|
|
|
|
2016-10-25 10:45:28 +01:00
|
|
|
def static(self, uri, file_or_directory, *args, **kwargs):
|
2017-02-14 19:10:19 +00:00
|
|
|
"""Create a blueprint static route from a decorated function.
|
|
|
|
|
|
|
|
:param uri: endpoint at which the route will be accessible.
|
2016-12-25 05:47:51 +00:00
|
|
|
:param file_or_directory: Static asset.
|
2016-10-24 09:21:06 +01:00
|
|
|
"""
|
2017-01-30 01:39:55 +00:00
|
|
|
static = FutureStatic(uri, file_or_directory, args, kwargs)
|
|
|
|
self.statics.append(static)
|
2017-01-30 07:20:38 +00:00
|
|
|
|
|
|
|
# Shorthand method decorators
|
2017-03-24 01:37:06 +00:00
|
|
|
def get(self, uri, host=None, strict_slashes=False):
|
|
|
|
return self.route(uri, methods=["GET"], host=host,
|
|
|
|
strict_slashes=strict_slashes)
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2017-03-24 01:37:06 +00:00
|
|
|
def post(self, uri, host=None, strict_slashes=False):
|
|
|
|
return self.route(uri, methods=["POST"], host=host,
|
|
|
|
strict_slashes=strict_slashes)
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2017-03-24 01:37:06 +00:00
|
|
|
def put(self, uri, host=None, strict_slashes=False):
|
|
|
|
return self.route(uri, methods=["PUT"], host=host,
|
|
|
|
strict_slashes=strict_slashes)
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2017-03-24 01:37:06 +00:00
|
|
|
def head(self, uri, host=None, strict_slashes=False):
|
|
|
|
return self.route(uri, methods=["HEAD"], host=host,
|
|
|
|
strict_slashes=strict_slashes)
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2017-03-24 01:37:06 +00:00
|
|
|
def options(self, uri, host=None, strict_slashes=False):
|
|
|
|
return self.route(uri, methods=["OPTIONS"], host=host,
|
|
|
|
strict_slashes=strict_slashes)
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2017-03-24 01:37:06 +00:00
|
|
|
def patch(self, uri, host=None, strict_slashes=False):
|
|
|
|
return self.route(uri, methods=["PATCH"], host=host,
|
|
|
|
strict_slashes=strict_slashes)
|
2017-01-30 07:20:38 +00:00
|
|
|
|
2017-03-24 01:37:06 +00:00
|
|
|
def delete(self, uri, host=None, strict_slashes=False):
|
|
|
|
return self.route(uri, methods=["DELETE"], host=host,
|
|
|
|
strict_slashes=strict_slashes)
|