-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
119 lines (96 loc) · 3.9 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
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
from flask import Flask, request, jsonify, current_app, render_template
# Custom
from functools import wraps
import traceback
import sys
from yaqluator import yaqluator
from utils import files
app = Flask(__name__, static_url_path='/static')
def jsonp(func):
"""Wraps JSONified output for JSONP requests."""
@wraps(func)
def decorated_function(*args, **kwargs):
callback = request.args.get('callback', False)
if callback:
data = str(func(*args, **kwargs).data)
content = str(callback) + '(' + data + ')'
mimetype = 'application/javascript'
return current_app.response_class(content, mimetype=mimetype)
else:
return func(*args, **kwargs)
return decorated_function
@app.route("/api/evaluate/", methods=['POST'])
@jsonp
def handle_evaluate():
data = request.json or request.form
if data is None:
return json_error_response("yaml and yaql_expression are missing in request body")
if not "yaql_expression" in data:
return json_error_response("yaql_expression is missing")
if not "yaml" in data:
return json_error_response("yaml is missing")
if data['st2_host'] and data['st2_key'] and data['st2_execution']:
from st2client.client import Client
import json
try:
client = Client(api_url=data['st2_host'] + '/api/v1', api_key=data['st2_key'])
execution = client.liveactions.get_by_id(data['st2_execution'])
payload = execution.result
except Exception as e:
return json_error_response(str(e))
else:
payload = data['yaml']
return invoke(yaqluator.evaluate, {"expression": data["yaql_expression"],
"data": payload
}
)
@app.route("/api/autoComplete/", methods=['POST'])
@jsonp
def handle_auto_complete():
data = request.json or request.form
if data is None:
return json_error_response("yaml and yaql_expression are missing in request body")
if not "yaql_expression" in data:
return json_error_response("yaql_expression is missing")
if not "yaml" in data:
return json_error_response("yaml is missing")
legacy = str(data.get("legacy", False)).lower() == "true"
return invoke(yaqluator.auto_complete, {"yaql_expression": data["yaql_expression"], "yaml_string": data["yaml"], "legacy":legacy})
@app.route("/examples/", methods=["GET"])
@jsonp
def list_examples():
return invoke(files.list_examples, value_key="examples")
@app.route("/api/examples/<example_name>", methods=["GET"])
@jsonp
def get_example(example_name):
# if "exampleName" not in request.args:
# return json_error_response("example name is missing")
return invoke(files.get_example, {"example_name": example_name})
def invoke(function, params=None, value_key="value"):
try:
params = params or {}
response = function(**params)
ret = {"statusCode": 1, value_key: response}
except Exception as e:
#print format_exception(e)
ret = error_response(str(e))
return jsonify(**ret)
def json_error_response(message):
return jsonify({"statusCode": -1, "error": message})
def error_response(message):
return {"statusCode": -1, "error": message}
def format_exception(e):
exception_list = traceback.format_stack()
exception_list = exception_list[:-2]
exception_list.extend(traceback.format_tb(sys.exc_info()[2]))
exception_list.extend(traceback.format_exception_only(sys.exc_info()[0], sys.exc_info()[1]))
exception_str = "Traceback (most recent call last):\n"
exception_str += "".join(exception_list)
# Removing the last \n
exception_str = exception_str[:-1]
return exception_str
@app.route('/')
def doit(name=None):
return render_template('index.html', name=name)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')