Merge pull request #20 from avatao-content/webide-directories

Implement support for changing working directory in webide
This commit is contained in:
Bokros Bálint 2018-03-09 11:22:05 +01:00 committed by GitHub
commit b6f6725550
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 17 deletions

View File

@ -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 tfw.event_handler_base import TriggerlessEventHandler
@ -9,11 +9,22 @@ log = logging.getLogger(__name__)
class FileManager:
def __init__(self, working_directory, selected_file=None):
self.exclude = ['__pycache__']
self._workdir = working_directory
def __init__(self, working_directory, selected_file=None, exclude=None):
self.exclude = exclude if exclude is not None else []
self._workdir = None
self.workdir = working_directory
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):
if not filename in self.files:
raise EnvironmentError('No such file in workdir!')
@ -43,23 +54,29 @@ class FileManager:
class SourceCodeEventHandler(TriggerlessEventHandler):
def __init__(self, key, directory, selected_file=None):
def __init__(self, key, directory, selected_file=None, exclude=None):
super().__init__(key)
self.filemanager = FileManager(directory, selected_file=selected_file)
self.filemanager = FileManager(directory, selected_file=selected_file, exclude=exclude)
self.commands = {
'read': self.read,
'write': self.write,
'select': self.select
}
self.commands = {'read': self.read,
'write': self.write,
'select': self.select,
'selectdir': self.select_dir}
self._monitor = DirectoryMonitor(directory)
self._monitor.watch() # This runs on a separate thread
self._monitor = None
self.reload_monitor()
@property
def monitor(self):
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):
try: data['content'] = self.filemanager.file_contents
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']))
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):
data['filename'] = self.filemanager.filename
data['files'] = self.filemanager.files
data['directory'] = self.filemanager.workdir
def handle_event(self, key, message):
try:

View File

@ -9,10 +9,10 @@ log = logging.getLogger(__name__)
if __name__ == '__main__':
ide = SourceCodeEventHandler('webide', tfwenv.WEBIDE_WD)
terminado = TerminadoEventHandler('shell')
terminado.historymonitor.subscribe_callback(lambda hist: log.debug('User executed command: "{}"'.format(hist[-1])))
processmanager = ProcessManagingEventHandler('processmanager', ide.monitor)
ide = SourceCodeEventHandler(key='webide', directory=tfwenv.WEBIDE_WD, exclude=['__pycache__'])
terminado = TerminadoEventHandler(key='shell')
terminado.historymonitor.subscribe_callback(callback=lambda hist: log.debug('User executed command: "{}"'.format(hist[-1])))
processmanager = ProcessManagingEventHandler(key='processmanager', dirmonitor=ide.monitor)
eventhandlers = {ide, terminado, processmanager}
try: