aboutsummaryrefslogtreecommitdiffstats
path: root/wee_slack.py
diff options
context:
space:
mode:
authorRyan Huber <rhuber@gmail.com>2017-02-02 06:16:06 -0800
committerRyan Huber <rhuber@gmail.com>2017-02-02 06:16:06 -0800
commitd4920b661b99f53097343dffbb8774392c1a6b43 (patch)
tree3831720fc6016d8fc23abe88cd7ed1f513e2c160 /wee_slack.py
parent25f7dab5a58fca12e4a0bf4151e5eb8f9a52e1b6 (diff)
downloadwee-slack-d4920b661b99f53097343dffbb8774392c1a6b43.tar.gz
enhance event recorder
Diffstat (limited to 'wee_slack.py')
-rw-r--r--wee_slack.py23
1 files changed, 18 insertions, 5 deletions
diff --git a/wee_slack.py b/wee_slack.py
index 98941c7..2ba38bd 100644
--- a/wee_slack.py
+++ b/wee_slack.py
@@ -100,12 +100,27 @@ class EventRouter(object):
self.recording_path = "/tmp"
def record(self):
+ """
+ Toggles the event recorder and creates a directory for data if enabled.
+ """
self.recording = not self.recording
if self.recording:
import os
if not os.path.exists(RECORD_DIR):
os.makedirs(RECORD_DIR)
+ def record_event(self, message_json, file_name_field):
+ """
+ Called each time you want to record an event.
+ message_json is a json in dict form
+ file_name_field is the json key whose value you want to be part of the file name
+ """
+ now = time.time()
+ mtype = message_json.get(file_name_field, 'unknown')
+ f = open('{}/{}-{}.json'.format(RECORD_DIR, now, mtype), 'w')
+ f.write("{}".format(json.dumps(message_json)))
+ f.close()
+
def shutdown(self):
self.shutting_down = not self.shutting_down
@@ -163,11 +178,7 @@ class EventRouter(object):
message_json["wee_slack_metadata"] = metadata
#print message_json
if self.recording:
- now = time.time()
- mtype = message_json.get('type', 'unknown')
- f = open('{}/{}-{}.json'.format(RECORD_DIR, now, mtype), 'w')
- f.write("{}".format(json.dumps(message_json)))
- f.close()
+ self.record_event(message_json, 'type')
self.receive_json(json.dumps(message_json))
except WebSocketConnectionClosedException:
#TODO: handle reconnect here
@@ -194,6 +205,8 @@ class EventRouter(object):
j["wee_slack_request_metadata"] = pickle.dumps(request_metadata)
#print self.reply_buffer[request_metadata.response_id]
self.reply_buffer.pop(request_metadata.response_id)
+ if self.recording:
+ self.record_event(j, 'wee_slack_process_method')
self.receive_json(json.dumps(j))
except:
dbg("FAILED")