This repository has been archived by the owner on Jul 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
web-ui.py
58 lines (47 loc) · 1.72 KB
/
web-ui.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
from flask import Flask, request, Response, render_template
import sdv
import StringIO
import urllib2
import json
app = Flask(__name__, static_folder='public', static_url_path='')
@app.route('/validate', methods=['POST', 'GET'])
def validate_ws():
post_data = request.get_data()
url = request.args.get('url')
if url:
results = validate_url(url)
elif len(post_data) > 0:
results = validate_xml(post_data)
else:
results = {"result": "error", "message": "could not find xml"}
return Response(json.dumps(results),mimetype="application/json")
@app.route('/', methods=['POST', 'GET'])
def root():
if request.method == 'POST':
url = request.form['url']
xml = request.form['xml']
if url and len(url) > 0:
results = validate_url(url)
elif xml and len(xml) > 0:
results = validate_xml(xml)
else:
results = {"result": "error", "message": "could not find xml"}
return render_template('index.html', results=results, url=url, xml=xml, json=json.dumps(results))
else:
return render_template('index.html', results=None)
def validate_xml(data):
try:
return {
"xml": sdv.validate_xml(StringIO.StringIO(data)).as_dict(),
"best_practices": sdv.validate_best_practices(StringIO.StringIO(data)).as_dict(),
"result": "validated"
}
except sdv.errors.ValidationError as e:
return {"result": "error", "message": str(e)}
def validate_url(url):
try:
return validate_xml(urllib2.urlopen(url).read())
except ValueError:
return {"result": "error", "message": "could not open url"}
if __name__ == "__main__":
app.run('localhost', 5000, True)