Skip to content

Commit

Permalink
Merge pull request #228 from seanpar203/issue_41
Browse files Browse the repository at this point in the history
Adding more docstrings
  • Loading branch information
seemethere committed Jan 19, 2017
2 parents 4682d7b + d5ad5e4 commit a79f073
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
23 changes: 20 additions & 3 deletions sanic/blueprints.py
Expand Up @@ -3,6 +3,7 @@

class BlueprintSetup:
"""
Creates a blueprint state like object.
"""

def __init__(self, blueprint, app, options):
Expand Down Expand Up @@ -32,13 +33,13 @@ def add_route(self, handler, uri, methods, host=None):

def add_exception(self, handler, *args, **kwargs):
"""
Registers exceptions to sanic
Registers exceptions to sanic.
"""
self.app.exception(*args, **kwargs)(handler)

def add_static(self, uri, file_or_directory, *args, **kwargs):
"""
Registers static files to sanic
Registers static files to sanic.
"""
if self.url_prefix:
uri = self.url_prefix + uri
Expand All @@ -47,7 +48,7 @@ def add_static(self, uri, file_or_directory, *args, **kwargs):

def add_middleware(self, middleware, *args, **kwargs):
"""
Registers middleware to sanic
Registers middleware to sanic.
"""
if args or kwargs:
self.app.middleware(*args, **kwargs)(middleware)
Expand Down Expand Up @@ -77,18 +78,23 @@ def record(self, func):

def make_setup_state(self, app, options):
"""
Returns a new BlueprintSetup object
"""
return BlueprintSetup(self, app, options)

def register(self, app, options):
"""
Registers the blueprint to the sanic app.
"""
state = self.make_setup_state(app, options)
for deferred in self.deferred_functions:
deferred(state)

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):
self.record(lambda s: s.add_route(handler, uri, methods, host))
Expand All @@ -97,12 +103,18 @@ def decorator(handler):

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))
return handler

def listener(self, event):
"""
Create a listener from a decorated function.
:param event: Event to listen to.
"""
def decorator(listener):
self.listeners[event].append(listener)
Expand All @@ -111,6 +123,7 @@ def decorator(listener):

def middleware(self, *args, **kwargs):
"""
Creates a blueprint middleware from a decorated function.
"""
def register_middleware(middleware):
self.record(
Expand All @@ -127,6 +140,7 @@ def register_middleware(middleware):

def exception(self, *args, **kwargs):
"""
Creates a blueprint exception from a decorated function.
"""
def decorator(handler):
self.record(lambda s: s.add_exception(handler, *args, **kwargs))
Expand All @@ -135,6 +149,9 @@ def decorator(handler):

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(
lambda s: s.add_static(uri, file_or_directory, *args, **kwargs))
24 changes: 24 additions & 0 deletions sanic/response.py
Expand Up @@ -143,21 +143,45 @@ def cookies(self):


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,
content_type="application/json")


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,
content_type="text/plain; charset=utf-8")


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,
content_type="text/html; charset=utf-8")


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]

async with open_async(location, mode='rb') as _file:
Expand Down

0 comments on commit a79f073

Please sign in to comment.