From 9ef25ffee01ec541098498596e7bec040d3627d5 Mon Sep 17 00:00:00 2001 From: Graham Northup Date: Thu, 23 Jan 2020 17:03:12 -0500 Subject: [PATCH] Added changes made since the last known push --- model.py | 91 +++++++++++++++++++++++----------------- pluto.py | 6 +-- static/site.js | 10 +++-- static/style.css | 7 +++- templates/api_hit.txt | 23 +--------- templates/debuglogs.html | 4 +- templates/macros.html | 33 +++++++++------ util.py | 4 +- 8 files changed, 94 insertions(+), 84 deletions(-) diff --git a/model.py b/model.py index 8d06b6f..b928a73 100644 --- a/model.py +++ b/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) diff --git a/pluto.py b/pluto.py index 6765dce..4b584a4 100644 --- a/pluto.py +++ b/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) diff --git a/static/site.js b/static/site.js index 1837ea8..5b44dcc 100644 --- a/static/site.js +++ b/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")); } diff --git a/static/style.css b/static/style.css index 68c583e..7ed1230 100644 --- a/static/style.css +++ b/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; +} diff --git a/templates/api_hit.txt b/templates/api_hit.txt index 87661ac..1e9ab07 100644 --- a/templates/api_hit.txt +++ b/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. diff --git a/templates/debuglogs.html b/templates/debuglogs.html index 687f8a9..ca92d4d 100644 --- a/templates/debuglogs.html +++ b/templates/debuglogs.html @@ -11,6 +11,7 @@ Path Headers Data + Values Hook Actor Success @@ -22,6 +23,7 @@ {{ log.path }} {{ log.headers | hloads | pprint(True) }} {{ log.data | jloads | pprint(True) }} + {{ log.value | jloads | pprint(True) }} {{ macros.hook_id(log.hook) }} {% if log.cond %} @@ -37,7 +39,7 @@ {% else %} - No data. + No data. {% endfor %} diff --git a/templates/macros.html b/templates/macros.html index adff8b6..5afb21c 100644 --- a/templates/macros.html +++ b/templates/macros.html @@ -8,6 +8,10 @@

{{desc}}:

{%- endmacro %} +{% macro textarea_in_id(desc, id, name, value) -%} +

{{desc}}:

+{%- endmacro %} + {% macro select_in(desc, name, value, displays, options) -%}

{{desc}}: