Refactor of writing/reading in SourceCodeEventHandler.handle_event()

This commit is contained in:
Kristóf Tóth 2018-01-17 14:34:27 +01:00
parent 70a2dd1de6
commit 2793efeaf9

View File

@ -25,20 +25,25 @@ class SourceCodeEventHandler(EventHandlerBase):
if anchor == b'reset': if anchor == b'reset':
self.file = self.create_initial_state(process_is_running=True) self.file = self.create_initial_state(process_is_running=True)
if data['command'] == 'read': if data['command'] == 'read':
with open(self.file, 'r') as ifile: self.read_file(data_json)
content = ifile.read()
data_json['data'] = {
'filename': self.filename,
'content': content,
'language': self.language
}
return data_json
elif data['command'] == 'write': elif data['command'] == 'write':
with open(self.file, 'w') as ofile: self.write_file(data)
ofile.write(data['content']) return data_json
self.supervisor.stopProcess(self.process_name)
self.supervisor.startProcess(self.process_name) def read_file(self, data_json):
return None 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): def create_initial_state(self, process_is_running=False):
if process_is_running: if process_is_running: