Merge branch 'feature_blueprints' of https://github.com/narzeja/sanic into narzeja-feature_blueprints
This commit is contained in:
@@ -1 +1,2 @@
|
||||
from .sanic import Sanic
|
||||
from .blueprints import Blueprint
|
||||
|
||||
68
sanic/blueprints.py
Normal file
68
sanic/blueprints.py
Normal file
@@ -0,0 +1,68 @@
|
||||
|
||||
|
||||
class BlueprintSetup():
|
||||
"""
|
||||
"""
|
||||
|
||||
def __init__(self, blueprint, app, options):
|
||||
self.app = app
|
||||
self.blueprint = blueprint
|
||||
self.options = options
|
||||
|
||||
url_prefix = self.options.get('url_prefix')
|
||||
if url_prefix is None:
|
||||
url_prefix = self.blueprint.url_prefix
|
||||
|
||||
#: The prefix that should be used for all URLs defined on the
|
||||
#: blueprint.
|
||||
self.url_prefix = url_prefix
|
||||
|
||||
def add_url_rule(self, uri, methods=None, handler=None, **options):
|
||||
"""A helper method to register a handler to the application url routes.
|
||||
|
||||
"""
|
||||
if self.url_prefix:
|
||||
uri = self.url_prefix + uri
|
||||
|
||||
self.app.router.add(uri, methods, handler)
|
||||
|
||||
|
||||
class Blueprint():
|
||||
def __init__(self, name, url_prefix=None):
|
||||
self.name = name
|
||||
self.url_prefix = url_prefix
|
||||
self.deferred_functions = []
|
||||
|
||||
def record(self, func):
|
||||
"""Registers a callback function that is invoked when the blueprint is
|
||||
registered on the application.
|
||||
|
||||
"""
|
||||
self.deferred_functions.append(func)
|
||||
|
||||
|
||||
def make_setup_state(self, app, options):
|
||||
"""
|
||||
"""
|
||||
return BlueprintSetup(self, app, options)
|
||||
|
||||
def register(self, app, options):
|
||||
"""
|
||||
"""
|
||||
state = self.make_setup_state(app, options)
|
||||
for deferred in self.deferred_functions:
|
||||
deferred(state)
|
||||
|
||||
def route(self, uri, methods=None):
|
||||
"""
|
||||
"""
|
||||
def decorator(handler):
|
||||
self.add_url_rule(uri=uri, methods=methods, handler=handler)
|
||||
return handler
|
||||
return decorator
|
||||
|
||||
def add_url_rule(self, uri, methods=None, handler=None):
|
||||
"""
|
||||
"""
|
||||
self.record(lambda s:
|
||||
s.add_url_rule(uri, methods, handler))
|
||||
@@ -21,6 +21,8 @@ class Sanic:
|
||||
self.config = Config()
|
||||
self.request_middleware = []
|
||||
self.response_middleware = []
|
||||
self.blueprints = {}
|
||||
self._blueprint_order = []
|
||||
|
||||
# -------------------------------------------------------------------- #
|
||||
# Registration
|
||||
@@ -85,6 +87,23 @@ class Sanic:
|
||||
|
||||
return middleware
|
||||
|
||||
def register_blueprint(self, blueprint, **options):
|
||||
"""
|
||||
Registers a blueprint on the application.
|
||||
:param blueprint: Blueprint object
|
||||
:param options: option dictionary with blueprint defaults
|
||||
:return: Nothing
|
||||
"""
|
||||
if blueprint.name in self.blueprints:
|
||||
assert self.blueprints[blueprint.name] is blueprint, \
|
||||
'A blueprint with the name "%s" is already registered. ' \
|
||||
'Blueprint names must be unique.' % \
|
||||
(blueprint.name,)
|
||||
else:
|
||||
self.blueprints[blueprint.name] = blueprint
|
||||
self._blueprint_order.append(blueprint)
|
||||
blueprint.register(self, options)
|
||||
|
||||
# -------------------------------------------------------------------- #
|
||||
# Request Handling
|
||||
# -------------------------------------------------------------------- #
|
||||
|
||||
Reference in New Issue
Block a user