<commit_before>import os
import sys
import time
import socket
import tempfile
import urllib2
from StringIO import StringIO
import urlparse
from flask import Flask, send_file, request, abort
from utils import jsonify

# TODO remove this and do use client launcher
this_dir = os.path.dirname(os.path.realpath(__file__))
core_dir = os.path.join(os.path.dirname(this_dir), 'core')
sys.path.insert(1, core_dir)
# leave this though
from module import use_module, path, packaging

# flask app
app = Flask(__name__, static_folder='public', static_url_path='')

STORE_URL = 'http://localhost:8889'

def allowed_file(filename, exts):
    return '.' in filename and filename.rsplit('.', 1)[1] in exts
IMAGE_EXTS = ['png', 'jpg', 'jpeg', 'gif', 'bmp']

# fix for index page
@app.route('/')
def index():
    return app.send_static_file('index.html')

# move in module ?
@app.route('/api/rooms')
def api_rooms():
    return app.send_static_file('rooms.json')

@app.route('/api/modules')
def api_modules():
    return jsonify(packaging.get_installed_modules(detailed=True))

@app.route('/api/modules/<module_name>/fields')
def api_module_fields(module_name):
    try:
        return jsonify(use_module(module_name).fields)
    except socket.error:
        abort(404)

@app.route('/api/modules/install', methods=['POST'])
def api_install_module():
    try:
        module_name = request.form['name']
    except KeyError:
        abort(400)
    else:
        try:
            io = StringIO()
            zipfile = urllib2.urlopen(urlparse.urljoin(STORE_URL,
                '/api/available_modules/%s/download' % module_name))
            io.write(zipfile.read())
            packaging.install_from_zip(io)
        except urllib2.HTTPError:
            abort(404)
        except IOError:
            abort(403)
        return jsonify({ 'success': False })

@app.route('/api/modules/install', methods=['POST'])
def api_upload_module():<commit_msg>Decorator for routing, uninstall started<commit_after>import os
import sys
import time
import socket
import urllib2
import tempfile
from functools import wraps
from StringIO import StringIO
import urlparse
from flask import Flask, send_file, request, abort
from utils import jsonify

# TODO remove this and do use client launcher
this_dir = os.path.dirname(os.path.realpath(__file__))
core_dir = os.path.join(os.path.dirname(this_dir), 'core')
sys.path.insert(1, core_dir)
# leave this though
from module import use_module, path, packaging

# flask app
app = Flask(__name__, static_folder='public', static_url_path='')

STORE_URL = 'http://localhost:8889'

def allowed_file(filename, exts):
    return '.' in filename and filename.rsplit('.', 1)[1] in exts
IMAGE_EXTS = ['png', 'jpg', 'jpeg', 'gif', 'bmp']

# fix for index page
@app.route('/')
def index():
    return app.send_static_file('index.html')

# move in module ?
@app.route('/api/rooms')
def api_rooms():
    return app.send_static_file('rooms.json')

@app.route('/api/modules')
def api_modules():
    return jsonify(packaging.get_installed_modules(detailed=True))

@app.route('/api/modules/<module_name>/fields')
def api_module_fields(module_name):
    try:
        return jsonify(use_module(module_name).fields)
    except socket.error:
        abort(404)

def route_with_module_posted(url):
    def decorator(f):
        @wraps(f)
        def view(*args, **kwargs):
            try:
                module_name = request.form['name']
            except KeyError:
                abort(400)
            else:
                return f(module_name, *args, **kwargs)
        return app.add_url_rule(url, view_func=view, methods=['POST'])
    return decorator

@route_with_module_posted('/api/modules/install')
def api_install_module(module_name):
    try:
        io = StringIO()
        zipfile = urllib2.urlopen(urlparse.urljoin(STORE_URL,
            '/api/available_modules/%s/download' % module_name))
        io.write(zipfile.read())
        packaging.install_from_zip(io)
    except urllib2.HTTPError:
        abort(404)
    except IOError:
        abort(403)
    return jsonify({ 'success': False })

@route_with_module_posted('/api/modules/uninstall/<instance_id>')
def api_uninstall_module(module_name, instance_id):
    print 'Stopping instance %s of module %s' % (instance_id, module_name)
    pass #instance.stop(module_name, instance_id)

@app.route('/api/modules/upload', methods=['POST'])
def api_upload_module():
{'fraction_non_alphanumeric': 0.12856496152105024, 'fraction_numerical': 0.009959257582616569, 'mean_word_length': 3.7824675324675323, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3050437', 'n_tokens_mistral': 1448, 'n_tokens_neox': 1425, 'n_words': 326}