From eb344de7a297ee6c2d019f78676b523c9b22bf4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 7 Feb 2018 12:02:53 +0100 Subject: [PATCH] Implement SourceCodeEventHandler support for multiple files --- src/event_handlers/event_handler_main.py | 2 +- .../source_code_event_handler.py | 29 +++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/event_handlers/event_handler_main.py b/src/event_handlers/event_handler_main.py index add1bb1..23aa5e5 100644 --- a/src/event_handlers/event_handler_main.py +++ b/src/event_handlers/event_handler_main.py @@ -4,6 +4,6 @@ from tornado.ioloop import IOLoop if __name__ == '__main__': - anchor_webide = SourceCodeEventHandler('anchor_webide', 'login_component.py', 'login') + anchor_webide = SourceCodeEventHandler('anchor_webide', 'login_projectdir', 'login') anchor_terminado = TerminadoEventHandler('anchor_terminado', 'terminado') IOLoop.instance().start() diff --git a/src/event_handlers/source_code_event_handler.py b/src/event_handlers/source_code_event_handler.py index 345ee20..0972079 100644 --- a/src/event_handlers/source_code_event_handler.py +++ b/src/event_handlers/source_code_event_handler.py @@ -1,5 +1,5 @@ from shutil import copy, rmtree, copytree -from os.path import splitext, isfile, join +from os.path import splitext, isfile, join, basename from glob import glob from tfw.util import SupervisorMixin @@ -15,12 +15,12 @@ class FileManager: self._sourcedir = source_directory self._workdir = working_directory self._reload_files() - self.filename = selected_file or self.files[0] + self.filename = selected_file or basename(self.files[0]) self.language = map_file_extension_to_language(self.filename) def select_file(self, filename): - if not filename in self.files: - raise EnvironmentError('No such file in workdir!') + if not self._filepath(filename) in self.files: + raise EnvironmentError('No such file in workdir!') self.filename = filename self.language = map_file_extension_to_language(self.filename) @@ -47,13 +47,15 @@ class FileManager: class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): - def __init__(self, anchor, filename, process_name=None): + def __init__(self, anchor, directory, process_name=None, selected_file=None): super().__init__(anchor) - self.filemanager = FileManager('login_projectdir', LOGIN_APP_DIR, selected_file=filename) - self.process_name = process_name or splitext(filename)[0] + self.filemanager = FileManager(directory, LOGIN_APP_DIR, selected_file=selected_file) + self.process_name = process_name + self.commands = { 'read': self.read, - 'write': self.write + 'write': self.write, + 'select': self.select } # Supervisor needs these to run the login program @@ -71,6 +73,12 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): self.filemanager.file_contents = data_json['data']['content'] self.restart_process() + def select(self, data_json): + try: + self.filemanager.select_file(data_json['data']['filename']) + except EnvironmentError: + log.critical('Failed to select file "{}"'.format(data_json['data']['filename'])) + def handle_event(self, anchor, data_json): data = data_json['data'] self.commands[data['command']](data_json) @@ -84,4 +92,7 @@ def map_file_extension_to_language(filename): '.js': 'javascript' } _, extension = splitext(filename) - return language_map[extension] \ No newline at end of file + language = 'javascript' + try: language = language_map[extension] + except KeyError: log.debug('No such extension in list, falling back to default: "{}".'.format(language)) + return language \ No newline at end of file