<commit_before>#!/usr/bin/env python
import peewee
from flask import Flask, g, render_template, request, redirect, url_for, Response, current_app
from werkzeug import secure_filename
import werkzeug.exceptions
import werkzeug
from argparse import ArgumentParser
import logging
import os
import settings
import tempfile
from PIL import Image
from beaker.middleware import SessionMiddleware

import model
import context

app = Flask(__name__)
app.config.from_object('settings')
app.register_blueprint(context.bp)
app.register_blueprint(model.bp)

if settings.TEMP_DIR is None:
    settings.TEMP_DIR = tempfile.mkdtemp(prefix='spacewiki')

app.wsgi_app = SessionMiddleware(app.wsgi_app, {
  'session.type': 'file',
  'session.cookie_expires': False,
  'session.data_dir': settings.TEMP_DIR
})

if settings.ADMIN_EMAILS:
    from logging.handlers import SMTPHandler
    mail_handler = SMTPHandler('127.0.0.1',
        'spacewiki@localhost',
        settings.ADMIN_EMAILS, 'SpaceWiki error')
    mail_handler.setLevel(logging.ERROR)
    app.logger.addHandler(mail_handler)

@app.route("/<slug>/revert", methods=['POST'])
def revert(slug):
    revision = request.form['revision']
    message = request.form['message']
    author = request.form['author']
    page = model.Page.get(slug=slug)
    oldRevision = model.Revision.get(page=page, id=revision)
    page.newRevision(oldRevision.body, "Revert to revision %s from %s: %s"
        %(oldRevision.id, oldRevision.timestamp, message), author)
    session = request.environ['beaker.session']
    session['author'] = author
    session.save()
    return redirect(url_for('view', slug=page.slug))

@app.route("/<slug>/history")
def history(slug):
    """View the revision list of a page"""
    page = model.Page.get(slug=slug)
    return render_template('history.html', page=page)

@app.route("/<slug>/attach", methods=['GET'])
def upload(slug):
    """Show the file attachment form"""
    try:
        page = model.Page.get(slug=slug)
    except peewee.DoesNotExist:
        page = model.Page.create(title=slug, slug=slug)
    return render_template('attach.html', page=page)

@app.route("/<slug>/attach", methods=['POST'])
def attach(slug):
    """Handle saving a file upload"""
    try:
        page = model.Page.get(slug=slug)
        logging.debug("Attaching file to existing page: %s", page.slug)
    except peewee.DoesNotExist:
        page = model.Page.create(title=slug, slug=slug)
        logging.debug("Created new page for attachment: %s", page.slug)
    file = request.files['file']
    fname = secure_filename(file.filename)
    tmpname = os.path.join(tempfile.mkdtemp(), "upload")
    with model.database.transaction():
        file.save(tmpname)
        page.attachUpload(tmpname, fname, current_app.config['UPLOAD_PATH'])
    return redirect(url_for('view', slug=page.slug))

@app.route("/<slug>/file/<fileslug>")
@app.route("/<slug>/file/<fileslug>/<size>")
def get_attachment(slug, fileslug, size=None):
    attachment = model.Attachment.findAttachment(slug, fileslug)
    if attachment is None:
        logging.info("No attachment %s on %s", fileslug, slug)
        return Response(status=404)
    latestRevision = attachment.revisions[0]
    maxSize = None

    if size is not None:
        try:
            maxSize = max(0, int(size))
        except ValueError, e:
            maxSize = -1

    if maxSize is not None and maxSize <= 0:
        raise werkzeug.exceptions.NotFound()

    def generate():
        fname = model.Attachment.hashPath(latestRevision.sha,
            attachment.filename)
        if maxSize is not None:
            resizedFname = os.path.join(current_app.config['UPLOAD_PATH'], fname)+'-%s'%(maxSize)
            if not os.path.exists(resizedFname):
                img = Image.open(os.path.join(current_app.config['UPLOAD_PATH'], fname))
                w, h = img.size
                if w > h:
                  scale = float(maxSize) / w
                  w = maxSize
                  h = h * scale
                else:
                  scale = float(maxSize) / h
                  h = maxSize
                  w = w * scale
                img.thumbnail([w, h], Image.ANTIALIAS)
                img.save(resizedFname, format='png')
            f = open(resizedFname, 'r')
        else:
            f = open(os.path.join(current_app.config['UPLOAD_PATH'], fname))
        buf = f.read(2048)
        while buf:
            yield buf
            buf = f.read(2048)
    """FIXME: mimetype detection"""
    mimetype = 'image/png; charset=binary'
    return Response(generate(), mimetype=mimetype)

@app.route("/<slug>", methods=['POST'])
def save(slug):
    """Save a new Revision, creating a new Page if needed"""
    try:
        page = model.Page.get(slug=slug)
        logging.debug("Updating existing page: %s", page.slug)
    except peewee.DoesNotExist:
        page = model.Page.create(title=request.form['title'], slug=request.form['title'])
        logging.debug("Created new page: %s (%s)", page.title, page.slug)
    page.newRevision(request.form['body'], request.form['message'],
        request.form['author'])
    session = request.environ['beaker.session']
    session['author'] = request.form['author']
    session.save()
    return redirect(url_for('view', slug=page.slug))

@app.route("/<slug>/edit", methods=['GET'])
def edit(slug, redirectFrom=None):
    """Show the editing form for a page"""
    revision = model.Page.latestRevision(slug)
    if revision is not None:
        return render_template('edit.html',
            page=revision.page, revision=revision, slug=revision.page.slug,
            redirectFrom=redirectFrom)
    else:
        page = None<commit_msg>uploads: give stream generator an app context<commit_after>#!/usr/bin/env python
