<commit_before># imports from built-in modules
import os.path
from uuid import uuid4 as random_uuid

# imports from third-party modules
from flask import abort
from flask import flash
from flask import g
from flask import redirect
from flask import render_template
from flask import request
from flask import session
from flask import url_for
from flaskext.wtf import Form
from flaskext.wtf import FileField
from flaskext.wtf import SelectField
from flaskext.wtf.file import file_required
import Image

# imports from this application
from ophot import _get_categories
from ophot import _get_last_display_position
from ophot import app
from ophot import site_config
from ophot.forms import ChangeSplashPhotoForm
from ophot.forms import SettingsForm

# the first and last name of the photographer
realname = app.config['NAME']

# TODO use mime types or magic numbers to identify files
def _allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']

def _add_new_category(categoryname):
    """Creates a new category in the database with the specified *categoryname*
    and returns its ID number.

    """
    g.db.execute('insert into category (categoryname) values (?)',
                 [categoryname])
    g.db.commit()
    # get the ID of the category that we just inserted
    return g.db.execute('select categoryid from category where'
                        ' categoryname == "{0}"'.format(categoryname)).fetchone()[0]

def _generate_filename(directory, filename):
    """Generate the path at which to the save a file uploaded by the user.

    For now, this just generates a random UUID (version 4), then appends the
    same extension found from the input filename.
    
    """
    extension = filename.rsplit('.', 1)[1]
    uuid = random_uuid().hex # the string containing just the hex characters
    filename = os.path.join(directory, '{0}.{1}'.format(uuid, extension))
    while os.path.exists(filename):
        uuid = random_uuid().hex
        filename = os.path.join(directory, '{0}.{1}'.format(uuid, extension))
    return filename

def _get_categories_plus_new():
    """Helper method for the PhotoUploadForm which returns a list of pairs,
    each containing the category ID on the left and the category name on the
    right. The final element of this list is the pair (-1, 'new category...'),
    which is a sentinel value notifying the server that the user wishes to
    create a new category and apply these photos to it.

    Pre-condition: none of the existing categories have an ID of -1.
    """
    return _get_categories().items() + [(-1, 'new category...')]

# def _get_categories_plus_new_escaped():
#     """Helper method for the PhotoUploadForm which returns a list of pairs,
#     each containing the category ID on the left and the category name on the
#     right. The final element of this list is the pair (-1,
#     'new&nbsp;category...'), which is a sentinel value notifying the server
#     that the user wishes to create a new category and apply these photos to it.

#     Pre-condition: none of the existing categories have an ID of -1.
#     """
#     return _get_categories().items() + [(-1, 'new&nbsp;category...')]
    
def _to_html_paragraphs(string):
    """Splits the input string on newlines, then wraps HTML <p> tags around
    each non-empty line.

    """
    return ''.join('<p>{0}</p>'.format(line) for line in string.splitlines()
                   if line)

@app.route('/')
def show_splash():
    """Shows the splash page as the root."""
    categories = _get_categories().iteritems()
    bio = _to_html_paragraphs(site_config['BIO'])
    contact = _to_html_paragraphs(site_config['CONTACT'])
    return render_template('splash.html', realname=realname,
                           categories=categories,
                           filename=app.config['SPLASH_PHOTO_FILENAME'],
                           photo_padding=site_config['SPACING'],
                           bio=bio,
                           contact=contact)

@app.route('/change_splash_photo', methods=['GET', 'POST'])
def change_splash_photo():
    """View which on GET requests shows a page containing a form with which the
    user can upload a photo to be the background on the splash page, and on
    POST requests stores the new photo.

    """
    form = ChangeSplashPhotoForm()
    if form.validate_on_submit():
        if not session.get('logged_in'):
            abort(401)
        # HACK must add ophot/ prefix because app is run from top level dir.
        # TODO allow PNG files
        filename = os.path.join('ophot', app.config['SPLASH_PHOTO_FILENAME'])
        with open(filename, 'r+b') as photo_fd:
            request.files['photo'].save(photo_fd)
            photo_fd.seek(0)
            im = Image.open(photo_fd)
            if im.size[0] > int(app.config['SPLASH_PHOTO_WIDTH']) \
                    or im.size[1] > int(app.config['SPLASH_PHOTO_HEIGHT']):
                im = im.resize((int(app.config['SPLASH_PHOTO_WIDTH']),
                                int(app.config['SPLASH_PHOTO_HEIGHT'])),
                               Image.ANTIALIAS)
                im.save(photo_fd, im.format)
            flash('New splash photo uploaded.')
        return redirect(url_for('show_splash'))
    return render_template('change_splash_photo.html', form=form,
                           realname=realname,
                           height=app.config['SPLASH_PHOTO_HEIGHT'],
                           width=app.config['SPLASH_PHOTO_WIDTH'])

@app.route('/add', methods=['GET', 'POST'])
def add_photos():<commit_msg>open file in mode w+ instead of r+, so that it gets created if it does not yet exist<commit_after># imports from built-in modules
import os.path
from uuid import uuid4 as random_uuid

