<commit_before>#!/usr/bin/env python
import argparse
import urllib
import logging
import logging.config

from flask import Flask, g, redirect, url_for, render_template, flash, request, Markup

import savant.db
import savant.comparisons
import savant.diffs
import savant.sets

import forms

# configuration
DEBUG = True
SECRET_KEY = 'development key'

app = Flask(__name__)
app.config.from_object(__name__)

logging.config.fileConfig('log.conf')

@app.before_request
def before_request():
    g.db = savant.db.DB(args.inference_db)

@app.route('/')
def home():
    return render_template(
            'home.html',
            )

@app.route('/comparisons')
def comparisons():
    all_comps = savant.comparisons.all(g.db)
    complete = []
    incomplete = []

    for comp_id in all_comps:
        comp_obj = savant.comparisons.Comparison(g.db, id=comp_id)
        if comp_obj.all_assigned():
            complete.append(comp_obj.id)
        else:
            incomplete.append(comp_obj.id)

    return render_template(
            'comparisons.html',
            comparisons = all_comps,
            complete = complete,
            incomplete = incomplete,
            )

@app.route('/comparison/<id>')
def comparison(id=None):
    comp_obj = savant.comparisons.Comparison(g.db, id=id)

    sets = []
    for diff_id in comp_obj.get_diff_ids():
        diff = savant.diffs.Diff(diff_id)
        sets.extend(savant.sets.find_with_diff(diff, g.db))
    sets = sorted(list(set(sets)))
    sets = [savant.sets.Set(g.db, s) for s in sets]
    exclude_set_ids = [s.id for s in sets]

    include_id = request.args.get('include')
    if include_id is not None:
        include_id = urllib.unquote(include_id)
        exclude_set_ids.remove(include_id)
    dform = forms.DDiffNamingForm(g.db, id, exclude_set_ids=exclude_set_ids)

    return render_template(
            'comparison.html',
            id = id,
            sets = sets,
            form = dform.form,
            )

@app.route('/sets')
def sets():
    set_ids = savant.sets.all(g.db)
    sets = []
    for set_id in set_ids:
        set_obj = savant.sets.Set(g.db, set_id)

        comparisons = []
        other_sets = []

        for diff_id in set_obj.get_diff_ids():
            diff = savant.diffs.Diff(diff_id)
            comparisons.extend(savant.comparisons.find_with_diff(diff, g.db))
            other_sets.extend(savant.sets.find_with_diff(diff, g.db))

        comparisons = sorted(list(set(comparisons)))

        other_sets = sorted(list(set(other_sets)))
        other_sets.remove(set_id)
        other_sets = [savant.sets.Set(g.db, s) for s in other_sets]

        data = {
                'set': set_obj,
                'comparisons': comparisons,
                'other_sets': other_sets,
                }
        sets.append(data)
    return render_template('sets.html', sets=sets)

@app.route('/set/edit/<escaped_id>')
def set_edit(escaped_id=None):
    set_id = urllib.unquote(escaped_id)
    dform = forms.DDiffForm(g.db, set_id)
    set_obj = dform.set_obj

    comparisons = []
    for diff_id in set_obj.get_diff_ids():
        diff = savant.diffs.Diff(diff_id)
        comparisons.extend(savant.comparisons.find_with_diff(diff, g.db))
    comparisons = sorted(list(set(comparisons)))
    comparisons = [savant.comparisons.Comparison(g.db, c) for c in comparisons]

    return render_template(
            'set.html',
            set = set_obj,
            comparisons = comparisons,
            form = dform.form)

@app.route('/set/add', methods=['POST'])
def set_add():
    id=request.form['id']
    dform = forms.DDiffNamingForm(g.db, id, request)
    new_set = savant.sets.Set(g.db, dform.set_id)
    new_set.update_diffs(dform.set_choices)
    message = Markup('New set; action: <strong>%s</strong>; system: <strong>%s</strong>; name: <strong>%s</strong>; diffs: <strong>%s</strong>' %
        (
        dform.form.action.data,
        dform.form.system.data,
        dform.form.name.data,
        str(len(dform.set_choices)),
        ))
    flash(message)
    return redirect(url_for('comparison', id=id))
<commit_msg>Disallow creating sets without diffs<commit_after>#!/usr/bin/env python
import argparse
import urllib
import logging
import logging.config

from flask import Flask, g, redirect, url_for, render_template, flash, request, Markup

import savant.db
import savant.comparisons
import savant.diffs
import savant.sets

import forms

# configuration
DEBUG = True
SECRET_KEY = 'development key'

app = Flask(__name__)
app.config.from_object(__name__)

logging.config.fileConfig('log.conf')

@app.before_request
def before_request():
    g.db = savant.db.DB(args.inference_db)

@app.route('/')
def home():
    return render_template(
            'home.html',
            )

@app.route('/comparisons')
def comparisons():
    all_comps = savant.comparisons.all(g.db)
    complete = []
    incomplete = []

    for comp_id in all_comps:
        comp_obj = savant.comparisons.Comparison(g.db, id=comp_id)
        if comp_obj.all_assigned():
            complete.append(comp_obj.id)
        else:
            incomplete.append(comp_obj.id)

    return render_template(
            'comparisons.html',
            comparisons = all_comps,
            complete = complete,
            incomplete = incomplete,
            )

@app.route('/comparison/<id>')
def comparison(id=None):
    comp_obj = savant.comparisons.Comparison(g.db, id=id)

    sets = []
    for diff_id in comp_obj.get_diff_ids():
        diff = savant.diffs.Diff(diff_id)
        sets.extend(savant.sets.find_with_diff(diff, g.db))
    sets = sorted(list(set(sets)))
    sets = [savant.sets.Set(g.db, s) for s in sets]
    exclude_set_ids = [s.id for s in sets]

    include_id = request.args.get('include')
    if include_id is not None:
        include_id = urllib.unquote(include_id)
        exclude_set_ids.remove(include_id)
    dform = forms.DDiffNamingForm(g.db, id, exclude_set_ids=exclude_set_ids)

    return render_template(
            'comparison.html',
            id = id,
            sets = sets,
            form = dform.form,
            )

@app.route('/sets')
def sets():
    set_ids = savant.sets.all(g.db)
    sets = []
    for set_id in set_ids:
        set_obj = savant.sets.Set(g.db, set_id)

        comparisons = []
        other_sets = []

        for diff_id in set_obj.get_diff_ids():
            diff = savant.diffs.Diff(diff_id)
            comparisons.extend(savant.comparisons.find_with_diff(diff, g.db))
            other_sets.extend(savant.sets.find_with_diff(diff, g.db))

        comparisons = sorted(list(set(comparisons)))

        other_sets = sorted(list(set(other_sets)))
        other_sets.remove(set_id)
        other_sets = [savant.sets.Set(g.db, s) for s in other_sets]

        data = {
                'set': set_obj,
                'comparisons': comparisons,
                'other_sets': other_sets,
                }
        sets.append(data)
    return render_template('sets.html', sets=sets)

@app.route('/set/edit/<escaped_id>')
def set_edit(escaped_id=None):
    set_id = urllib.unquote(escaped_id)
    dform = forms.DDiffForm(g.db, set_id)
    set_obj = dform.set_obj

    comparisons = []
    for diff_id in set_obj.get_diff_ids():
        diff = savant.diffs.Diff(diff_id)
        comparisons.extend(savant.comparisons.find_with_diff(diff, g.db))
    comparisons = sorted(list(set(comparisons)))
    comparisons = [savant.comparisons.Comparison(g.db, c) for c in comparisons]

    return render_template(
            'set.html',
            set = set_obj,
            comparisons = comparisons,
            form = dform.form)

@app.route('/set/add', methods=['POST'])
def set_add():
    id=request.form['id']
    dform = forms.DDiffNamingForm(g.db, id, request)
    if len(dform.set_choices) == 0:
        flash(Markup('Did you forget to choose diffs?'))
        return redirect(url_for('comparison', id=id))

    new_set = savant.sets.Set(g.db, dform.set_id)
    new_set.update_diffs(dform.set_choices)
    message = Markup('New set; action: <strong>%s</strong>; system: <strong>%s</strong>; name: <strong>%s</strong>; diffs: <strong>%s</strong>' %
        (
        dform.form.action.data,
        dform.form.system.data,
        dform.form.name.data,
        str(len(dform.set_choices)),
        ))
    flash(message)
    return redirect(url_for('comparison', id=id))

{'fraction_non_alphanumeric': 0.1262066499821237, 'fraction_numerical': 0.00011917530687641521, 'mean_word_length': 2.6550522648083623, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '4174209', 'n_tokens_mistral': 2889, 'n_tokens_neox': 2859, 'n_words': 516}