Browse Source

Added changes made since the last known push

master
Graham Northup 2 years ago
parent
commit
9ef25ffee0
  1. 91
      model.py
  2. 6
      pluto.py
  3. 10
      static/site.js
  4. 7
      static/style.css
  5. 23
      templates/api_hit.txt
  6. 4
      templates/debuglogs.html
  7. 33
      templates/macros.html
  8. 4
      util.py

91
model.py

@ -129,7 +129,7 @@ class Log(DBObject):
class DebugLog(DBObject):
__TABLE__ = 'debuglog'
__FIELDS__ = ('time', 'path', 'headers', 'data', 'hook', 'cond', 'act', 'success', 'message')
__FIELDS__ = ('time', 'path', 'headers', 'data', 'value', 'hook', 'cond', 'act', 'success', 'message')
@classmethod
def most_recent(cls, n=None):
@ -143,27 +143,27 @@ class Hook(DBObject):
'debugged': 0,
}
def trigger(self, path, headers, data, response):
def trigger(self, path, headers, data, values, response):
if self.disabled:
return False
conditions = Condition.for_hook(self)
actions = Action.for_hook(self)
for condition in conditions:
result = condition.test_select(path, headers, data, response)
result, msg = condition.test_select(path, headers, data, values, response)
if self.debugged:
DebugLog.create(time.time(), path, header_dumps(headers), jdumps(data), self.rowid, condition.rowid, None, result, None)
DebugLog.create(time.time(), path, header_dumps(headers), jdumps(data), jdumps(values), self.rowid, condition.rowid, None, result, msg)
if not result:
break
else:
for act in actions:
result = act.actuate(path, headers, data, response)
result = act.actuate(path, headers, data, values, response)
if self.debugged:
DebugLog.create(time.time(), path, header_dumps(headers), jdumps(data), self.rowid, None, act.rowid, None, result)
DebugLog.create(time.time(), path, header_dumps(headers), jdumps(data), jdumps(values), self.rowid, None, act.rowid, None, result)
if self.debugged:
DebugLog.create(time.time(), path, header_dumps(headers), jdumps(data), self.rowid, None, None, True, None)
DebugLog.create(time.time(), path, header_dumps(headers), jdumps(data), jdumps(values), self.rowid, None, None, True, None)
return True
if self.debugged:
DebugLog.create(time.time(), path, header_dumps(headers), jdumps(data), self.rowid, None, None, False, None)
DebugLog.create(time.time(), path, header_dumps(headers), jdumps(data), jdumps(values), self.rowid, None, None, False, None)
return False
class Condition(DBObject):
@ -177,16 +177,17 @@ class Condition(DBObject):
def get_hook(self):
return Hook.get_one(rowid=self.hook)
def select(self, path, headers, data, response):
return getattr(self, 'select_' + self.selector, self.no_select)(path, headers, data, response)
def select(self, path, headers, data, values, response):
return getattr(self, 'select_' + self.selector, self.no_select)(path, headers, data, values, response)
def no_select(self, path, headers, data, response):
def no_select(self, path, headers, data, values, response):
print 'No selector found for', self.selector
return None
def select_header(self, path, headers, data, response):
def select_header(self, path, headers, data, values, response):
return headers.get(self.s1, '')
def select_JSON(self, path, headers, data, response):
def select_JSON(self, path, headers, data, values, response):
if not isinstance(data, dict):
return False
cur = data
@ -196,35 +197,41 @@ class Condition(DBObject):
return False
return str(cur)
def select_path(self, path, headers, data, response):
def select_path(self, path, headers, data, values, response):
return path
def select_value(self, path, headers, data, values, response):
print values
print self.s1
print values.get(self.s1, '')
return values.get(self.s1, '')
def test_value(self, val):
try:
result = getattr(self, 'test_' + self.test, self.no_test)(val)
except (ValueError, TypeError):
result = False
except (ValueError, TypeError) as e:
result = (False, "Error: " + str(e))
if self.invert:
result = not result
result = (not result[0], result[1])
return result
def no_test(self, val):
return False
return False, "No valid test by that name"
def test_equal(self, val):
return str(val) == self.t1
return str(val) == self.t1, "Compare: %r == %r" % (val, self.t1)
def test_inrange(self, val):
return float(self.t1) <= float(val) <= float(self.t2)
return float(self.t1) <= float(val) <= float(self.t2), "Compare %r <= %r <= %r" % (float(self.t1), float(val), float(self.t2))
def test_truthy(self, val):
return bool(val)
return bool(val), "Test: %r" %(val,)
def test_contains(self, val):
return self.t1 in val
return self.t1 in val, "Compare: %r in %r" % (self.t1, val)
def test_select(self, path, headers, data, response):
return self.test_value(self.select(path, headers, data, response))
def test_select(self, path, headers, data, values, response):
return self.test_value(self.select(path, headers, data, values, response))
class Action(DBObject):
__TABLE__ = 'actions'
@ -241,17 +248,18 @@ class Action(DBObject):
def get_hook(self):
return Hook.get_one(rowid=self.hook)
def actuate(self, path, headers, data, response):
def actuate(self, path, headers, data, values, response):
try:
return getattr(self, 'act_' + self.action, self.no_act)(path, headers, data, response)
return getattr(self, 'act_' + self.action, self.no_act)(path, headers, data, values, response)
except (ValueError, TypeError):
pass
def no_act(self, path, headers, data, response):
def no_act(self, path, headers, data, values, response):
return 'INTERNAL ERROR: ACTION NOT FOUND'
def act_post(self, path, headers, data, response):
args = {'path': path, 'headers': headers, 'data': data}
def act_post(self, path, headers, data, values, response):
args = {'path': path, 'headers': headers, 'data': data, 'values': values}
args['response'] = {'data': response.get_data(), 'headers': response.headers}
url = render_template_string(self.a1, **args)
postdata = render_template_string(self.a2, **args)
headers = json.loads(render_template_string(self.a3, **args))
@ -264,8 +272,9 @@ class Action(DBObject):
print 'Complete, got', repr(out)
return out
def act_gitlab(self, path, headers, data, response):
args = {'path': path, 'headers': headers, 'data': data}
def act_gitlab(self, path, headers, data, values, response):
args = {'path': path, 'headers': headers, 'data': data, 'values': values}
args['response'] = {'data': response.get_data(), 'headers': response.headers}
url = self.GITLAB_API + render_template_string(self.a1, **args)
params = json.loads(render_template_string(self.a2, **args))
headers = json.loads(render_template_string(self.a3, **args))
@ -280,8 +289,9 @@ class Action(DBObject):
print 'Complete, got', repr(out)
return out
def act_system(self, path, headers, data, response):
args = {'path': path, 'headers': headers, 'data': data}
def act_system(self, path, headers, data, values, response):
args = {'path': path, 'headers': headers, 'data': data, 'values': values}
args['response'] = {'data': response.get_data(), 'headers': response.headers}
cmd = render_template_string(self.a1, **args)
if not self.a2:
proc = subprocess.Popen(cmd, shell=True)
@ -292,8 +302,9 @@ class Action(DBObject):
except subprocess.CalledProcessError as e:
return e.output
def act_udp(self, path, headers, data, response):
args = {'path': path, 'headers': headers, 'data': data}
def act_udp(self, path, headers, data, values, response):
args = {'path': path, 'headers': headers, 'data': data, 'values': values}
args['response'] = {'data': response.get_data(), 'headers': response.headers}
dest = render_template_string(self.a1, **args)
packet = render_template_string(self.a2, **args)
encoding = render_template_string(self.a3, **args)
@ -334,8 +345,9 @@ class Action(DBObject):
pass
return 'no good address family found'
def act_tcp(self, path, headers, data, response):
args = {'path': path, 'headers': headers, 'data': data}
def act_tcp(self, path, headers, data, values, response):
args = {'path': path, 'headers': headers, 'data': data, 'values': values}
args['response'] = {'data': response.get_data(), 'headers': response.headers}
dest = render_template_string(self.a1, **args)
packet = render_template_string(self.a2, **args)
encoding = render_template_string(self.a3, **args)
@ -382,8 +394,9 @@ class Action(DBObject):
pass
return 'no good address family found'
def act_set_response(self, path, headers, data, response):
args = {'path': path, 'headers': headers, 'data': data}
def act_set_response(self, path, headers, data, values, response):
args = {'path': path, 'headers': headers, 'data': data, 'values': values}
args['response'] = {'data': response.get_data(), 'headers': response.headers}
content = render_template_string(self.a1, **args)
content_type = render_template_string(self.a2, **args)
response.set_data(content)

