
"""Static pages for various HTTP codes."""

from flask import render_template
from inspect import isfunction


def page_404(error):
    """404 page."""
    return render_template(
        'status_codes/404.html',
        code=404, desc='not found', error=error)


def page_500(error):
    """500 page."""
    return render_template(
        'status_codes/500.html',
        code=500, desc='server error', error=error)


def _isview(name, func):
    """Check if arguments represent a valid Flask view.

    Args:
        name (str): String name of function.
        func (function): A function.

    Returns:
        bool: Whether the given args are valid.
    """
    return isfunction(func) and name.startswith('page_')


def _get_viewfuncs():
    """Return all functions in this module that are relevant views.

    Returns:
        dict: The view functions, keyed by name.
    """
    return {name: func for name, func
            in globals().iteritems() if _isview(name, func)}


def inject_error_views(app):
    """Inject all relevant error (status code) views into an app.

    Args:
        app (object): The Flask instance.
        funcs (dict): A dict of functions by name.

    Returns:
        object: The modified Flask instance.
    """
    # See flask.pocoo.org/docs/0.10/api/#flask.Flask.errorhandler
    for name, func in _get_viewfuncs().iteritems():
        code = name.replace('page_', '')
        app.error_handler_spec[None][int(code)] = func
    return app

@@ -1,9 +1,8 @@
-%0A
 %22%22%22Stati
@@ -36,77 +36,693 @@
 des.
-%22%22%22%0A%0Afrom flask import render_template%0Afrom inspect import isfunction
+%0A%0AFormat must be %60page_XXX%60 where XXX is the code in question. This allows%0Aproper dynamic injection into the Flask app error handling mechanism.%0A%22%22%22%0A%0Afrom flask import render_template%0Afrom inspect import isfunction%0A%0A%0Adef page_400(error):%0A    %22%22%22400 page.%22%22%22%0A    return render_template(%0A        'status_codes/400.html',%0A        code=400, desc='bad request', error=error)%0A%0A%0Adef page_401(error):%0A    %22%22%22401 page.%22%22%22%0A    return render_template(%0A        'status_codes/401.html',%0A        code=401, desc='unauthorized access', error=error)%0A%0A%0Adef page_403(error):%0A    %22%22%22403 page.%22%22%22%0A    return render_template(%0A        'status_codes/403.html',%0A        code=403, desc='forbidden', error=error)
 %0A%0A%0Ad
@@ -999,16 +999,25 @@
 , desc='
+internal 
 server e

Add new error code views