You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.1 KiB
Python

from flask import Flask, render_template, request, redirect, url_for
from werkzeug.utils import secure_filename
import yaml
from yaml.loader import SafeLoader
import os
import json
from pathlib import Path
class PrefixMiddleware(object):
def __init__(self, app, prefix=""):
self.app = app
self.prefix = prefix
def __call__(self, environ, start_response):
if environ["PATH_INFO"].startswith(self.prefix):
environ["PATH_INFO"] = environ["PATH_INFO"][len(self.prefix) :]
environ["SCRIPT_NAME"] = self.prefix
return self.app(environ, start_response)
else:
start_response("404", [("Content-Type", "text/plain")])
return ["This url does not belong to the app.".encode()]
app = Flask(__name__)
# register the middleware to prefix all the requests with our base_url
app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix='/soupboat/workbook')
def create_instrument(name, description, params, sockets, panel):
slug = secure_filename(name)
os.mkdir(f'instruments/{slug}')
os.mkdir(f'instruments/{slug}/patches')
os.mkdir(f'instruments/{slug}/samples')
instrument = {
"name": name,
"slug": slug,
"description": description,
"params": params,
"sockets": sockets
}
with open(f'instruments/{slug}/model.yml', 'w') as f:
yaml.dump(instrument, f, sort_keys=False)
panel.save(f'instruments/{slug}/panel.svg')
@app.route("/")
def home():
return render_template('home.html')
@app.route("/workbook")
def workbook():
instruments = []
for filename in os.listdir('instruments'):
with open(f'instruments/{filename}/model.yml') as f:
instrument = yaml.load(f, Loader=SafeLoader)
instruments.append(instrument)
return render_template('workbook.html', instruments=instruments)
@app.route("/workbook/<instrument>")
def patches(instrument):
with open(f'instruments/{instrument}/panel.svg') as f:
panel = f.read()
patches = []
for filename in os.listdir(f'instruments/{instrument}/patches'):
with open(f'instruments/{instrument}/patches/{filename}') as f:
patch = yaml.load(f, Loader=SafeLoader)
patches.append(patch)
return render_template('patches.html', instrument=instrument, patches=patches, panel=panel)
@app.route("/workbook/add", methods=['GET', 'POST'])
def add_instrument():
if request.method == 'POST':
name = request.form.get('name')
description = request.form.get('description')
params = json.loads(request.form.get('params'))
sockets = json.loads(request.form.get('sockets'))
panel = request.files['panel']
create_instrument(name, description, params, sockets, panel)
return redirect(url_for('workbook'))
return render_template('add_instrument.html')
@app.route("/workbook/<instrument>/add", methods=['GET', 'POST'])
def add_patch(instrument):
if request.method == 'POST':
patch = request.form.to_dict()
patch = {k: v for k, v in patch.items() if v}
slug = secure_filename(patch["name"])
patch['slug'] = slug
with open(f'instruments/{instrument}/patches/{slug}.yml', 'w') as f:
yaml.dump(patch, f)
return redirect(url_for('patches', instrument=instrument))
with open(f'instruments/{instrument}/panel.svg') as f:
panel = f.read()
with open(f'instruments/{instrument}/model.yml') as f:
instrument = yaml.load(f, Loader=SafeLoader)
return render_template('add_patch.html', instrument=instrument, panel=panel)
@app.route("/workbook/<instrument>/<name>")
def patch(instrument, name):
with open(f'instruments/{instrument}/panel.svg') as f:
panel = f.read()
with open(f'instruments/{instrument}/patches/{name}.yml') as f:
patch = yaml.load(f, Loader=SafeLoader)
with open(f'instruments/{instrument}/model.yml') as f:
instrument = yaml.load(f, Loader=SafeLoader)
return render_template('patch.html', instrument=instrument, patch=patch, panel=panel)
app.run(port=3146, debug=True)