<commit_before>import flask
import hemlib
import json
import os
from os.path import join
import sys

app = flask.Flask(__name__)

SRCDIR = "static/coffee"
TEST_SRCDIR = "tests/coffee"
DEMO_SRCDIR = "demo/coffee"
# TODO: Should be able to remove EXCLUDES now that demo and tests have
# been moved out.
EXCLUDES = [join(SRCDIR,"demo"), join(SRCDIR,"unittest"),
            join(SRCDIR,"unittest/primitives")]

HOST = "localhost"
slug = json.load(open("slug.all.json"))
@app.route("/demo/<demoname>")
def demo(demoname):
    demos = alldemos[demoname]
    demofiles = [os.path.join(DEMO_SRCDIR, name+".coffee") for name in demos]
    if app.debug:
        jslibs = hemlib.slug_libs(app, slug['libs'])
        hemfiles = hemlib.coffee_assets(SRCDIR, HOST, slug['port'])
        hemfiles.extend(hemlib.make_urls(demofiles, HOST, slug['port']))
    else:
        jslibs= ['/static/js/demo/application.js']
        hemfiles = []
    for demo in demofiles:
        if not os.path.isfile(demo):
            raise RuntimeError("Cannot find demo named '%s'"%demo)
    return flask.render_template("demos.html", jslibs = jslibs,
                                 hemfiles=hemfiles, demos=demos)

@app.route("/test/<testname>")
def test(testname):
    tests = alltests[testname]
    testfiles = [os.path.join(TEST_SRCDIR, name+".coffee") for name in tests]
    for test in testfiles:
        if not os.path.isfile(test):
            raise RuntimeError("Cannot find test named '%s'" % test)
    if app.debug:
        jslibs = hemlib.slug_libs(app, slug['libs'])
        hemfiles = hemlib.coffee_assets(SRCDIR, HOST, slug['port'],
                                        excludes=EXCLUDES)
        hemfiles.extend(hemlib.make_urls(testfiles, HOST, slug['port']))
        print "demoserver hemfiles", hemfiles
    else:
        jslibs= ['/static/js/demo/application.js']
        hemfiles = []
    return flask.render_template("tests.html", jslibs=jslibs,
            hemfiles=hemfiles, tests=tests)


alldemos = {

    'all' : [
        'scatter',
        'image',
        'bars',
        'map',
        'map_overlay',
        'candle',
        'vector',
        'group',
        'stack',
        'lorenz10',
        'lorenz50',
        'lorenz100',
    ],

    'scatter'    : ['scatter'],<commit_msg> much simpler demoserver, demo and test refactored to remove code duplication<commit_after>import flask
import hemlib
import json
import os
from os.path import join
import sys

app = flask.Flask(__name__)

SRCDIR = "static/coffee"
TEST_SRCDIR = "tests/coffee"
DEMO_SRCDIR = "demo/coffee"
# TODO: Should be able to remove EXCLUDES now that demo and tests have
# been moved out.
EXCLUDES = [join(SRCDIR,"demo"), join(SRCDIR,"unittest"),
            join(SRCDIR,"unittest/primitives")]

HOST = "localhost"
slug = json.load(open("slug.all.json"))

def display_page(file_set, template, **extra_template_vars):
    for f in file_set:
        if not os.path.isfile(f):
            raise RuntimeError("Cannot find file named '%s'" % f)
    if app.debug:
        jslibs = hemlib.slug_libs(app, slug['libs'])
        hemfiles = hemlib.coffee_assets(SRCDIR, HOST, slug['port'],
                                        excludes=EXCLUDES)
        hemfiles.extend(hemlib.make_urls(file_set, HOST, slug['port']))
        print "demoserver hemfiles", hemfiles
    else:
        jslibs= ['/static/js/demo/application.js']
        hemfiles = []
    return flask.render_template(template, jslibs=jslibs,
            hemfiles=hemfiles, **extra_template_vars)

@app.route("/demo/<demoname>")
def demo(demoname):
    demos = alldemos[demoname]
    demofiles = [os.path.join(DEMO_SRCDIR, name+".coffee") for name in demos]
    return display_page(demofiles, "demos.html", demos=demos)

@app.route("/test/<testname>")
def test(testname):
    tests = alltests[testname]
    testfiles = [os.path.join(TEST_SRCDIR, name+".coffee") for name in tests]
    return display_page(testfiles, "tests.html", tests=tests)
    
alldemos = {

    'all' : [
        'scatter',
        'image',
        'bars',
        'map',
        'map_overlay',
        'candle',
        'vector',
        'group',
        'stack',
        'lorenz10',
        'lorenz50',
        'lorenz100',
    ],

    'scatter'    : ['scatter'],
{'fraction_non_alphanumeric': 0.1367902393242609, 'fraction_numerical': 0.00328484279680901, 'mean_word_length': 2.7493403693931397, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4173723', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1463, 'n_words': 309}