-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
executable file
·84 lines (75 loc) · 2.8 KB
/
app.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python
from flask import Flask, render_template, request, g
import json
import os
import pydot
import shortuuid
from dot_parser import ParseException
from pymongo import Connection
MONGO_URI = "mongodb://localhost"
MONGO_NS = "hamilton"
DEBUG = True
app = Flask(__name__, static_folder=".", template_folder=".", static_url_path="/static")
app.config.from_object(__name__)
app.config.from_envvar('HAMILTON_SETTINGS', silent=True)
@app.before_request
def connect_to_db():
conn = Connection(app.config["MONGO_URI"])
g.db = conn[app.config["MONGO_NS"]]
@app.teardown_request
def disconnect_from_db(exception):
if hasattr(g, 'db'):
g.db.connection.disconnect()
@app.route('/')
def index():
return render_template('index.html', graph="{}")
@app.route('/parse', methods=['GET', 'POST'])
def parse():
dotfile = request.form['dotfile']
try:
print dotfile
data = dotfile.encode('ascii','ignore') # because unicode strings lose edges
graph = pydot.graph_from_dot_data(data)
gattr = graph.get_attributes()
for (k,v) in gattr.iteritems(): gattr[k] = v[1:-1]
return_json = {'nodes': [], 'edges': [], 'name' : graph.get_name(),
'type': graph.get_type(),
'attributes': gattr }
for x in graph.get_node_list():
attr = x.get_attributes()
for (k,v) in attr.iteritems(): attr[k] = v[1:-1]
return_json['nodes'].append({'attributes' : attr,
'name' : x.get_name()})
for y in graph.get_edge_list():
attr = y.get_attributes()
for (k,v) in attr.iteritems(): attr[k] = v[1:-1]
return_json['edges'].append({'attributes' : attr,
'source' : y.get_source(),
'destination' : y.get_destination()})
return_json['status'] = 'success'
return json.dumps(return_json)
except ParseException, err:
message = err.line + "\n" + " "*(err.column-1) + "^" + "\n\n" + str(err)
return json.dumps({'status' : 'error', 'message': unicode(message)})
@app.route('/create', methods=['GET', 'POST'])
def create_fiddle():
try:
graphobj = json.loads(request.data)['graph']
uid = shortuuid.uuid()[:8]
g.db.graphs.insert({'uid': uid, 'graph': graphobj})
return json.dumps({'status': 'success', 'uid': unicode(uid)})
except KeyError as err:
message = err.line + ": sent bad graph data: %s" % str(err)
return json.dumps({'status': 'error', 'message': unicode(message)})
@app.route('/<fiddle_id>')
def get_fiddle(fiddle_id):
graph = g.db.graphs.find_one({"uid": fiddle_id })
if graph:
del graph['_id']
return render_template('index.html', graph=json.dumps(graph))
else:
# TODO raise 404
return "you suck"
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)