<commit_before>import collections
import flask
from passlib import hash
from flask.ext.restful import Api
from flask_jwt import JWTError


class MyApi(Api):
    """A simple class to keep the default flask_jwt.JWTError behaviour."""

    def handle_error(self, e):
        if isinstance(e, JWTError):
            return flask.jsonify(
                collections.OrderedDict([
                    ('status_code', e.status_code),
                    ('error', e.error),
                    ('description', e.description),
                ])
            ), e.status_code, e.headers
        return super(MyApi, self).handle_error(e)


def get_hash_algorithm(hash_algorithm):
    """Returns a object with methods to encrypt a string and verify a hash.

    :hash_algorithm: a string object
    :returns: hash function

    """

    available_methods = {
        'SHA256': hash.sha256_crypt,
        'SHA512': hash.sha512_crypt,
    }

    if not isinstance(hash_algorithm, str):
        raise ValueError('The parameter "hash_algorithm" should be a string.')

    if hash_algorithm.upper() not in available_methods:
        raise ValueError('Invalid hash method.')

    return available_methods[hash_algorithm]


def encrypt_password(plaintext):
    """Generate a hash based on current_app HASH_ALGORITHM.

    :plaintext: a string object.
    :returns: a string object.

    """

    method = get_hash_algorithm(flask.current_app.config['HASH_ALGORITHM'])

    return method.encrypt(
        plaintext, salt=flask.current_app.config['HASH_SALT'])


def verify_password(password, hash):
    """Verify if a hash was generated by the password specified.

    :password: a string object (plaintext).
    :hash: a string object.
    :returns: True or False.

    """

    method = get_hash_algorithm(flask.current_app.config['HASH_ALGORITHM'])

    return method.verify(password, hash)<commit_msg>added a function to standardie the api's response<commit_after>import collections
import flask
from passlib import hash
from flask.ext.restful import Api
from flask_jwt import JWTError


class MyApi(Api):
    """A simple class to keep the default flask_jwt.JWTError behaviour."""

    def handle_error(self, e):
        if isinstance(e, JWTError):
            return flask.jsonify(
                collections.OrderedDict([
                    ('status_code', e.status_code),
                    ('error', e.error),
                    ('description', e.description),
                ])
            ), e.status_code, e.headers
        return super(MyApi, self).handle_error(e)


def get_hash_algorithm(hash_algorithm):
    """Returns a object with methods to encrypt a string and verify a hash.

    :hash_algorithm: a string object
    :returns: hash function

    """

    available_methods = {
        'SHA256': hash.sha256_crypt,
        'SHA512': hash.sha512_crypt,
    }

    if not isinstance(hash_algorithm, str):
        raise ValueError('The parameter "hash_algorithm" should be a string.')

    if hash_algorithm.upper() not in available_methods:
        raise ValueError('Invalid hash method.')

    return available_methods[hash_algorithm]


def encrypt_password(plaintext):
    """Generate a hash based on current_app HASH_ALGORITHM.

    :plaintext: a string object.
    :returns: a string object.

    """

    method = get_hash_algorithm(flask.current_app.config['HASH_ALGORITHM'])

    return method.encrypt(
        plaintext, salt=flask.current_app.config['HASH_SALT'])


def verify_password(password, hash):
    """Verify if a hash was generated by the password specified.

    :password: a string object (plaintext).
    :hash: a string object.
    :returns: True or False.

    """

    method = get_hash_algorithm(flask.current_app.config['HASH_ALGORITHM'])

    return method.verify(password, hash)


def standardize_api_response(result):
    """Creates a standardized response. This function eval the result key
    to define the status code and data.

    :result: a dict object with the operation result and on of the keys:
        'success', 'error', 'created', 'updated' or 'no-data'.
    :returns: TODO

    """
    pass
{'fraction_non_alphanumeric': 0.09618173030449492, 'fraction_numerical': 0.005799903334944417, 'mean_word_length': 2.9120982986767485, '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': '2678798', 'n_tokens_mistral': 1236, 'n_tokens_neox': 1158, 'n_words': 349}