From 2793efeaf9320b96c082a04165647d6ff899e24b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 17 Jan 2018 14:34:27 +0100 Subject: [PATCH] Refactor of writing/reading in SourceCodeEventHandler.handle_event() --- .../source_code_event_handler.py | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/event_handlers/source_code_event_handler.py b/src/event_handlers/source_code_event_handler.py index 67b3058..2113699 100644 --- a/src/event_handlers/source_code_event_handler.py +++ b/src/event_handlers/source_code_event_handler.py @@ -25,20 +25,25 @@ class SourceCodeEventHandler(EventHandlerBase): if anchor == b'reset': self.file = self.create_initial_state(process_is_running=True) if data['command'] == 'read': - with open(self.file, 'r') as ifile: - content = ifile.read() - data_json['data'] = { - 'filename': self.filename, - 'content': content, - 'language': self.language - } - return data_json + self.read_file(data_json) elif data['command'] == 'write': - with open(self.file, 'w') as ofile: - ofile.write(data['content']) - self.supervisor.stopProcess(self.process_name) - self.supervisor.startProcess(self.process_name) - return None + self.write_file(data) + return data_json + + def read_file(self, data_json): + with open(self.file, 'r') as ifile: + content = ifile.read() + data_json['data'] = { + 'filename': self.filename, + 'content': content, + 'language': self.language + } + + def write_file(self, data): + with open(self.file, 'w') as ofile: + ofile.write(data['content']) + self.supervisor.stopProcess(self.process_name) + self.supervisor.startProcess(self.process_name) def create_initial_state(self, process_is_running=False): if process_is_running: