<commit_before>'''
Created on 28.07.2016

@author: Jonas
'''
import json

from flask import request
from flask.wrappers import Response, Request
from pkg_resources import Requirement, resource_string
import yaml

CONFIG = yaml.safe_load(resource_string(Requirement.parse("upb-son-editor-backend"), "son_editor/config.yaml"))


def prepare_response(data=None, code=200) -> Response:
    """
    Sets the necessary headers and status code on the response
    :param data: The data to be returned to the client
    :param code: the status code. 200 by default
    :return: The Response object with the headers set according to the input data
    """
    response = Response()
    headers = response.headers
    if allowed_origin():
        headers['Access-Control-Allow-Origin'] = request.headers['Origin']
    headers['Access-Control-Allow-Methods'] = "GET,POST,PUT,DELETE,OPTIONS"
    headers['Access-Control-Allow-Headers'] = "Content-Type, Authorization, X-Requested-With"
    headers['Access-Control-Allow-Credentials'] = "true"
    headers['Access-Control-Max-Age'] = 1000
    if data is not None:
        if isinstance(data, dict) or isinstance(data, list):
            response.set_data(json.dumps(data))
            headers['Content-Type'] = 'application/json'
        else:
            response.set_data(data)
            headers['Content-Type'] = 'text/plain; charset=utf-8'
    response.status_code = code
    return response


def prepare_error(data=None, code=500) -> tuple:
    """
    Prepares the error response and returns it as a tuple
    to accommodate for flask_restplus's way to deal with errors
    :param data: The error message
    :param code: the http error code, 500 by default
    :return: A tuple of the data, the status code and the headers
    """
    response = Response()
    headers = response.headers
    if allowed_origin():
        headers['Access-Control-Allow-Origin'] = request.headers['Origin']
    headers['Access-Control-Allow-Methods'] = "GET,POST,PUT,DELETE,OPTIONS"
    headers['Access-Control-Allow-Headers'] = "Content-Type, Authorization, X-Requested-With"
    headers['Access-Control-Allow-Credentials'] = "true"
    headers['Access-Control-Max-Age'] = 1000
    return data, code, headers


def allowed_origin():
    """Checks if the request originates from a known origin"""
    if 'Origin' in request.headers:
        #strip the http method because we are only interested in the host
        origin = request.headers['Origin'].replace("http://", "").replace("https://", "")
        origin_parts = origin.split(":")
        if len(origin_parts) > 1:
            origin = origin_parts[0]
        return origin in CONFIG['allowed-hosts']
    return False


def get_json(request: Request) -> dict:
    json_data = request.get_json()
    if json_data is None:
        json_data = request.form
    if json_data is None:
        json_data = json.loads(request.get_data().decode("utf8"))
    return json_data


def rreplace(s, old, new, occurrence):
    li = s.rsplit(old, occurrence)
    return new.join(li)<commit_msg>Added missing documentation<commit_after>'''
Created on 28.07.2016

@author: Jonas
'''
import json

from flask import request
from flask.wrappers import Response, Request
from pkg_resources import Requirement, resource_string
import yaml

CONFIG = yaml.safe_load(resource_string(Requirement.parse("upb-son-editor-backend"), "son_editor/config.yaml"))


def prepare_response(data=None, code=200) -> Response:
    """
    Sets the necessary headers and status code on the response
    :param data: The data to be returned to the client
    :param code: the status code. 200 by default
    :return: The Response object with the headers set according to the input data
    """
    response = Response()
    headers = response.headers
    if allowed_origin():
        headers['Access-Control-Allow-Origin'] = request.headers['Origin']
    headers['Access-Control-Allow-Methods'] = "GET,POST,PUT,DELETE,OPTIONS"
    headers['Access-Control-Allow-Headers'] = "Content-Type, Authorization, X-Requested-With"
    headers['Access-Control-Allow-Credentials'] = "true"
    headers['Access-Control-Max-Age'] = 1000
    if data is not None:
        if isinstance(data, dict) or isinstance(data, list):
            response.set_data(json.dumps(data))
            headers['Content-Type'] = 'application/json'
        else:
            response.set_data(data)
            headers['Content-Type'] = 'text/plain; charset=utf-8'
    response.status_code = code
    return response


def prepare_error(data=None, code=500) -> tuple:
    """
    Prepares the error response and returns it as a tuple
    to accommodate for flask_restplus's way to deal with errors
    :param data: The error message
    :param code: the http error code, 500 by default
    :return: A tuple of the data, the status code and the headers
    """
    response = Response()
    headers = response.headers
    if allowed_origin():
        headers['Access-Control-Allow-Origin'] = request.headers['Origin']
    headers['Access-Control-Allow-Methods'] = "GET,POST,PUT,DELETE,OPTIONS"
    headers['Access-Control-Allow-Headers'] = "Content-Type, Authorization, X-Requested-With"
    headers['Access-Control-Allow-Credentials'] = "true"
    headers['Access-Control-Max-Age'] = 1000
    return data, code, headers


def allowed_origin():
    """Checks if the request originates from a known origin"""
    if 'Origin' in request.headers:
        #strip the http method because we are only interested in the host
        origin = request.headers['Origin'].replace("http://", "").replace("https://", "")
        origin_parts = origin.split(":")
        if len(origin_parts) > 1:
            origin = origin_parts[0]
        return origin in CONFIG['allowed-hosts']
    return False


def get_json(request: Request) -> dict:
    """
    Helper function to get a json dict out of a request
    :param request: Request to get the json data from
    :return: json data as dict
    """
    json_data = request.get_json()
    if json_data is None:
        json_data = request.form
    if json_data is None:
        json_data = json.loads(request.get_data().decode("utf8"))
    return json_data


def rreplace(s, old, new, occurrence):
    """
    Replaces 'occurences' occurences of string 'old' in the given string 's' from right by 'new'
    :param s: String that contains the replacing string
    :param old: String that gets replaced
    :param new: New string that replaces the old string
    :param occurrence: How many occurences get replaced
    :return: String that has replaced strings
    """
    li = s.rsplit(old, occurrence)
    return new.join(li)
{'fraction_non_alphanumeric': 0.11281128112811281, 'fraction_numerical': 0.009600960096009602, 'mean_word_length': 3.389071757735352, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 2, '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': '4720204', 'n_tokens_mistral': 1944, 'n_tokens_neox': 1856, 'n_words': 591}