<commit_before>from __future__ import (
        absolute_import, division, print_function, unicode_literals)
from builtins import (ascii, bytes, chr, dict, filter, hex, input,  # noqa
                      int, map, next, oct, open, pow, range, round,
                      str, super, zip)

from collections import namedtuple
from enum import IntEnum

from flask import (
        Flask, abort, redirect, render_template, request, session, url_for
)
from flask.ext.sqlalchemy import SQLAlchemy
from flask_wtf import Form
import jinja2
import requests
from wtforms import StringField
from wtforms.validators import DataRequired, Email


IMG_PATH_EMPTY = '/static/images/goban/e.gif'
IMG_PATH_BLACK = '/static/images/goban/b.gif'
IMG_PATH_WHITE = '/static/images/goban/w.gif'

app = Flask(__name__)
app.config.from_object('config')
app.jinja_env.undefined = jinja2.StrictUndefined
db = SQLAlchemy(app)


# Views
#
# Since view functions tend to have side-effects and to depend on global state,
# try to keep complexity (if, for...) out of them and move it into pure
# function helpers instead.

@app.route('/')
def front_page():
    if 'email' in session:
        return redirect(url_for('status'))
    return render_template_with_email("frontpage.html")

@app.route('/game')
def game():
    try:
        game_no = int(request.args['game_no'])
    except (KeyError, ValueError):
        return redirect('/')
    moves = Move.query.filter(Move.game_no == game_no).all()
    stone = get_stone_if_args_good(args=request.args, moves=moves)
    if stone is not None:
        db.session.add(stone)
        db.session.commit()
    moves = Move.query.filter(Move.game_no == game_no).all()
    goban = get_img_array_from_moves(moves)
    return render_template_with_email(
            "game.html", game_no=game_no, move_no=len(moves), goban=goban)

@app.route('/challenge', methods=('GET', 'POST'))
def challenge():
    form = ChallengeForm()
    if form.validate_on_submit():
        game = Game()
        game.black = form.opponent_email.data
        game.white = session['email']
        db.session.add(game)
        db.session.commit()
        return redirect(url_for('status'))
    return render_template_with_email("challenge.html", form=form)

@app.route('/status')
def status():
    if 'email' not in session:
        return redirect('/')
    logged_in_email = session['email']

    def involved_in_game(game):
        return (game.black == logged_in_email or game.white == logged_in_email)
    games = filter(involved_in_game, Game.query.all())
    return render_template_with_email("status.html", games=games)

@app.route('/persona/login', methods=['POST'])
def persona_login():
    if 'assertion' not in request.form:
        abort(400)
    data = {
            'assertion': request.form['assertion'],
            'audience': 'http://localhost:5000',
    }
    response = requests.post(
            'https://verifier.login.persona.org/verify',
            data=data, verify=True
    )
    session_update = process_persona_response(response)
    if session_update.do:
        # we separate out our internal "who's logged in" email from the one
        # used by Persona so that when the browser automation tests need to
        # create a fake login session, Persona doesn't get confused by a user
        # appearing who it doesn't remember processing.
        session.update({'email': session_update.email})
        session.update({'persona_email': session_update.email})
        # we're only accessed through AJAX, the response doesn't matter
        return ''
    else:
        abort(500)

@app.route('/logout', methods=['POST'])
def logout():
    try:
        del session['email']
        del session['persona_email']
    except KeyError:
        pass
    return ''


# helper functions
<commit_msg>tiny fix: separate out statements in a try/except<commit_after>from __future__ import (
        absolute_import, division, print_function, unicode_literals)
from builtins import (ascii, bytes, chr, dict, filter, hex, input,  # noqa
                      int, map, next, oct, open, pow, range, round,
                      str, super, zip)

from collections import namedtuple
from enum import IntEnum

from flask import (
        Flask, abort, redirect, render_template, request, session, url_for
)
from flask.ext.sqlalchemy import SQLAlchemy
from flask_wtf import Form
import jinja2
import requests
from wtforms import StringField
from wtforms.validators import DataRequired, Email


IMG_PATH_EMPTY = '/static/images/goban/e.gif'
IMG_PATH_BLACK = '/static/images/goban/b.gif'
IMG_PATH_WHITE = '/static/images/goban/w.gif'

app = Flask(__name__)
app.config.from_object('config')
app.jinja_env.undefined = jinja2.StrictUndefined
db = SQLAlchemy(app)


# Views
#
# Since view functions tend to have side-effects and to depend on global state,
# try to keep complexity (if, for...) out of them and move it into pure
# function helpers instead.

@app.route('/')
def front_page():
    if 'email' in session:
        return redirect(url_for('status'))
    return render_template_with_email("frontpage.html")

@app.route('/game')
def game():
    try:
        game_no = int(request.args['game_no'])
    except (KeyError, ValueError):
        return redirect('/')
    moves = Move.query.filter(Move.game_no == game_no).all()
    stone = get_stone_if_args_good(args=request.args, moves=moves)
    if stone is not None:
        db.session.add(stone)
        db.session.commit()
    moves = Move.query.filter(Move.game_no == game_no).all()
    goban = get_img_array_from_moves(moves)
    return render_template_with_email(
            "game.html", game_no=game_no, move_no=len(moves), goban=goban)

@app.route('/challenge', methods=('GET', 'POST'))
def challenge():
    form = ChallengeForm()
    if form.validate_on_submit():
        game = Game()
        game.black = form.opponent_email.data
        game.white = session['email']
        db.session.add(game)
        db.session.commit()
        return redirect(url_for('status'))
    return render_template_with_email("challenge.html", form=form)

@app.route('/status')
def status():
    if 'email' not in session:
        return redirect('/')
    logged_in_email = session['email']

    def involved_in_game(game):
        return (game.black == logged_in_email or game.white == logged_in_email)
    games = filter(involved_in_game, Game.query.all())
    return render_template_with_email("status.html", games=games)

@app.route('/persona/login', methods=['POST'])
def persona_login():
    if 'assertion' not in request.form:
        abort(400)
    data = {
            'assertion': request.form['assertion'],
            'audience': 'http://localhost:5000',
    }
    response = requests.post(
            'https://verifier.login.persona.org/verify',
            data=data, verify=True
    )
    session_update = process_persona_response(response)
    if session_update.do:
        # we separate out our internal "who's logged in" email from the one
        # used by Persona so that when the browser automation tests need to
        # create a fake login session, Persona doesn't get confused by a user
        # appearing who it doesn't remember processing.
        session.update({'email': session_update.email})
        session.update({'persona_email': session_update.email})
        # we're only accessed through AJAX, the response doesn't matter
        return ''
    else:
        abort(500)

@app.route('/logout', methods=['POST'])
def logout():
    try:
        del session['email']
    except KeyError:
        pass
    try:
        del session['persona_email']
    except KeyError:
        pass
    return ''


# helper functions

{'fraction_non_alphanumeric': 0.11581001951854261, 'fraction_numerical': 0.0031229668184775536, 'mean_word_length': 3.206896551724138, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1138577', 'n_tokens_mistral': 2388, 'n_tokens_neox': 2321, 'n_words': 617}