# imports from third-party modules
from flask import abort
from flask import flash
from flask import g
from flask import redirect
from flask import render_template
from flask import request
from flask import session
from flask import url_for
from flaskext.wtf import Form
from flaskext.wtf import FileField
from flaskext.wtf import SelectField
from flaskext.wtf.file import file_required
import Image

# imports from this application
from ophot import _get_categories
from ophot import _get_last_display_position
from ophot import app
from ophot import site_config
from ophot.forms import ChangeSplashPhotoForm
from ophot.forms import SettingsForm

# the first and last name of the photographer
realname = app.config['NAME']

# TODO use mime types or magic numbers to identify files
def _allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']

def _add_new_category(categoryname):
    """Creates a new category in the database with the specified *categoryname*
    and returns its ID number.

    """
    g.db.execute('insert into category (categoryname) values (?)',
                 [categoryname])
    g.db.commit()
    # get the ID of the category that we just inserted
    return g.db.execute('select categoryid from category where'
                        ' categoryname == "{0}"'.format(categoryname)).fetchone()[0]

def _generate_filename(directory, filename):
    """Generate the path at which to the save a file uploaded by the user.

    For now, this just generates a random UUID (version 4), then appends the
    same extension found from the input filename.
    
    """
    extension = filename.rsplit('.', 1)[1]
    uuid = random_uuid().hex # the string containing just the hex characters
    filename = os.path.join(directory, '{0}.{1}'.format(uuid, extension))
    while os.path.exists(filename):
        uuid = random_uuid().hex
        filename = os.path.join(directory, '{0}.{1}'.format(uuid, extension))
    return filename

def _get_categories_plus_new():
    """Helper method for the PhotoUploadForm which returns a list of pairs,
    each containing the category ID on the left and the category name on the
    right. The final element of this list is the pair (-1, 'new category...'),
    which is a sentinel value notifying the server that the user wishes to
    create a new category and apply these photos to it.

    Pre-condition: none of the existing categories have an ID of -1.
    """
    return _get_categories().items() + [(-1, 'new category...')]

# def _get_categories_plus_new_escaped():
#     """Helper method for the PhotoUploadForm which returns a list of pairs,
#     each containing the category ID on the left and the category name on the
#     right. The final element of this list is the pair (-1,
#     'new&nbsp;category...'), which is a sentinel value notifying the server
#     that the user wishes to create a new category and apply these photos to it.

#     Pre-condition: none of the existing categories have an ID of -1.
#     """
#     return _get_categories().items() + [(-1, 'new&nbsp;category...')]
    
def _to_html_paragraphs(string):
    """Splits the input string on newlines, then wraps HTML <p> tags around
    each non-empty line.

    """
    return ''.join('<p>{0}</p>'.format(line) for line in string.splitlines()
                   if line)

@app.route('/')
def show_splash():
    """Shows the splash page as the root."""
    categories = _get_categories().iteritems()
    bio = _to_html_paragraphs(site_config['BIO'])
    contact = _to_html_paragraphs(site_config['CONTACT'])
    return render_template('splash.html', realname=realname,
                           categories=categories,
                           filename=app.config['SPLASH_PHOTO_FILENAME'],
                           photo_padding=site_config['SPACING'],
                           bio=bio,
                           contact=contact)

@app.route('/change_splash_photo', methods=['GET', 'POST'])
def change_splash_photo():
    """View which on GET requests shows a page containing a form with which the
    user can upload a photo to be the background on the splash page, and on
    POST requests stores the new photo.

    """
    form = ChangeSplashPhotoForm()
    if form.validate_on_submit():
        if not session.get('logged_in'):
            abort(401)
        # HACK must add ophot/ prefix because app is run from top level dir.
        # TODO allow PNG files
        filename = os.path.join('ophot', app.config['SPLASH_PHOTO_FILENAME'])
        with open(filename, 'w+b') as photo_fd:
            request.files['photo'].save(photo_fd)
            photo_fd.seek(0)
            im = Image.open(photo_fd)
            if im.size[0] > int(app.config['SPLASH_PHOTO_WIDTH']) \
                    or im.size[1] > int(app.config['SPLASH_PHOTO_HEIGHT']):
                im = im.resize((int(app.config['SPLASH_PHOTO_WIDTH']),
                                int(app.config['SPLASH_PHOTO_HEIGHT'])),
                               Image.ANTIALIAS)
                im.save(photo_fd, im.format)
            flash('New splash photo uploaded.')
        return redirect(url_for('show_splash'))
    return render_template('change_splash_photo.html', form=form,
                           realname=realname,
                           height=app.config['SPLASH_PHOTO_HEIGHT'],
                           width=app.config['SPLASH_PHOTO_WIDTH'])

@app.route('/add', methods=['GET', 'POST'])
def add_photos():
{'fraction_non_alphanumeric': 0.09847213635962201, 'fraction_numerical': 0.004415790868144484, 'mean_word_length': 2.8847341337907375, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '3340555', 'n_tokens_mistral': 3242, 'n_tokens_neox': 3128, 'n_words': 1101}