<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/5/19 上午9:33
# @Author  : Matrix
# @Site    :
# @File    : controller.py
# @Software: PyCharm
from functools import wraps
from inspect import unwrap
from .version import allversion
from app.apizen.schema import convert
from inspect import signature, Parameter
from .exceptions import ApiSysExceptions

__author__ = 'blackmatrix'

'''
接口处理方法的异常判断与执行
'''


def raw_response(func):
    """
    装饰器，表示接口处理函数返回的值不需要统一的返回格式
    :param func: 装饰的函数
    :return:
    """
    @wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)

    wrapper.raw_response = True
    return wrapper


class Method:

    # 获取api处理函数及相关异常判断
    @staticmethod
    def get(version, method_name, request_method):
        """
        获取api处理函数及相关异常判断
        :param version:  接口版本
        :param method_name:  方法名
        :param request_method:  http请求方式
        :return:
        """
        raw_resp = False
        allow_anonymous = False

        def check_decorator(func):
            nonlocal raw_resp
            nonlocal allow_anonymous
            if hasattr(func, 'raw_response') and func.raw_response is True:
                raw_resp = True
            if hasattr(func, 'allow_anonymous') and func.allow_anonymous is True:
                allow_anonymous = True

        # 检查版本号
        if version not in allversion:
            raise ApiSysExceptions.unsupported_version
        # 检查版本是否停用
        elif not allversion[version].get('enable', True):
            raise ApiSysExceptions.version_stop

        methods = getattr(allversion[version]['methods'], 'api_methods')

        # 检查方法名是否存在
        if method_name not in methods:
            raise ApiSysExceptions.invalid_method
        # 检查方法是否停用
        elif not methods[method_name].get('enable', True):
            raise ApiSysExceptions.api_stop
        # 检查方法是否允许以某种请求方式调用
        elif request_method.lower() not in methods[method_name].get('methods', ['get', 'post']):
            raise ApiSysExceptions.not_allowed_request
        # 检查函数是否可调用
        elif not callable(methods[method_name].get('func')):
            raise ApiSysExceptions.error_api_config

        _func = methods[method_name].get('func')

        # 解包，检查是否有不统一格式化输出的装饰器，或运行匿名访问情况
        unwrap(_func, stop=check_decorator)

        return _func, raw_resp, allow_anonymous

    # 运行接口处理方法，及异常处理
    @staticmethod
    def run(api_method, request_params):
<commit_msg>修改保持接口原始结果的装饰器<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/5/19 上午9:33
# @Author  : Matrix
# @Site    :
# @File    : controller.py
# @Software: PyCharm
from functools import wraps
from inspect import unwrap
from .version import allversion
from app.apizen.schema import convert
from inspect import signature, Parameter
from .exceptions import ApiSysExceptions

__author__ = 'blackmatrix'

'''
接口处理方法的异常判断与执行
'''


def raw_response(func):
    """
    装饰器，表示接口处理函数返回的值不需要统一的返回格式
    :param func: 装饰的函数
    :return:
    """
    @wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)

    wrapper.raw_response = True
    return wrapper


class Method:

    # 获取api处理函数及相关异常判断
    @staticmethod
    def get(version, method_name, request_method):
        """
        获取api处理函数及相关异常判断
        :param version:  接口版本
        :param method_name:  方法名
        :param request_method:  http请求方式
        :return:
        """
        raw_resp = False
        allow_anonymous = False

        def check_decorator(func):
            nonlocal raw_resp
            nonlocal allow_anonymous
            if hasattr(func, 'raw_response') and func.raw_response is True:
                raw_resp = True
            if hasattr(func, 'allow_anonymous') and func.allow_anonymous is True:
                allow_anonymous = True

        # 检查版本号
        if version not in allversion:
            raise ApiSysExceptions.unsupported_version
        # 检查版本是否停用
        elif not allversion[version].get('enable', True):
            raise ApiSysExceptions.version_stop

        methods = getattr(allversion[version]['methods'], 'api_methods')

        # 检查方法名是否存在
        if method_name not in methods:
            raise ApiSysExceptions.invalid_method
        # 检查方法是否停用
        elif not methods[method_name].get('enable', True):
            raise ApiSysExceptions.api_stop
        # 检查方法是否允许以某种请求方式调用
        elif request_method.lower() not in methods[method_name].get('methods', ['get', 'post']):
            raise ApiSysExceptions.not_allowed_request
        # 检查函数是否可调用
        elif not callable(methods[method_name].get('func')):
            raise ApiSysExceptions.error_api_config

        _func = methods[method_name].get('func')

        # 解包，检查是否有不统一格式化输出的装饰器，或允许匿名访问情况
        unwrap(_func, stop=check_decorator)

        return _func, raw_resp, allow_anonymous

    # 运行接口处理方法，及异常处理
    @staticmethod
    def run(api_method, request_params):

{'fraction_non_alphanumeric': 0.08949808506349527, 'fraction_numerical': 0.004434589800443459, 'mean_word_length': 2.5417558886509637, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5407123', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1828, 'n_words': 340}