diff options
Diffstat (limited to 'server.py')
| -rw-r--r-- | server.py | 47 |
1 files changed, 9 insertions, 38 deletions
@@ -2,6 +2,7 @@ from datetime import datetime from flask import Flask from flask import json, jsonify, render_template, request from unipath import FSPath as Path +from uuid import uuid4 DATA_DIR = Path(__file__).absolute().ancestor(1).child('data') @@ -11,48 +12,18 @@ app = Flask(__name__) 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)) + with open(DATA_DIR.child('data.txt'), 'r') as f: + return jsonify({ + 'data': f.read() + }) 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') + with open(DATA_DIR.child('data.txt'), 'w') as f: + f.write(request.json['data']) + with open(DATA_DIR.child('data.{}.txt'.format(uuid4().hex[:10])), 'w') as f: + f.write(request.json['data']) return 'ok' if __name__ == '__main__': |
