<commit_before>from werkzeug.wrappers import Response as ResponseBase
from flask import request, render_template, make_response, json, abort
from logging import getLogger
from marshmallow import MarshalResult

from functools import wraps

import importlib


API_MIMETYPES = set((
                    'application/json',
                    'application/vnd.api+json'
                    ))


def load_modules_and_app(app):
    logger = getLogger("beavy.loadModules")
    loaders = []
    for modl in app.config.get("MODULES", []):
        # load module
        logger.debug("Importing Module {}".format(modl))
        subm = importlib.import_module("beavy_modules.{}".format(modl))
        # defer call init on module if found
        if hasattr(subm, "init_app"):
            loaders.append((modl, subm.init_app))

    # Load the app
    app_modl = app.config.get('APP')
    logger.debug("Importing APP {}".format(app_modl))
    app_subm = importlib.import_module("beavy_apps.{}".format(app_modl))

    for (modl, init_app) in loaders:
        logger.debug("Init Module {}".format(modl))
        init_app(app)

    # call init on app if found
    if hasattr(app_subm, "init_app"):
        app_subm.init_app(app)


def api_only(fn):
    @wraps(fn)
    def wrapped(*args, **kwargs):
        accepted = set(request.accept_mimetypes.values())
        if not (accepted & API_MIMETYPES) and not request.args.get("json"):
            return abort(415, "Unsupported Media Type")
        return fn(*args, **kwargs)
    return wrapped


def fallbackRender(template, key=None):

    def wrapper(fn):
        @wraps(fn)
        def decorated_view(*args, **kwargs):
            resp = fn(*args, **kwargs)
            if isinstance(resp, ResponseBase):
                return resp

            data, code, headers = unpack(resp)

            accepted = set(request.accept_mimetypes.values())
            if len(accepted & API_MIMETYPES) or request.args.get("json"):
                # we've found one, return json
                if isinstance(data, MarshalResult):
                    data = data.data
                resp = make_response(json.dumps(data), code)
                ct = 'application/json'<commit_msg>Allow 'apionly' to return the same data types as fallback<commit_after>from werkzeug.wrappers import Response as ResponseBase
from flask import request, render_template, make_response, json, abort
from logging import getLogger
from marshmallow import MarshalResult

from functools import wraps

import importlib


API_MIMETYPES = set((
                    'application/json',
                    'application/vnd.api+json'
                    ))


def load_modules_and_app(app):
    logger = getLogger("beavy.loadModules")
    loaders = []
    for modl in app.config.get("MODULES", []):
        # load module
        logger.debug("Importing Module {}".format(modl))
        subm = importlib.import_module("beavy_modules.{}".format(modl))
        # defer call init on module if found
        if hasattr(subm, "init_app"):
            loaders.append((modl, subm.init_app))

    # Load the app
    app_modl = app.config.get('APP')
    logger.debug("Importing APP {}".format(app_modl))
    app_subm = importlib.import_module("beavy_apps.{}".format(app_modl))

    for (modl, init_app) in loaders:
        logger.debug("Init Module {}".format(modl))
        init_app(app)

    # call init on app if found
    if hasattr(app_subm, "init_app"):
        app_subm.init_app(app)


def api_only(fn):
    @wraps(fn)
    def wrapped(*args, **kwargs):
        accepted = set(request.accept_mimetypes.values())
        if not (accepted & API_MIMETYPES) and not request.args.get("json"):
            return abort(415, "Unsupported Media Type")

        resp = fn(*args, **kwargs)
        if not isinstance(resp, ResponseBase):
            data, code, headers = unpack(resp)
            # we've found one, return json
            if isinstance(data, MarshalResult):
                data = data.data
            resp = make_response(json.dumps(data), code)

            if headers:
                resp.headers.update(headers)
            resp.headers["Content-Type"] = 'application/json'
        return resp
    return wrapped


def fallbackRender(template, key=None):

    def wrapper(fn):
        @wraps(fn)
        def decorated_view(*args, **kwargs):
            resp = fn(*args, **kwargs)
            if isinstance(resp, ResponseBase):
                return resp

            data, code, headers = unpack(resp)

            accepted = set(request.accept_mimetypes.values())
            if len(accepted & API_MIMETYPES) or request.args.get("json"):
                # we've found one, return json
                if isinstance(data, MarshalResult):
                    data = data.data
                resp = make_response(json.dumps(data), code)
                ct = 'application/json'
{'fraction_non_alphanumeric': 0.11099712761592122, 'fraction_numerical': 0.0012310217480508822, 'mean_word_length': 2.3830673143650243, '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': '7424879', 'n_tokens_mistral': 1458, 'n_tokens_neox': 1431, 'n_words': 350}