<commit_before>import flask
import accounts
import settings

app = flask.Flask(__name__)

if not app.debug:
    import logging
    file_handler = logging.FileHandler('production.log')
    file_handler.setLevel(logging.WARNING)
    app.logger.addHandler(file_handler)


def account_manager():
    a = getattr(flask.g, '_accounts', None)
    if a is None:
        a = flask.g._accounts = accounts.Manager(settings.DATABASE_PATH)

    return a

def authenticate(f):
    def f2(*args, **kwargs):
        api_key = flask.request.headers.get('Authentication', None)

        if not account_manager().valid_api_key(api_key):
            return flask.jsonify(status="invalid-authentication"), 401
        else:
            return f(*args, **kwargs)

    f2.__name__ = f.__name__
    return f2


@app.route("/accounts/token/new", methods=['POST'])
def token_new():
    tm = account_manager()

    return flask.jsonify(token=tm.generate())


@app.route("/accounts/token/prices", methods=['GET'])
def token_prices():
    tm = account_manager()

    return flask.jsonify(prices=[
        {
            "amount": price.amount,
            "cost": price.cost
        }
        for price in tm.prices()])


@app.route("/accounts/token/balance/<token>", methods=['GET'])
def token_balance(token):
    tm = account_manager()

    return flask.jsonify(balance=tm.balance(token))


@app.route("/accounts/token/redeem/<token>", methods=['POST'])
def token_redeem(token):
    tm = account_manager()

    promocode = None if flask.request.json is None else flask.request.json.get('promocode', None)

    if promocode and tm.redeem(token, promocode):
        return flask.jsonify(status="ok"), 201
    else:
        return flask.jsonify(status="error"), 403


@app.route("/accounts/token/deposit/<token>", methods=['POST'])
@authenticate
def token_deposit(token):
    try:
        byte_amount = int(flask.request.json.get('bytes', None))
    except:
        return flask.jsonify(status="bad-request"), 400
<commit_msg>Fix NameError for undefined `tm` variable in token_deposit()<commit_after>import flask
import accounts
import settings

app = flask.Flask(__name__)

if not app.debug:
    import logging
    file_handler = logging.FileHandler('production.log')
    file_handler.setLevel(logging.WARNING)
    app.logger.addHandler(file_handler)


def account_manager():
    a = getattr(flask.g, '_accounts', None)
    if a is None:
        a = flask.g._accounts = accounts.Manager(settings.DATABASE_PATH)

    return a

def authenticate(f):
    def f2(*args, **kwargs):
        api_key = flask.request.headers.get('Authentication', None)

        if not account_manager().valid_api_key(api_key):
            return flask.jsonify(status="invalid-authentication"), 401
        else:
            return f(*args, **kwargs)

    f2.__name__ = f.__name__
    return f2


@app.route("/accounts/token/new", methods=['POST'])
def token_new():
    tm = account_manager()

    return flask.jsonify(token=tm.generate())


@app.route("/accounts/token/prices", methods=['GET'])
def token_prices():
    tm = account_manager()

    return flask.jsonify(prices=[
        {
            "amount": price.amount,
            "cost": price.cost
        }
        for price in tm.prices()])


@app.route("/accounts/token/balance/<token>", methods=['GET'])
def token_balance(token):
    tm = account_manager()

    return flask.jsonify(balance=tm.balance(token))


@app.route("/accounts/token/redeem/<token>", methods=['POST'])
def token_redeem(token):
    tm = account_manager()

    promocode = None if flask.request.json is None else flask.request.json.get('promocode', None)

    if promocode and tm.redeem(token, promocode):
        return flask.jsonify(status="ok"), 201
    else:
        return flask.jsonify(status="error"), 403


@app.route("/accounts/token/deposit/<token>", methods=['POST'])
@authenticate
def token_deposit(token):
    tm = account_manager()

    try:
        byte_amount = int(flask.request.json.get('bytes', None))
    except:
        return flask.jsonify(status="bad-request"), 400

{'fraction_non_alphanumeric': 0.14194341943419433, 'fraction_numerical': 0.007380073800738007, 'mean_word_length': 3.40997830802603, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 9, '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': '578615', 'n_tokens_mistral': 1359, 'n_tokens_neox': 1268, 'n_words': 261}