# Path: kael/web_admin/forms.py
# def get_mapping(blueprint, endpoint):
#     global _forms
#     if not _forms:
#         forms_yaml_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'forms.yml')
#         with codecs.open(forms_yaml_path, 'rb', 'utf-8') as fp:
#             t = Template(fp.read())
#             yml = t.render({'settings': current_app.config})
#             _forms = yaml.load(yml)
#     return _forms.get(blueprint, {}).get(endpoint, {})
# 
# Path: kael/validate.py
# def timestamp(instance):
# def is_md5(instance):
# 
# Path: kael/web_admin/exceptions.py
# class ArgumentError(BaseError):
#     """ Argument error. """
#     status = 10201
#     description = 'Argument Error'
# 
# class BaseError(Exception):
#     status = None
#     description = None
# 
#     def __init__(self, description=None):
#         if description is not None:
#             self.description = description
# 
#     def __str__(self):
#         return '%d: %s' % (self.status, self.description)
# 
#     def __repr__(self):
#         return '<%s \'%s\'>' % (self.__class__.__name__, self)
# 
# class UnknownError(BaseError):
#     """ Uncatch error. """
#     status = 10999
#     description = 'Unknown Error'


import sys
import simplejson as json
        import traceback
from .forms import get_mapping
from kael.validate import formatchecker
from jsonschema import validate
from .exceptions import ArgumentError, BaseError, UnknownError
from flask import request
from common import json_converter, stream_gen
        bl.__class__.route = _route
    else:
        sys.modules['flask'].Flask.route = _route
        sys.modules['flask'].Blueprint.route = _route


def patch_validate_handler(name, bl=None):
    def params_validate_handler():
        if not request.endpoint \
                or not request.endpoint.startswith('{0}.'.format(name)) \
                or request.method == 'HEAD':
            return
        _, blueprint, endpoint = request.endpoint.split('.')
        mapping = get_mapping(blueprint, endpoint)
        if mapping:
            params = request.get_json()
            if validate(params, mapping, format_checker=formatchecker):
                raise ArgumentError('Json schema validate failed')

    if bl:
        bl.before_app_first_request(params_validate_handler)
    else:
        sys.modules['flask'].app.before_app_first_request(params_validate_handler)

    return params_validate_handler


def patch_erro_handler(bl=None):
    def common_error_handler(exc):
        traceback.print_exc()

# Path: kael/web_admin/forms.py
# def get_mapping(blueprint, endpoint):
#     global _forms
#     if not _forms:
#         forms_yaml_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'forms.yml')
#         with codecs.open(forms_yaml_path, 'rb', 'utf-8') as fp:
#             t = Template(fp.read())
#             yml = t.render({'settings': current_app.config})
#             _forms = yaml.load(yml)
#     return _forms.get(blueprint, {}).get(endpoint, {})
# 
# Path: kael/validate.py
# def timestamp(instance):
# def is_md5(instance):
# 
# Path: kael/web_admin/exceptions.py
# class ArgumentError(BaseError):
#     """ Argument error. """
#     status = 10201
#     description = 'Argument Error'
# 
# class BaseError(Exception):
#     status = None
#     description = None
# 
#     def __init__(self, description=None):
#         if description is not None:
#             self.description = description
# 
#     def __str__(self):
#         return '%d: %s' % (self.status, self.description)
# 
#     def __repr__(self):
#         return '<%s \'%s\'>' % (self.__class__.__name__, self)
# 
# class UnknownError(BaseError):
#     """ Uncatch error. """
#     status = 10999
#     description = 'Unknown Error'


# Path: kael/web_admin/monkey.py
import sys
import simplejson as json
        import traceback
from .forms import get_mapping
from kael.validate import formatchecker
from jsonschema import validate
from .exceptions import ArgumentError, BaseError, UnknownError
from flask import request
from common import json_converter, stream_gen

        bl.__class__.route = _route
    else:
        sys.modules['flask'].Flask.route = _route
        sys.modules['flask'].Blueprint.route = _route


def patch_validate_handler(name, bl=None):
    def params_validate_handler():
        if not request.endpoint \
                or not request.endpoint.startswith('{0}.'.format(name)) \
                or request.method == 'HEAD':
            return
        _, blueprint, endpoint = request.endpoint.split('.')
        mapping = get_mapping(blueprint, endpoint)
        if mapping:
            params = request.get_json()
            if validate(params, mapping, format_checker=formatchecker):
                raise ArgumentError('Json schema validate failed')

    if bl:
        bl.before_app_first_request(params_validate_handler)
    else:
        sys.modules['flask'].app.before_app_first_request(params_validate_handler)

    return params_validate_handler


def patch_erro_handler(bl=None):
    def common_error_handler(exc):
        traceback.print_exc()

        if not isinstance(exc, BaseError):