import peewee
from flask import Flask, g, render_template, request, redirect, url_for, Response, current_app
from werkzeug import secure_filename
import werkzeug.exceptions
import werkzeug
from argparse import ArgumentParser
import logging
import os
import settings
import tempfile
from PIL import Image
from beaker.middleware import SessionMiddleware

import model
import context

app = Flask(__name__)
app.config.from_object('settings')
app.register_blueprint(context.bp)
app.register_blueprint(model.bp)

if settings.TEMP_DIR is None:
    settings.TEMP_DIR = tempfile.mkdtemp(prefix='spacewiki')

app.wsgi_app = SessionMiddleware(app.wsgi_app, {
  'session.type': 'file',
  'session.cookie_expires': False,
  'session.data_dir': settings.TEMP_DIR
})

if settings.ADMIN_EMAILS:
    from logging.handlers import SMTPHandler
    mail_handler = SMTPHandler('127.0.0.1',
        'spacewiki@localhost',
        settings.ADMIN_EMAILS, 'SpaceWiki error')
    mail_handler.setLevel(logging.ERROR)
    app.logger.addHandler(mail_handler)

@app.route("/<slug>/revert", methods=['POST'])
def revert(slug):
    revision = request.form['revision']
    message = request.form['message']
    author = request.form['author']
    page = model.Page.get(slug=slug)
    oldRevision = model.Revision.get(page=page, id=revision)
    page.newRevision(oldRevision.body, "Revert to revision %s from %s: %s"
        %(oldRevision.id, oldRevision.timestamp, message), author)
    session = request.environ['beaker.session']
    session['author'] = author
    session.save()
    return redirect(url_for('view', slug=page.slug))

@app.route("/<slug>/history")
def history(slug):
    """View the revision list of a page"""
    page = model.Page.get(slug=slug)
    return render_template('history.html', page=page)

@app.route("/<slug>/attach", methods=['GET'])
def upload(slug):
    """Show the file attachment form"""
    try:
        page = model.Page.get(slug=slug)
    except peewee.DoesNotExist:
        page = model.Page.create(title=slug, slug=slug)
    return render_template('attach.html', page=page)

@app.route("/<slug>/attach", methods=['POST'])
def attach(slug):
    """Handle saving a file upload"""
    try:
        page = model.Page.get(slug=slug)
        logging.debug("Attaching file to existing page: %s", page.slug)
    except peewee.DoesNotExist:
        page = model.Page.create(title=slug, slug=slug)
        logging.debug("Created new page for attachment: %s", page.slug)
    file = request.files['file']
    fname = secure_filename(file.filename)
    tmpname = os.path.join(tempfile.mkdtemp(), "upload")
    with model.database.transaction():
        file.save(tmpname)
        page.attachUpload(tmpname, fname, current_app.config['UPLOAD_PATH'])
    return redirect(url_for('view', slug=page.slug))

@app.route("/<slug>/file/<fileslug>")
@app.route("/<slug>/file/<fileslug>/<size>")
def get_attachment(slug, fileslug, size=None):
    attachment = model.Attachment.findAttachment(slug, fileslug)
    if attachment is None:
        logging.info("No attachment %s on %s", fileslug, slug)
        return Response(status=404)
    latestRevision = attachment.revisions[0]
    maxSize = None

    if size is not None:
        try:
            maxSize = max(0, int(size))
        except ValueError, e:
            maxSize = -1

    if maxSize is not None and maxSize <= 0:
        raise werkzeug.exceptions.NotFound()

    def generate():
        with app.app_context():
            fname = model.Attachment.hashPath(latestRevision.sha,
                attachment.filename)
            if maxSize is not None:
                resizedFname = os.path.join(current_app.config['UPLOAD_PATH'], fname)+'-%s'%(maxSize)
                if not os.path.exists(resizedFname):
                    img = Image.open(os.path.join(current_app.config['UPLOAD_PATH'], fname))
                    w, h = img.size
                    if w > h:
                      scale = float(maxSize) / w
                      w = maxSize
                      h = h * scale
                    else:
                      scale = float(maxSize) / h
                      h = maxSize
                      w = w * scale
                    img.thumbnail([w, h], Image.ANTIALIAS)
                    img.save(resizedFname, format='png')
                f = open(resizedFname, 'r')
            else:
                f = open(os.path.join(current_app.config['UPLOAD_PATH'], fname))
            buf = f.read(2048)
            while buf:
                yield buf
                buf = f.read(2048)
    """FIXME: mimetype detection"""
    mimetype = 'image/png; charset=binary'
    return Response(generate(), mimetype=mimetype)

@app.route("/<slug>", methods=['POST'])
def save(slug):
    """Save a new Revision, creating a new Page if needed"""
    try:
        page = model.Page.get(slug=slug)
        logging.debug("Updating existing page: %s", page.slug)
    except peewee.DoesNotExist:
        page = model.Page.create(title=request.form['title'], slug=request.form['title'])
        logging.debug("Created new page: %s (%s)", page.title, page.slug)
    page.newRevision(request.form['body'], request.form['message'],
        request.form['author'])
    session = request.environ['beaker.session']
    session['author'] = request.form['author']
    session.save()
    return redirect(url_for('view', slug=page.slug))

@app.route("/<slug>/edit", methods=['GET'])
def edit(slug, redirectFrom=None):
    """Show the editing form for a page"""
    revision = model.Page.latestRevision(slug)
    if revision is not None:
        return render_template('edit.html',
            page=revision.page, revision=revision, slug=revision.page.slug,
            redirectFrom=redirectFrom)
    else:
        page = None
{'fraction_non_alphanumeric': 0.13332755031228313, 'fraction_numerical': 0.0036433032616238724, 'mean_word_length': 2.9782608695652173, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '3410014', 'n_tokens_mistral': 3597, 'n_tokens_neox': 3612, 'n_words': 789}