Merge pull request #228 from seanpar203/issue_41

Adding more docstrings
This commit is contained in:
Eli Uriegas 2017-01-19 16:18:28 -06:00 committed by GitHub
commit a79f073077
2 changed files with 44 additions and 3 deletions

View File

@ -3,6 +3,7 @@ from collections import defaultdict
class BlueprintSetup: class BlueprintSetup:
""" """
Creates a blueprint state like object.
""" """
def __init__(self, blueprint, app, options): def __init__(self, blueprint, app, options):
@ -32,13 +33,13 @@ class BlueprintSetup:
def add_exception(self, handler, *args, **kwargs): def add_exception(self, handler, *args, **kwargs):
""" """
Registers exceptions to sanic Registers exceptions to sanic.
""" """
self.app.exception(*args, **kwargs)(handler) self.app.exception(*args, **kwargs)(handler)
def add_static(self, uri, file_or_directory, *args, **kwargs): def add_static(self, uri, file_or_directory, *args, **kwargs):
""" """
Registers static files to sanic Registers static files to sanic.
""" """
if self.url_prefix: if self.url_prefix:
uri = self.url_prefix + uri uri = self.url_prefix + uri
@ -47,7 +48,7 @@ class BlueprintSetup:
def add_middleware(self, middleware, *args, **kwargs): def add_middleware(self, middleware, *args, **kwargs):
""" """
Registers middleware to sanic Registers middleware to sanic.
""" """
if args or kwargs: if args or kwargs:
self.app.middleware(*args, **kwargs)(middleware) self.app.middleware(*args, **kwargs)(middleware)
@ -77,11 +78,13 @@ class Blueprint:
def make_setup_state(self, app, options): def make_setup_state(self, app, options):
""" """
Returns a new BlueprintSetup object
""" """
return BlueprintSetup(self, app, options) return BlueprintSetup(self, app, options)
def register(self, app, options): def register(self, app, options):
""" """
Registers the blueprint to the sanic app.
""" """
state = self.make_setup_state(app, options) state = self.make_setup_state(app, options)
for deferred in self.deferred_functions: for deferred in self.deferred_functions:
@ -89,6 +92,9 @@ class Blueprint:
def route(self, uri, methods=None, host=None): def route(self, uri, methods=None, host=None):
""" """
Creates a blueprint route from a decorated function.
:param uri: Endpoint at which the route will be accessible.
:param methods: List of acceptable HTTP methods.
""" """
def decorator(handler): def decorator(handler):
self.record(lambda s: s.add_route(handler, uri, methods, host)) self.record(lambda s: s.add_route(handler, uri, methods, host))
@ -97,12 +103,18 @@ class Blueprint:
def add_route(self, handler, uri, methods=None, host=None): def add_route(self, handler, uri, methods=None, host=None):
""" """
Creates a blueprint route from a function.
:param handler: Function to handle uri request.
:param uri: Endpoint at which the route will be accessible.
:param methods: List of acceptable HTTP methods.
""" """
self.record(lambda s: s.add_route(handler, uri, methods, host)) self.record(lambda s: s.add_route(handler, uri, methods, host))
return handler return handler
def listener(self, event): def listener(self, event):
""" """
Create a listener from a decorated function.
:param event: Event to listen to.
""" """
def decorator(listener): def decorator(listener):
self.listeners[event].append(listener) self.listeners[event].append(listener)
@ -111,6 +123,7 @@ class Blueprint:
def middleware(self, *args, **kwargs): def middleware(self, *args, **kwargs):
""" """
Creates a blueprint middleware from a decorated function.
""" """
def register_middleware(middleware): def register_middleware(middleware):
self.record( self.record(
@ -127,6 +140,7 @@ class Blueprint:
def exception(self, *args, **kwargs): def exception(self, *args, **kwargs):
""" """
Creates a blueprint exception from a decorated function.
""" """
def decorator(handler): def decorator(handler):
self.record(lambda s: s.add_exception(handler, *args, **kwargs)) self.record(lambda s: s.add_exception(handler, *args, **kwargs))
@ -135,6 +149,9 @@ class Blueprint:
def static(self, uri, file_or_directory, *args, **kwargs): def static(self, uri, file_or_directory, *args, **kwargs):
""" """
Creates a blueprint static route from a decorated function.
:param uri: Endpoint at which the route will be accessible.
:param file_or_directory: Static asset.
""" """
self.record( self.record(
lambda s: s.add_static(uri, file_or_directory, *args, **kwargs)) lambda s: s.add_static(uri, file_or_directory, *args, **kwargs))

View File

@ -143,21 +143,45 @@ class HTTPResponse:
def json(body, status=200, headers=None): def json(body, status=200, headers=None):
"""
Returns response object with body in json format.
:param body: Response data to be serialized.
:param status: Response code.
:param headers: Custom Headers.
"""
return HTTPResponse(json_dumps(body), headers=headers, status=status, return HTTPResponse(json_dumps(body), headers=headers, status=status,
content_type="application/json") content_type="application/json")
def text(body, status=200, headers=None): def text(body, status=200, headers=None):
"""
Returns response object with body in text format.
:param body: Response data to be encoded.
:param status: Response code.
:param headers: Custom Headers.
"""
return HTTPResponse(body, status=status, headers=headers, return HTTPResponse(body, status=status, headers=headers,
content_type="text/plain; charset=utf-8") content_type="text/plain; charset=utf-8")
def html(body, status=200, headers=None): def html(body, status=200, headers=None):
"""
Returns response object with body in html format.
:param body: Response data to be encoded.
:param status: Response code.
:param headers: Custom Headers.
"""
return HTTPResponse(body, status=status, headers=headers, return HTTPResponse(body, status=status, headers=headers,
content_type="text/html; charset=utf-8") content_type="text/html; charset=utf-8")
async def file(location, mime_type=None, headers=None): async def file(location, mime_type=None, headers=None):
"""
Returns response object with file data.
:param location: Location of file on system.
:param mime_type: Specific mime_type.
:param headers: Custom Headers.
"""
filename = path.split(location)[-1] filename = path.split(location)[-1]
async with open_async(location, mode='rb') as _file: async with open_async(location, mode='rb') as _file: