<commit_before>#!/usr/bin/env python
import peewee
from flask import Flask, g, render_template, request, redirect, url_for
from flask.ext.misaka import Misaka
from argparse import ArgumentParser
import logging
import re
import urlparse
import urllib

import settings

app = Flask(__name__)
Misaka(app)

database = peewee.SqliteDatabase(settings.DATABASE, threadlocals=True)

@app.context_processor
def add_random_page():
    page = Page.select().order_by(peewee.fn.Random()).limit(1)[0]
    return dict(random_page=page)

@app.context_processor
def add_site_settings():
    return dict(settings=settings)

linkSyntax = re.compile("\[\[(.+?)\]\]")
titledLinkSyntax = re.compile("\[\[(.+?)\|(.+?)\]\]")
def make_wikilink(match):
    groups = match.groups()
    if len(groups) == 1:
        title = groups[0]
        link = groups[0]
    else:
        title = groups[1]
        link = groups[0]
    return "[%s](%s)"%(title, link)

@app.template_filter('wikilinks')
def wikilinks(s):
    s = titledLinkSyntax.sub(make_wikilink, s)
    s = linkSyntax.sub(make_wikilink, s)
    return s

@app.before_request
def setup_db():
    g._database = database
    g._database.connect()

@app.teardown_appcontext
def close_db(error):<commit_msg>Dont crash in random_page if there isnt a page<commit_after>#!/usr/bin/env python
import peewee
from flask import Flask, g, render_template, request, redirect, url_for
from flask.ext.misaka import Misaka
from argparse import ArgumentParser
import logging
import re
import urlparse
import urllib

import settings

app = Flask(__name__)
Misaka(app)

database = peewee.SqliteDatabase(settings.DATABASE, threadlocals=True)

@app.context_processor
def add_random_page():
    page = None
    try:
        page = Page.select().order_by(peewee.fn.Random()).limit(1)[0]
    except:
        pass
    return dict(random_page=page)

@app.context_processor
def add_site_settings():
    return dict(settings=settings)

linkSyntax = re.compile("\[\[(.+?)\]\]")
titledLinkSyntax = re.compile("\[\[(.+?)\|(.+?)\]\]")
def make_wikilink(match):
    groups = match.groups()
    if len(groups) == 1:
        title = groups[0]
        link = groups[0]
    else:
        title = groups[1]
        link = groups[0]
    return "[%s](%s)"%(title, link)

@app.template_filter('wikilinks')
def wikilinks(s):
    s = titledLinkSyntax.sub(make_wikilink, s)
    s = linkSyntax.sub(make_wikilink, s)
    return s

@app.before_request
def setup_db():
    g._database = database
    g._database.connect()

@app.teardown_appcontext
def close_db(error):
{'fraction_non_alphanumeric': 0.13937966234786023, 'fraction_numerical': 0.005496662740478995, 'mean_word_length': 4.0858283433133735, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5634897', 'n_tokens_mistral': 901, 'n_tokens_neox': 880, 'n_words': 196}