Browse Source

Added changes made since the last known push

Graham Northup 1 year ago
parent
commit
9ef25ffee0
8 changed files with 94 additions and 84 deletions
  1. 52
    39
      model.py
  2. 3
    3
      pluto.py
  3. 7
    3
      static/site.js
  4. 6
    1
      static/style.css
  5. 1
    22
      templates/api_hit.txt
  6. 3
    1
      templates/debuglogs.html
  7. 20
    13
      templates/macros.html
  8. 2
    2
      util.py

+ 52
- 39
model.py View File

@@ -129,7 +129,7 @@ class Log(DBObject):
129 129
 
130 130
 class DebugLog(DBObject):
131 131
     __TABLE__ = 'debuglog'
132
-    __FIELDS__ = ('time', 'path', 'headers', 'data', 'hook', 'cond', 'act', 'success', 'message')
132
+    __FIELDS__ = ('time', 'path', 'headers', 'data', 'value', 'hook', 'cond', 'act', 'success', 'message')
133 133
 
134 134
     @classmethod
135 135
     def most_recent(cls, n=None):
@@ -143,27 +143,27 @@ class Hook(DBObject):
143 143
         'debugged': 0,
144 144
     }
145 145
 
146
-    def trigger(self, path, headers, data, response):
146
+    def trigger(self, path, headers, data, values, response):
147 147
         if self.disabled:
148 148
             return False
149 149
         conditions = Condition.for_hook(self)
150 150
         actions = Action.for_hook(self)
151 151
         for condition in conditions:
152
-            result = condition.test_select(path, headers, data, response)
152
+            result, msg = condition.test_select(path, headers, data, values, response)
153 153
             if self.debugged:
154
-                DebugLog.create(time.time(), path, header_dumps(headers), jdumps(data), self.rowid, condition.rowid, None, result, None)
154
+                DebugLog.create(time.time(), path, header_dumps(headers), jdumps(data), jdumps(values), self.rowid, condition.rowid, None, result, msg)
155 155
             if not result:
156 156
                 break
157 157
         else:
158 158
             for act in actions:
159
-                result = act.actuate(path, headers, data, response)
159
+                result = act.actuate(path, headers, data, values, response)
160 160
                 if self.debugged:
161
-                    DebugLog.create(time.time(), path, header_dumps(headers), jdumps(data), self.rowid, None, act.rowid, None, result)
161
+                    DebugLog.create(time.time(), path, header_dumps(headers), jdumps(data), jdumps(values), self.rowid, None, act.rowid, None, result)
162 162
             if self.debugged:
163
-                DebugLog.create(time.time(), path, header_dumps(headers), jdumps(data), self.rowid, None, None, True, None)
163
+                DebugLog.create(time.time(), path, header_dumps(headers), jdumps(data), jdumps(values), self.rowid, None, None, True, None)
164 164
             return True
165 165
         if self.debugged:
166
-            DebugLog.create(time.time(), path, header_dumps(headers), jdumps(data), self.rowid, None, None, False, None)
166
+            DebugLog.create(time.time(), path, header_dumps(headers), jdumps(data), jdumps(values), self.rowid, None, None, False, None)
167 167
         return False
168 168
     
169 169
 class Condition(DBObject):
@@ -177,16 +177,17 @@ class Condition(DBObject):
177 177
     def get_hook(self):
178 178
         return Hook.get_one(rowid=self.hook)
179 179
 
180
-    def select(self, path, headers, data, response):
181
-        return getattr(self, 'select_' + self.selector, self.no_select)(path, headers, data, response)
180
+    def select(self, path, headers, data, values, response):
181
+        return getattr(self, 'select_' + self.selector, self.no_select)(path, headers, data, values, response)
182 182
 
183
-    def no_select(self, path, headers, data, response):
183
+    def no_select(self, path, headers, data, values, response):
184
+        print 'No selector found for', self.selector
184 185
         return None
185 186
 
186
-    def select_header(self, path, headers, data, response):
187
+    def select_header(self, path, headers, data, values, response):
187 188
         return headers.get(self.s1, '')
188 189
 
189
-    def select_JSON(self, path, headers, data, response):
190
+    def select_JSON(self, path, headers, data, values, response):
190 191
         if not isinstance(data, dict):
191 192
             return False
192 193
         cur = data
@@ -196,35 +197,41 @@ class Condition(DBObject):
196 197
                 return False
197 198
         return str(cur)
198 199
 
199
-    def select_path(self, path, headers, data, response):
200
+    def select_path(self, path, headers, data, values, response):
200 201
         return path
201 202
 
203
+    def select_value(self, path, headers, data, values, response):
204
+        print values
205
+        print self.s1
206
+        print values.get(self.s1, '')
207
+        return values.get(self.s1, '')
208
+
202 209
     def test_value(self, val):
203 210
         try:
204 211
             result = getattr(self, 'test_' + self.test, self.no_test)(val)
205
-        except (ValueError, TypeError):
206
-            result = False
212
+        except (ValueError, TypeError) as e:
213
+            result = (False, "Error: " + str(e))
207 214
         if self.invert:
208
-            result = not result
215
+            result = (not result[0], result[1])
209 216
         return result
210 217
 
211 218
     def no_test(self, val):
212
-        return False
219
+        return False, "No valid test by that name"
213 220
 
214 221
     def test_equal(self, val):
215
-        return str(val) == self.t1
222
+        return str(val) == self.t1, "Compare: %r == %r" % (val, self.t1)
216 223
 
217 224
     def test_inrange(self, val):
218
-        return float(self.t1) <= float(val) <= float(self.t2)
225
+        return float(self.t1) <= float(val) <= float(self.t2), "Compare %r <= %r <= %r" % (float(self.t1), float(val), float(self.t2))
219 226
 
220 227
     def test_truthy(self, val):
221
-        return bool(val)
228
+        return bool(val), "Test: %r" %(val,)
222 229
 
223 230
     def test_contains(self, val):
224
-        return self.t1 in val
231
+        return self.t1 in val, "Compare: %r in %r" % (self.t1, val)
225 232
 
226
-    def test_select(self, path, headers, data, response):
227
-        return self.test_value(self.select(path, headers, data, response))
233
+    def test_select(self, path, headers, data, values, response):
234
+        return self.test_value(self.select(path, headers, data, values, response))
228 235
 
229 236
 class Action(DBObject):
230 237
     __TABLE__ = 'actions'
@@ -241,17 +248,18 @@ class Action(DBObject):
241 248
     def get_hook(self):
242 249
         return Hook.get_one(rowid=self.hook)
243 250
 
244
-    def actuate(self, path, headers, data, response):
251
+    def actuate(self, path, headers, data, values, response):
245 252
         try:
246
-            return getattr(self, 'act_' + self.action, self.no_act)(path, headers, data, response)
253
+            return getattr(self, 'act_' + self.action, self.no_act)(path, headers, data, values, response)
247 254
         except (ValueError, TypeError):
248 255
             pass
249 256
 
250
-    def no_act(self, path, headers, data, response):
257
+    def no_act(self, path, headers, data, values, response):
251 258
         return 'INTERNAL ERROR: ACTION NOT FOUND'
252 259
 
253
-    def act_post(self, path, headers, data, response):
254
-        args = {'path': path, 'headers': headers, 'data': data}
260
+    def act_post(self, path, headers, data, values, response):
261
+        args = {'path': path, 'headers': headers, 'data': data, 'values': values}
262
+        args['response'] = {'data': response.get_data(), 'headers': response.headers}
255 263
         url = render_template_string(self.a1, **args)
256 264
         postdata = render_template_string(self.a2, **args)
257 265
         headers = json.loads(render_template_string(self.a3, **args))
@@ -264,8 +272,9 @@ class Action(DBObject):
264 272
         print 'Complete, got', repr(out)
265 273
         return out
266 274
 
267
-    def act_gitlab(self, path, headers, data, response):
268
-        args = {'path': path, 'headers': headers, 'data': data}
275
+    def act_gitlab(self, path, headers, data, values, response):
276
+        args = {'path': path, 'headers': headers, 'data': data, 'values': values}
277
+        args['response'] = {'data': response.get_data(), 'headers': response.headers}
269 278
         url = self.GITLAB_API + render_template_string(self.a1, **args)
270 279
         params = json.loads(render_template_string(self.a2, **args))
271 280
         headers = json.loads(render_template_string(self.a3, **args))
@@ -280,8 +289,9 @@ class Action(DBObject):
280 289
         print 'Complete, got', repr(out)
281 290
         return out
282 291
 
283
-    def act_system(self, path, headers, data, response):
284
-        args = {'path': path, 'headers': headers, 'data': data}
292
+    def act_system(self, path, headers, data, values, response):
293
+        args = {'path': path, 'headers': headers, 'data': data, 'values': values}
294
+        args['response'] = {'data': response.get_data(), 'headers': response.headers}
285 295
         cmd = render_template_string(self.a1, **args)
286 296
         if not self.a2:
287 297
 			proc = subprocess.Popen(cmd, shell=True)
@@ -292,8 +302,9 @@ class Action(DBObject):
292 302
 			except subprocess.CalledProcessError as e:
293 303
 				return e.output
294 304
 
295
-    def act_udp(self, path, headers, data, response):
296
-        args = {'path': path, 'headers': headers, 'data': data}
305
+    def act_udp(self, path, headers, data, values, response):
306
+        args = {'path': path, 'headers': headers, 'data': data, 'values': values}
307
+        args['response'] = {'data': response.get_data(), 'headers': response.headers}
297 308
         dest = render_template_string(self.a1, **args)
298 309
         packet = render_template_string(self.a2, **args)
299 310
         encoding = render_template_string(self.a3, **args)
@@ -334,8 +345,9 @@ class Action(DBObject):
334 345
                     pass
335 346
         return 'no good address family found'
336 347
 
337
-    def act_tcp(self, path, headers, data, response):
338
-        args = {'path': path, 'headers': headers, 'data': data}
348
+    def act_tcp(self, path, headers, data, values, response):
349
+        args = {'path': path, 'headers': headers, 'data': data, 'values': values}
350
+        args['response'] = {'data': response.get_data(), 'headers': response.headers}
339 351
         dest = render_template_string(self.a1, **args)
340 352
         packet = render_template_string(self.a2, **args)
341 353
         encoding = render_template_string(self.a3, **args)
@@ -382,8 +394,9 @@ class Action(DBObject):
382 394
                     pass
383 395
         return 'no good address family found'
384 396
 
385
-    def act_set_response(self, path, headers, data, response):
386
-        args = {'path': path, 'headers': headers, 'data': data}
397
+    def act_set_response(self, path, headers, data, values, response):
398
+        args = {'path': path, 'headers': headers, 'data': data, 'values': values}
399
+        args['response'] = {'data': response.get_data(), 'headers': response.headers}
387 400
         content = render_template_string(self.a1, **args)
388 401
         content_type = render_template_string(self.a2, **args)
389 402
         response.set_data(content)

+ 3
- 3
pluto.py View File

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

+ 7
- 3
static/site.js View File

@@ -11,6 +11,7 @@ const SELECTOR_SCHEMA = {
11 11
     'header': ['Header name'],
12 12
     'JSON': ['Object path'],
13 13
     'path': [],
14
+	'value': ['Parameter'],
14 15
 };
15 16
 
16 17
 const TEST_SCHEMA = {
@@ -20,7 +21,7 @@ const TEST_SCHEMA = {
20 21
     'contains': ['String value'],
21 22
 };
22 23
 
23
-function reg_action_select(select, laba1, laba2, laba3) {
24
+function reg_action_select(select, laba1, laba2, laba3, current) {
24 25
     select.addEventListener("change", function() {
25 26
         var schema = ACTION_SCHEMA[this.value];
26 27
         if(!schema) {
@@ -33,10 +34,11 @@ function reg_action_select(select, laba1, laba2, laba3) {
33 34
             laba3.textContent = (schema.length > 2 ? schema[2] : "Unused");
34 35
         }
35 36
     });
37
+	if(current != undefined) select.value = current;
36 38
     select.dispatchEvent(new Event("change"));
37 39
 }
38 40
 
39
-function reg_selector_select(select, labs1, labs2, labs3) {
41
+function reg_selector_select(select, labs1, labs2, labs3, current) {
40 42
     select.addEventListener("change", function() {
41 43
         var schema = SELECTOR_SCHEMA[this.value];
42 44
         if(!schema) {
@@ -49,10 +51,11 @@ function reg_selector_select(select, labs1, labs2, labs3) {
49 51
             labs3.textContent = (schema.length > 2 ? schema[2] : "Unused");
50 52
         }
51 53
     });
54
+	if(current != undefined) select.value = current;
52 55
     select.dispatchEvent(new Event("change"));
53 56
 }
54 57
 
55
-function reg_test_select(select, labt1, labt2, labt3) {
58
+function reg_test_select(select, labt1, labt2, labt3, current) {
56 59
     select.addEventListener("change", function() {
57 60
         var schema = TEST_SCHEMA[this.value];
58 61
         if(!schema) {
@@ -65,5 +68,6 @@ function reg_test_select(select, labt1, labt2, labt3) {
65 68
             labt3.textContent = (schema.length > 2 ? schema[2] : "Unused");
66 69
         }
67 70
     });
71
+	if(current != undefined) select.value = current;
68 72
     select.dispatchEvent(new Event("change"));
69 73
 }

+ 6
- 1
static/style.css View File

@@ -39,7 +39,7 @@ table td, table th {
39 39
     font-size: 75%;
40 40
 }
41 41
 
42
-#logs .headers, #logs .data {
42
+#logs .headers, #logs .data, #logs .values {
43 43
     white-space: pre-wrap;
44 44
     font-family: monospace;
45 45
     background-color: #eee;
@@ -193,3 +193,8 @@ div.error {
193 193
 .flex-center > div {
194 194
     margin: 0 50px;
195 195
 }
196
+
197
+textarea.input {
198
+    width: 50%;
199
+    height: 25em;
200
+}

+ 1
- 22
templates/api_hit.txt View File

@@ -1,22 +1 @@
1
-{
2
-  "version": "1.0",
3
-  "sessionAttributes": {},
4
-  "response": {
5
-    "outputSpeech": {
6
-      "type": "PlainText",
7
-      "text": "Welcome to the Alexa Skills Kit sample, Please tell me your favorite color by saying, my favorite color is red"
8
-    },
9
-    "card": {
10
-      "type": "Simple",
11
-      "title": "SessionSpeechlet - Welcome",
12
-      "content": "SessionSpeechlet - Welcome to the Alexa Skills Kit sample, Please tell me your favorite color by saying, my favorite color is red"
13
-    },
14
-    "reprompt": {
15
-      "outputSpeech": {
16
-        "type": "PlainText",
17
-        "text": "Please tell me your favorite color by saying, my favorite color is red"
18
-      }
19
-    },
20
-    "shouldEndSession": false
21
-  }
22
-}
1
+Hook actuated.

+ 3
- 1
templates/debuglogs.html View File

@@ -11,6 +11,7 @@
11 11
             <th>Path</th>
12 12
             <th>Headers</th>
13 13
             <th>Data</th>
14
+			<th>Values</th>
14 15
             <th>Hook</th>
15 16
             <th>Actor</th>
16 17
             <th>Success</th>
@@ -22,6 +23,7 @@
22 23
             <td class="path">{{ log.path }}</td>
23 24
             <td class="headers">{{ log.headers | hloads | pprint(True) }}</td>
24 25
             <td class="data">{{ log.data | jloads | pprint(True) }}</td>
26
+			<td class="values">{{ log.value | jloads | pprint(True) }}</td>
25 27
             <td class="hook">{{ macros.hook_id(log.hook) }}</td>
26 28
             <td class="actor">
27 29
                 {% if log.cond %}
@@ -37,7 +39,7 @@
37 39
         </tr>
38 40
     {% else %}
39 41
         <tr>
40
-            <td colspan="8" class="nodata">No data.</td>
42
+            <td colspan="9" class="nodata">No data.</td>
41 43
         </tr>
42 44
     {% endfor %}
43 45
     </table>

+ 20
- 13
templates/macros.html View File

@@ -8,6 +8,10 @@
8 8
     <p><span id="{{ id }}">{{desc}}</span>: <input type="text" name="{{ name }}" value="{{ value }}"/></p>
9 9
 {%- endmacro %}
10 10
 
11
+{% macro textarea_in_id(desc, id, name, value) -%}
12
+    <p><span id="{{ id }}">{{desc}}</span>: <textarea class="input" name="{{ name }}">{{ value }}</textarea></p>
13
+{%- endmacro %}
14
+
11 15
 {% macro select_in(desc, name, value, displays, options) -%}
12 16
     <p>{{desc}}:
13 17
         <select name="{{ name }}" value="{{ value }}">
@@ -66,27 +70,29 @@
66 70
 {%- endmacro %}
67 71
 
68 72
 {% macro cond_params(cond) -%}
69
-    {{ select_in_id('Selector', 'select_selector', 'selector', cond.selector, ['header', 'JSON', 'path'], ['header', 'JSON', 'path']) }}
70
-    {{ text_in_id('Selector parameter 1', 'label_s1', 's1', cond.s1) }}
71
-    {{ text_in_id('Selector parameter 2', 'label_s2', 's2', cond.s2) }}
72
-    {{ text_in_id('Selector parameter 3', 'label_s3', 's3', cond.s3) }}
73
+    {{ select_in_id('Selector', 'select_selector', 'selector', cond.selector, ['header', 'JSON', 'path', 'value'], ['header', 'JSON', 'path', 'value']) }}
74
+    {{ textarea_in_id('Selector parameter 1', 'label_s1', 's1', cond.s1) }}
75
+    {{ textarea_in_id('Selector parameter 2', 'label_s2', 's2', cond.s2) }}
76
+    {{ textarea_in_id('Selector parameter 3', 'label_s3', 's3', cond.s3) }}
73 77
     {{ select_in_id('Test', 'select_test', 'test', cond.test, ['equal', 'inrange', 'truthy', 'contains'], ['equal', 'inrange', 'truthy', 'contains']) }}
74
-    {{ text_in_id('Test parameter 1', 'label_t1', 't1', cond.t1) }}
75
-    {{ text_in_id('Test parameter 2', 'label_t2', 't2', cond.t2) }}
76
-    {{ text_in_id('Test parameter 3', 'label_t3', 't3', cond.t3) }}
78
+    {{ textarea_in_id('Test parameter 1', 'label_t1', 't1', cond.t1) }}
79
+    {{ textarea_in_id('Test parameter 2', 'label_t2', 't2', cond.t2) }}
80
+    {{ textarea_in_id('Test parameter 3', 'label_t3', 't3', cond.t3) }}
77 81
     {{ checkbox_in('Invert this condition', 'invert', cond.invert) }}
78 82
     <script type="text/javascript">
79 83
         reg_selector_select(
80 84
             document.querySelector("#select_selector"),
81 85
             document.querySelector("#label_s1"),
82 86
             document.querySelector("#label_s2"),
83
-            document.querySelector("#label_s3")
87
+            document.querySelector("#label_s3"),
88
+			"{{cond.selector}}"
84 89
         );
85 90
         reg_test_select(
86 91
             document.querySelector("#select_test"),
87 92
             document.querySelector("#label_t1"),
88 93
             document.querySelector("#label_t2"),
89
-            document.querySelector("#label_t3")
94
+            document.querySelector("#label_t3"),
95
+			"{{cond.test}}"
90 96
         );
91 97
     </script>
92 98
 {%- endmacro %}
@@ -105,15 +111,16 @@
105 111
 
106 112
 {% macro act_params(act) -%}
107 113
     {{ select_in_id('Action', 'select_action', 'action', act.action, ['post', 'gitlab', 'system', 'udp', 'tcp', 'set_response'], ['post', 'gitlab', 'system', 'udp', 'tcp', 'set_response']) }}
108
-    {{ text_in_id('Action parameter 1', 'label_a1', 'a1', act.a1) }}
109
-    {{ text_in_id('Action parameter 2', 'label_a2', 'a2', act.a2) }}
110
-    {{ text_in_id('Action parameter 3', 'label_a3', 'a3', act.a3) }}
114
+    {{ textarea_in_id('Action parameter 1', 'label_a1', 'a1', act.a1) }}
115
+    {{ textarea_in_id('Action parameter 2', 'label_a2', 'a2', act.a2) }}
116
+    {{ textarea_in_id('Action parameter 3', 'label_a3', 'a3', act.a3) }}
111 117
     <script type="text/javascript">
112 118
         reg_action_select(
113 119
             document.querySelector("#select_action"),
114 120
             document.querySelector("#label_a1"),
115 121
             document.querySelector("#label_a2"),
116
-            document.querySelector("#label_a3")
122
+            document.querySelector("#label_a3"),
123
+			"{{act.action}}"
117 124
         );
118 125
     </script>
119 126
 {%- endmacro %}

+ 2
- 2
util.py View File

@@ -14,13 +14,13 @@ def header_loads(hdr):
14 14
 def jloads(s):
15 15
     try:
16 16
         return json.loads(s)
17
-    except ValueError:
17
+    except (ValueError, TypeError):
18 18
         return s
19 19
 
20 20
 def jdumps(obj):
21 21
     try:
22 22
         return json.dumps(obj)
23
-    except ValueError:
23
+    except (ValueError, TypeError):
24 24
         return obj
25 25
 
26 26
 def checkbox(request, name):

Loading…
Cancel
Save