Command -> handler mapping in SourceCodeEventHandler is now nicer

This commit is contained in:
Kristóf Tóth 2018-01-17 14:46:30 +01:00
parent 2793efeaf9
commit 863cc304ad

View File

@ -24,10 +24,9 @@ class SourceCodeEventHandler(EventHandlerBase):
data = data_json['data'] data = data_json['data']
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': command_handlers = {'read': self.read_file,
self.read_file(data_json) 'write': self.write_file}
elif data['command'] == 'write': command_handlers[data['command']](data_json)
self.write_file(data)
return data_json return data_json
def read_file(self, data_json): def read_file(self, data_json):
@ -39,7 +38,8 @@ class SourceCodeEventHandler(EventHandlerBase):
'language': self.language 'language': self.language
} }
def write_file(self, data): def write_file(self, data_json):
data = data_json['data']
with open(self.file, 'w') as ofile: with open(self.file, 'w') as ofile:
ofile.write(data['content']) ofile.write(data['content'])
self.supervisor.stopProcess(self.process_name) self.supervisor.stopProcess(self.process_name)