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.

169 lines
5.2 KiB
Python

3 years ago
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
3 years ago
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()]
3 years ago
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):
3 years ago
slug = secure_filename(name)
os.mkdir(f'static/instruments/{slug}')
os.mkdir(f'static/instruments/{slug}/patches')
os.mkdir(f'static/instruments/{slug}/samples')
3 years ago
instrument = {
"name": name,
"slug": slug,
"description": description,
"params": params,
"sockets": sockets
3 years ago
}
with open(f'static/instruments/{slug}/model.yml', 'w') as f:
yaml.dump(instrument, f, sort_keys=False)
3 years ago
panel.save(f'static/instruments/{slug}/panel.svg')
3 years ago
3 years ago
@app.route("/")
def workbook():
return render_template('workbook.html')
@app.route("/<slug>")
def page(slug):
return render_template(f'{slug}.html')
@app.route("/instruments")
def instruments():
3 years ago
instruments = []
for filename in os.listdir('static/instruments'):
with open(f'static/instruments/{filename}/model.yml') as f:
3 years ago
instrument = yaml.load(f, Loader=SafeLoader)
instruments.append(instrument)
return render_template('instruments.html', instruments=instruments)
3 years ago
@app.route("/instruments/<instrument>")
3 years ago
def patches(instrument):
3 years ago
2 years ago
with open(f'static/instruments/{instrument}/panel.svg') as f:
2 years ago
panel = f.read()
3 years ago
2 years ago
patches = []
for filename in os.listdir(f'static/instruments/{instrument}/patches'):
with open(f'static/instruments/{instrument}/patches/{filename}/{filename}.yml') as f:
3 years ago
patch = yaml.load(f, Loader=SafeLoader)
patches.append(patch)
2 years ago
return render_template('patches.html', instrument=instrument, patches=patches, panel=panel)
3 years ago
@app.route("/instruments/add", methods=['GET', 'POST'])
3 years ago
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'))
3 years ago
panel = request.files['panel']
create_instrument(name, description, params, sockets, panel)
return redirect(url_for('instruments'))
3 years ago
return render_template('add_instrument.html')
@app.route("/instruments/<instrument>/add", methods=['GET', 'POST'])
3 years ago
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
Path(f'static/instruments/{instrument}/patches/{slug}').mkdir()
with open(f'static/instruments/{instrument}/patches/{slug}/{slug}.yml', 'w') as f:
3 years ago
yaml.dump(patch, f)
return redirect(url_for('patches', instrument=instrument))
with open(f'static/instruments/{instrument}/panel.svg') as f:
panel = f.read()
3 years ago
with open(f'static/instruments/{instrument}/model.yml') as f:
3 years ago
instrument = yaml.load(f, Loader=SafeLoader)
return render_template('add_patch.html', instrument=instrument, panel=panel)
3 years ago
@app.route("/instruments/<instrument>/<name>", methods=['GET', 'POST'])
3 years ago
def patch(instrument, name):
if request.method == 'POST':
file = request.files['snippet']
if file:
Path(f'static/instruments/{instrument}/patches/{name}/snippets').mkdir(exist_ok=True)
filename = secure_filename(file.filename)
file.save(os.path.join(f'static/instruments/{instrument}/patches/{name}/snippets', filename))
# description = request.form['description']
redirect(url_for('patch', instrument=instrument, name=name))
snippets = []
if os.path.exists(f'static/instruments/{instrument}/patches/{name}/snippets'):
for filename in os.listdir(f'static/instruments/{instrument}/patches/{name}/snippets'):
snippets.append(filename)
with open(f'static/instruments/{instrument}/panel.svg') as f:
panel = f.read()
with open(f'static/instruments/{instrument}/patches/{name}/{name}.yml') as f:
patch = yaml.load(f, Loader=SafeLoader)
with open(f'static/instruments/{instrument}/model.yml') as f:
instrument = yaml.load(f, Loader=SafeLoader)
return render_template('patch.html', instrument=instrument, patch=patch, panel=panel, snippets=snippets)
3 years ago
app.run(port=3146, debug=True)