summaryrefslogtreecommitdiff
path: root/server.py
blob: 3a9f24ced5b9b9d647e4f427f709627ff547e9b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from datetime import datetime
from flask import Flask
from flask import json, jsonify, render_template, request
from unipath import FSPath as Path

DATA_DIR = Path(__file__).absolute().ancestor(1).child('data')

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('home.html')

@app.route('/sequence', methods=['GET'])
@app.route('/sequence/<name>', methods=['POST', 'GET'])
def sequence(name=None):
    sequences_dir = DATA_DIR.child('sequences')

    if name and request.method == 'GET':
        with open(sequences_dir.child('{}.json'.format(name)), 'r') as f:
            return jsonify(json.load(f))

    sequences = [{
        'name': sequence.name.split('.json')[0],
        'pattern': json.load(open(sequence, 'r'))
    } for sequence in sequences_dir.walk(pattern='*.json')]

    if request.method == 'POST':
        with open(sequences_dir.child('{}.json'.format(name)), 'w') as f:
            json.dump(data, f, indent=2)

    return render_template('sequence.html', sequences=sequences)

def save_json(data, filename):
    with open(DATA_DIR.child(filename), 'w') as f:
        json.dump(data, f, indent=2)

def save_raw(data, filename):
    with open(DATA_DIR.child(filename), 'w') as f:
        f.write('{}\n\n'.format(data['tempo']))
        for tree in data['trees']:
            for row in tree:
                f.write(''.join(map(str, row)) + '\n')
            f.write('\n')

@app.route('/data', methods=['POST', 'GET'])
def data():
    if request.method == 'GET':
        with open(DATA_DIR.child('data.json'), 'r') as f:
            return jsonify(json.load(f))
    elif request.method == 'POST':
        ts = datetime.now().strftime('%Y%m%d_%H%M%S')
        save_json(request.json, 'data.json')
        save_json(request.json, 'data.{}.json'.format(ts))
        save_raw(request.json, 'data.txt')
        return 'ok'

if __name__ == '__main__':
    app.run(debug=True)