mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-23 01:01:31 +00:00
Merge pull request #20 from avatao-content/webide-directories
Implement support for changing working directory in webide
This commit is contained in:
commit
b6f6725550
@ -1,4 +1,4 @@
|
|||||||
from os.path import isfile, join, relpath
|
from os.path import isfile, join, relpath, exists, isdir
|
||||||
from glob import glob
|
from glob import glob
|
||||||
|
|
||||||
from tfw.event_handler_base import TriggerlessEventHandler
|
from tfw.event_handler_base import TriggerlessEventHandler
|
||||||
@ -9,11 +9,22 @@ log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class FileManager:
|
class FileManager:
|
||||||
def __init__(self, working_directory, selected_file=None):
|
def __init__(self, working_directory, selected_file=None, exclude=None):
|
||||||
self.exclude = ['__pycache__']
|
self.exclude = exclude if exclude is not None else []
|
||||||
self._workdir = working_directory
|
self._workdir = None
|
||||||
|
self.workdir = working_directory
|
||||||
self.filename = selected_file or self.files[0]
|
self.filename = selected_file or self.files[0]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def workdir(self):
|
||||||
|
return self._workdir
|
||||||
|
|
||||||
|
@workdir.setter
|
||||||
|
def workdir(self, directory):
|
||||||
|
if not exists(directory) or not isdir(directory):
|
||||||
|
raise EnvironmentError('"{}" is not a directory!'.format(directory))
|
||||||
|
self._workdir = directory
|
||||||
|
|
||||||
def select_file(self, filename):
|
def select_file(self, filename):
|
||||||
if not filename in self.files:
|
if not filename in self.files:
|
||||||
raise EnvironmentError('No such file in workdir!')
|
raise EnvironmentError('No such file in workdir!')
|
||||||
@ -43,23 +54,29 @@ class FileManager:
|
|||||||
|
|
||||||
|
|
||||||
class SourceCodeEventHandler(TriggerlessEventHandler):
|
class SourceCodeEventHandler(TriggerlessEventHandler):
|
||||||
def __init__(self, key, directory, selected_file=None):
|
def __init__(self, key, directory, selected_file=None, exclude=None):
|
||||||
super().__init__(key)
|
super().__init__(key)
|
||||||
self.filemanager = FileManager(directory, selected_file=selected_file)
|
self.filemanager = FileManager(directory, selected_file=selected_file, exclude=exclude)
|
||||||
|
|
||||||
self.commands = {
|
self.commands = {'read': self.read,
|
||||||
'read': self.read,
|
|
||||||
'write': self.write,
|
'write': self.write,
|
||||||
'select': self.select
|
'select': self.select,
|
||||||
}
|
'selectdir': self.select_dir}
|
||||||
|
|
||||||
self._monitor = DirectoryMonitor(directory)
|
self._monitor = None
|
||||||
self._monitor.watch() # This runs on a separate thread
|
self.reload_monitor()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def monitor(self):
|
def monitor(self):
|
||||||
return self._monitor
|
return self._monitor
|
||||||
|
|
||||||
|
def reload_monitor(self):
|
||||||
|
if self._monitor:
|
||||||
|
try: self._monitor.stop()
|
||||||
|
except KeyError: logging.debug('Working directory was removed – ignoring...')
|
||||||
|
self._monitor = DirectoryMonitor(self.filemanager.workdir)
|
||||||
|
self._monitor.watch() # This runs on a separate thread
|
||||||
|
|
||||||
def read(self, data):
|
def read(self, data):
|
||||||
try: data['content'] = self.filemanager.file_contents
|
try: data['content'] = self.filemanager.file_contents
|
||||||
except PermissionError: data['content'] = 'You have no permission to open that file :('
|
except PermissionError: data['content'] = 'You have no permission to open that file :('
|
||||||
@ -79,9 +96,23 @@ class SourceCodeEventHandler(TriggerlessEventHandler):
|
|||||||
except EnvironmentError: log.exception('Failed to select file "{}"'.format(data['filename']))
|
except EnvironmentError: log.exception('Failed to select file "{}"'.format(data['filename']))
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
def select_dir(self, data):
|
||||||
|
try:
|
||||||
|
self.filemanager.workdir = data['directory']
|
||||||
|
self.reload_monitor()
|
||||||
|
try:
|
||||||
|
self.filemanager.select_file(self.filemanager.files[0])
|
||||||
|
self.read(data)
|
||||||
|
except IndexError:
|
||||||
|
data['content'] = 'No files in this directory :('
|
||||||
|
except EnvironmentError:
|
||||||
|
log.exception('Failed to select directory "{}"'.format(data['directory']))
|
||||||
|
return data
|
||||||
|
|
||||||
def attach_fileinfo(self, data):
|
def attach_fileinfo(self, data):
|
||||||
data['filename'] = self.filemanager.filename
|
data['filename'] = self.filemanager.filename
|
||||||
data['files'] = self.filemanager.files
|
data['files'] = self.filemanager.files
|
||||||
|
data['directory'] = self.filemanager.workdir
|
||||||
|
|
||||||
def handle_event(self, key, message):
|
def handle_event(self, key, message):
|
||||||
try:
|
try:
|
||||||
|
@ -9,10 +9,10 @@ log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
ide = SourceCodeEventHandler('webide', tfwenv.WEBIDE_WD)
|
ide = SourceCodeEventHandler(key='webide', directory=tfwenv.WEBIDE_WD, exclude=['__pycache__'])
|
||||||
terminado = TerminadoEventHandler('shell')
|
terminado = TerminadoEventHandler(key='shell')
|
||||||
terminado.historymonitor.subscribe_callback(lambda hist: log.debug('User executed command: "{}"'.format(hist[-1])))
|
terminado.historymonitor.subscribe_callback(callback=lambda hist: log.debug('User executed command: "{}"'.format(hist[-1])))
|
||||||
processmanager = ProcessManagingEventHandler('processmanager', ide.monitor)
|
processmanager = ProcessManagingEventHandler(key='processmanager', dirmonitor=ide.monitor)
|
||||||
|
|
||||||
eventhandlers = {ide, terminado, processmanager}
|
eventhandlers = {ide, terminado, processmanager}
|
||||||
try:
|
try:
|
||||||
|
Loading…
Reference in New Issue
Block a user