6
pluto.py

@ -30,8 +30,8 @@ def root():
@app.route('/hook', methods=['GET', 'POST'])
def hook():
response = make_response(render_template('api_hit.txt'))
response.headers['Content-type'] = 'text/json'
triggered = filter(lambda hook: hook.trigger(request.path, request.headers, jloads(request.data), response), Hook.all())
response.headers['Content-type'] = 'text/plain'
triggered = filter(lambda hook: hook.trigger(request.path, request.headers, jloads(request.data), dict(request.values.items()), response), Hook.all())
Log.create(time.time(), request.path, header_dumps(request.headers), request.data, ','.join(str(hook.rowid) for hook in triggered))
return response
@ -219,4 +219,4 @@ def act_delete(actid):
return render_template('act_delete.html', act=act)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
app.run(host='0.0.0.0', port=8880)

10
static/site.js

@ -11,6 +11,7 @@ const SELECTOR_SCHEMA = {
'header': ['Header name'],
'JSON': ['Object path'],
'path': [],
'value': ['Parameter'],
};
const TEST_SCHEMA = {
@ -20,7 +21,7 @@ const TEST_SCHEMA = {
'contains': ['String value'],
};
function reg_action_select(select, laba1, laba2, laba3) {
function reg_action_select(select, laba1, laba2, laba3, current) {
select.addEventListener("change", function() {
var schema = ACTION_SCHEMA[this.value];
if(!schema) {
@ -33,10 +34,11 @@ function reg_action_select(select, laba1, laba2, laba3) {
laba3.textContent = (schema.length > 2 ? schema[2] : "Unused");
}
});
if(current != undefined) select.value = current;
select.dispatchEvent(new Event("change"));
}
function reg_selector_select(select, labs1, labs2, labs3) {
function reg_selector_select(select, labs1, labs2, labs3, current) {
select.addEventListener("change", function() {
var schema = SELECTOR_SCHEMA[this.value];
if(!schema) {
@ -49,10 +51,11 @@ function reg_selector_select(select, labs1, labs2, labs3) {
labs3.textContent = (schema.length > 2 ? schema[2] : "Unused");
}
});
if(current != undefined) select.value = current;
select.dispatchEvent(new Event("change"));
}
function reg_test_select(select, labt1, labt2, labt3) {
function reg_test_select(select, labt1, labt2, labt3, current) {
select.addEventListener("change", function() {
var schema = TEST_SCHEMA[this.value];
if(!schema) {
@ -65,5 +68,6 @@ function reg_test_select(select, labt1, labt2, labt3) {
labt3.textContent = (schema.length > 2 ? schema[2] : "Unused");
}
});
if(current != undefined) select.value = current;
select.dispatchEvent(new Event("change"));
}

7
static/style.css

@ -39,7 +39,7 @@ table td, table th {
font-size: 75%;
}
#logs .headers, #logs .data {
#logs .headers, #logs .data, #logs .values {
white-space: pre-wrap;
font-family: monospace;
background-color: #eee;
@ -193,3 +193,8 @@ div.error {
.flex-center > div {
margin: 0 50px;
}
textarea.input {
width: 50%;
height: 25em;
}

23
templates/api_hit.txt

@ -1,22 +1 @@
{
"version": "1.0",
"sessionAttributes": {},
"response": {
"outputSpeech": {
"type": "PlainText",
"text": "Welcome to the Alexa Skills Kit sample, Please tell me your favorite color by saying, my favorite color is red"
},
"card": {
"type": "Simple",
"title": "SessionSpeechlet - Welcome",
"content": "SessionSpeechlet - Welcome to the Alexa Skills Kit sample, Please tell me your favorite color by saying, my favorite color is red"
},
"reprompt": {
"outputSpeech": {
"type": "PlainText",
"text": "Please tell me your favorite color by saying, my favorite color is red"
}
},
"shouldEndSession": false
}
}
Hook actuated.

4
templates/debuglogs.html

@ -11,6 +11,7 @@
<th>Path</th>
<th>Headers</th>
<th>Data</th>
<th>Values</th>
<th>Hook</th>
<th>Actor</th>
<th>Success</th>
@ -22,6 +23,7 @@
<td class="path">{{ log.path }}</td>
<td class="headers">{{ log.headers | hloads | pprint(True) }}</td>
<td class="data">{{ log.data | jloads | pprint(True) }}</td>
<td class="values">{{ log.value | jloads | pprint(True) }}</td>
<td class="hook">{{ macros.hook_id(log.hook) }}</td>
<td class="actor">
{% if log.cond %}
@ -37,7 +39,7 @@
</tr>
{% else %}
<tr>
<td colspan="8" class="nodata">No data.</td>
<td colspan="9" class="nodata">No data.</td>
</tr>
{% endfor %}
</table>

33
templates/macros.html

@ -8,6 +8,10 @@
<p><span id="{{ id }}">{{desc}}</span>: <input type="text" name="{{ name }}" value="{{ value }}"/></p>
{%- endmacro %}
{% macro textarea_in_id(desc, id, name, value) -%}
<p><span id="{{ id }}">{{desc}}</span>: <textarea class="input" name="{{ name }}">{{ value }}</textarea></p>
{%- endmacro %}
{% macro select_in(desc, name, value, displays, options) -%}
<p>{{desc}}:
<select name="{{ name }}" value="{{ value }}">
@ -66,27 +70,29 @@
{%- endmacro %}
{% macro cond_params(cond) -%}
{{ select_in_id('Selector', 'select_selector', 'selector', cond.selector, ['header', 'JSON', 'path'], ['header', 'JSON', 'path']) }}
{{ text_in_id('Selector parameter 1', 'label_s1', 's1', cond.s1) }}
{{ text_in_id('Selector parameter 2', 'label_s2', 's2', cond.s2) }}
{{ text_in_id('Selector parameter 3', 'label_s3', 's3', cond.s3) }}
{{ select_in_id('Selector', 'select_selector', 'selector', cond.selector, ['header', 'JSON', 'path', 'value'], ['header', 'JSON', 'path', 'value']) }}
{{ textarea_in_id('Selector parameter 1', 'label_s1', 's1', cond.s1) }}
{{ textarea_in_id('Selector parameter 2', 'label_s2', 's2', cond.s2) }}
{{ textarea_in_id('Selector parameter 3', 'label_s3', 's3', cond.s3) }}
{{ select_in_id('Test', 'select_test', 'test', cond.test, ['equal', 'inrange', 'truthy', 'contains'], ['equal', 'inrange', 'truthy', 'contains']) }}
{{ text_in_id('Test parameter 1', 'label_t1', 't1', cond.t1) }}
{{ text_in_id('Test parameter 2', 'label_t2', 't2', cond.t2) }}
{{ text_in_id('Test parameter 3', 'label_t3', 't3', cond.t3) }}
{{ textarea_in_id('Test parameter 1', 'label_t1', 't1', cond.t1) }}
{{ textarea_in_id('Test parameter 2', 'label_t2', 't2', cond.t2) }}
{{ textarea_in_id('Test parameter 3', 'label_t3', 't3', cond.t3) }}
{{ checkbox_in('Invert this condition', 'invert', cond.invert) }}
<script type="text/javascript">
reg_selector_select(
document.querySelector("#select_selector"),
document.querySelector("#label_s1"),
document.querySelector("#label_s2"),
document.querySelector("#label_s3")
document.querySelector("#label_s3"),
"{{cond.selector}}"
);
reg_test_select(
document.querySelector("#select_test"),
document.querySelector("#label_t1"),
document.querySelector("#label_t2"),
document.querySelector("#label_t3")
document.querySelector("#label_t3"),
"{{cond.test}}"
);
</script>
{%- endmacro %}
@ -105,15 +111,16 @@
{% macro act_params(act) -%}
{{ select_in_id('Action', 'select_action', 'action', act.action, ['post', 'gitlab', 'system', 'udp', 'tcp', 'set_response'], ['post', 'gitlab', 'system', 'udp', 'tcp', 'set_response']) }}
{{ text_in_id('Action parameter 1', 'label_a1', 'a1', act.a1) }}
{{ text_in_id('Action parameter 2', 'label_a2', 'a2', act.a2) }}
{{ text_in_id('Action parameter 3', 'label_a3', 'a3', act.a3) }}
{{ textarea_in_id('Action parameter 1', 'label_a1', 'a1', act.a1) }}
{{ textarea_in_id('Action parameter 2', 'label_a2', 'a2', act.a2) }}
{{ textarea_in_id('Action parameter 3', 'label_a3', 'a3', act.a3) }}
<script type="text/javascript">
reg_action_select(
document.querySelector("#select_action"),
document.querySelector("#label_a1"),
document.querySelector("#label_a2"),
document.querySelector("#label_a3")
document.querySelector("#label_a3"),
"{{act.action}}"
);
</script>
{%- endmacro %}

4
util.py

@ -14,13 +14,13 @@ def header_loads(hdr):
def jloads(s):
try:
return json.loads(s)
except ValueError:
except (ValueError, TypeError):
return s
def jdumps(obj):
try:
return json.dumps(obj)
except ValueError:
except (ValueError, TypeError):
return obj
def checkbox(request, name):

Loading…
Cancel
Save