Implement SourceCodeEventHandler support for multiple files

This commit is contained in:
Kristóf Tóth 2018-02-07 12:02:53 +01:00
parent 71dcb58c92
commit eb344de7a2
2 changed files with 21 additions and 10 deletions

View File

@ -4,6 +4,6 @@ from tornado.ioloop import IOLoop
if __name__ == '__main__': 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') anchor_terminado = TerminadoEventHandler('anchor_terminado', 'terminado')
IOLoop.instance().start() IOLoop.instance().start()

View File

@ -1,5 +1,5 @@
from shutil import copy, rmtree, copytree 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 glob import glob
from tfw.util import SupervisorMixin from tfw.util import SupervisorMixin
@ -15,12 +15,12 @@ class FileManager:
self._sourcedir = source_directory self._sourcedir = source_directory
self._workdir = working_directory self._workdir = working_directory
self._reload_files() 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) self.language = map_file_extension_to_language(self.filename)
def select_file(self, filename): def select_file(self, filename):
if not filename in self.files: if not self._filepath(filename) in self.files:
raise EnvironmentError('No such file in workdir!') raise EnvironmentError('No such file in workdir!')
self.filename = filename self.filename = filename
self.language = map_file_extension_to_language(self.filename) self.language = map_file_extension_to_language(self.filename)
@ -47,13 +47,15 @@ class FileManager:
class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): 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) super().__init__(anchor)
self.filemanager = FileManager('login_projectdir', LOGIN_APP_DIR, selected_file=filename) self.filemanager = FileManager(directory, LOGIN_APP_DIR, selected_file=selected_file)
self.process_name = process_name or splitext(filename)[0] self.process_name = process_name
self.commands = { self.commands = {
'read': self.read, 'read': self.read,
'write': self.write 'write': self.write,
'select': self.select
} }
# Supervisor needs these to run the login program # 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.filemanager.file_contents = data_json['data']['content']
self.restart_process() 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): def handle_event(self, anchor, data_json):
data = data_json['data'] data = data_json['data']
self.commands[data['command']](data_json) self.commands[data['command']](data_json)
@ -84,4 +92,7 @@ def map_file_extension_to_language(filename):
'.js': 'javascript' '.js': 'javascript'
} }
_, extension = splitext(filename) _, extension = splitext(filename)
return language_map[extension] language = 'javascript'
try: language = language_map[extension]
except KeyError: log.debug('No such extension in list, falling back to default: "{}".'.format(language))
return language