-
Notifications
You must be signed in to change notification settings - Fork 0
/
savant_web.py
executable file
·227 lines (192 loc) · 6.82 KB
/
savant_web.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python
import argparse
import urllib
import logging
import logging.config
from flask import Flask, g, redirect, url_for, render_template, flash, request, Markup
import flask_debugtoolbar
from flask_debugtoolbar_lineprofilerpanel.profile import line_profile
import savant.db
import savant.comparisons
import savant.diffs
import savant.sets
import forms
# configuration
DEBUG = True
SECRET_KEY = 'development key'
app = Flask(__name__)
app.config.from_object(__name__)
app.debug = True
app.config['DEBUG_TB_PANELS'] = [
'flask_debugtoolbar.panels.versions.VersionDebugPanel',
'flask_debugtoolbar.panels.timer.TimerDebugPanel',
'flask_debugtoolbar.panels.headers.HeaderDebugPanel',
'flask_debugtoolbar.panels.request_vars.RequestVarsDebugPanel',
'flask_debugtoolbar.panels.template.TemplateDebugPanel',
#'flask_debugtoolbar.panels.sqlalchemy.SQLAlchemyDebugPanel',
#'flask_debugtoolbar.panels.logger.LoggingPanel',
'flask_debugtoolbar.panels.profiler.ProfilerDebugPanel',
'flask_debugtoolbar_lineprofilerpanel.panels.LineProfilerPanel'
]
app.config['DEBUG_TB_PROFILER_ENABLED'] = True
toolbar = flask_debugtoolbar.DebugToolbarExtension(app)
try:
logging.config.fileConfig('log.conf', disable_existing_loggers=False)
except ConfigParser.NoSectionError:
# probably no log.conf file
logging.basicConfig(
format='%(message)s',
)
logger = logging.getLogger(__name__)
try:
from log_override import LOG_OVERRIDES
logging.config.dictConfig(LOG_OVERRIDES)
except:
logger.debug('unable to load log_override; ignoring')
@app.before_request
def before_request():
g.db = savant.db.DB(args.inference_db)
@app.after_request
def after_request(response):
g.db.close()
return response
@app.route('/')
def home():
return render_template(
'home.html',
)
@app.route('/comparisons')
def comparisons():
all_comps = savant.comparisons.all(g.db)
complete = []
incomplete = []
for comp_id in all_comps:
comp_obj = savant.comparisons.Comparison(g.db, id=comp_id)
if comp_obj.all_assigned():
complete.append(comp_obj.id)
else:
incomplete.append(comp_obj.id)
return render_template(
'comparisons.html',
comparisons = all_comps,
complete = complete,
incomplete = incomplete,
)
@app.route('/comparison/<id>')
def comparison(id=None):
comp_obj = savant.comparisons.Comparison(g.db, id=id)
sets = []
for diff_id in comp_obj.get_diff_ids():
diff = savant.diffs.Diff(diff_id)
sets.extend(savant.sets.find_with_diff(diff, g.db))
sets = sorted(list(set(sets)))
sets = [savant.sets.Set(g.db, s) for s in sets]
exclude_set_ids = [s.id for s in sets]
include_id = request.args.get('include')
if include_id is not None:
include_id = urllib.unquote(include_id)
exclude_set_ids.remove(include_id)
dform = forms.DDiffNamingForm(g.db, id, exclude_set_ids=exclude_set_ids)
return render_template(
'comparison.html',
id = id,
sets = sets,
form = dform.form,
)
@app.route('/sets')
def sets():
set_ids = savant.sets.all(g.db)
sets = []
for set_id in set_ids:
set_obj = savant.sets.Set(g.db, set_id)
comparisons = []
other_sets = []
for diff_id in set_obj.get_diff_ids():
diff = savant.diffs.Diff(diff_id)
comparisons.extend(savant.comparisons.find_with_diff(diff, g.db))
other_sets.extend(savant.sets.find_with_diff(diff, g.db))
comparisons = sorted(list(set(comparisons)))
other_sets = sorted(list(set(other_sets)))
other_sets.remove(set_id)
other_sets = [savant.sets.Set(g.db, s) for s in other_sets]
data = {
'set': set_obj,
'comparisons': comparisons,
'other_sets': other_sets,
}
sets.append(data)
return render_template('sets.html', sets=sets)
@app.route('/set/edit/<escaped_id>')
def set_edit(escaped_id=None):
set_id = urllib.unquote(escaped_id)
dform = forms.DDiffForm(g.db, set_id)
set_obj = dform.set_obj
comparisons = []
for diff_id in set_obj.get_diff_ids():
diff = savant.diffs.Diff(diff_id)
comparisons.extend(savant.comparisons.find_with_diff(diff, g.db))
comparisons = sorted(list(set(comparisons)))
comparisons = [savant.comparisons.Comparison(g.db, c) for c in comparisons]
return render_template(
'set.html',
set = set_obj,
comparisons = comparisons,
form = dform.form)
@app.route('/set/add', methods=['POST'])
def set_add():
id=request.form['id']
dform = forms.DDiffNamingForm(g.db, id, request)
if len(dform.set_choices) == 0:
flash(Markup('Did you forget to choose diffs?'))
return redirect(url_for('comparison', id=id))
new_set = savant.sets.Set(g.db, dform.set_id)
new_set.update_diffs(dform.set_choices)
message = Markup('New set; action: <strong>%s</strong>; system: <strong>%s</strong>; name: <strong>%s</strong>; diffs: <strong>%s</strong>' %
(
dform.form.action.data,
dform.form.system.data,
dform.form.name.data,
str(len(dform.set_choices)),
))
flash(message)
return redirect(url_for('comparison', id=id))
@app.route('/set/update_diffs', methods=['POST'])
def set_update_diffs():
escaped_id=request.form['escaped_id']
set_id = urllib.unquote(escaped_id)
dform = forms.DDiffForm(g.db, set_id, request)
for diff in dform.delete_diffs:
diff_obj = savant.diffs.Diff(diff)
dform.set_obj.delete_diff(diff_obj)
message = Markup('Set updated: <strong>%s %s: %s</strong>' %
(
dform.set_obj.info.action,
dform.set_obj.info.system,
dform.set_obj.info.name
))
flash(message)
return redirect(url_for('set_edit', escaped_id=escaped_id))
@app.route('/set/delete', methods=['POST'])
def set_delete():
escaped_id=request.form['escaped_id']
set_id = urllib.unquote(escaped_id)
set_obj = savant.sets.Set(g.db, set_id)
set_obj.delete()
message = Markup('Set deleted: <strong>%s %s: %s</strong>' %
(
set_obj.info.action,
set_obj.info.system,
set_obj.info.name
))
flash(message)
return redirect(url_for('sets'))
if __name__ == '__main__':
arg_parser = argparse.ArgumentParser(
description='App to assign diffs to sets')
arg_parser.add_argument(
'-i', '--inference-db',
required=True,
help='The path to the directory containing the inference ZODB \
files')
args = arg_parser.parse_args()
app.run()