From fd770a171d1363b163843b0950cbb179d80753b2 Mon Sep 17 00:00:00 2001 From: Yuval Adam Date: Sat, 7 Jun 2014 22:13:29 +0300 Subject: Initial text only version --- server.py | 47 +++++++++-------------------------------------- 1 file changed, 9 insertions(+), 38 deletions(-) (limited to 'server.py') diff --git a/server.py b/server.py index 3a9f24c..4012330 100644 --- a/server.py +++ b/server.py @@ -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/', 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__': -- cgit v1.3.1