mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-12-22 07:11:56 +00:00
Refactor IdeHandler
This commit is contained in:
parent
1d8de09c2a
commit
701b918e6f
@ -1,5 +1,4 @@
|
||||
import logging
|
||||
from os.path import isfile
|
||||
|
||||
from tfw.internals.networking import Scope
|
||||
from tfw.internals.inotify import InotifyObserver
|
||||
@ -36,10 +35,10 @@ class IdeHandler:
|
||||
keys = ['ide']
|
||||
type_id = 'ControlEventHandler'
|
||||
|
||||
def __init__(self, *, patterns, initial_file=''):
|
||||
def __init__(self, *, patterns, initial_file=None):
|
||||
self.connector = None
|
||||
self.filemanager = FileManager(patterns)
|
||||
self._initial_file = initial_file
|
||||
self._initial_file = initial_file or ''
|
||||
|
||||
self.monitor = InotifyObserver(
|
||||
path=self.filemanager.parents,
|
||||
@ -53,46 +52,48 @@ class IdeHandler:
|
||||
'ide.write' : self.write
|
||||
}
|
||||
|
||||
@property
|
||||
def initial_file(self):
|
||||
if not isfile(self._initial_file):
|
||||
self._initial_file = self.filemanager.files[0]
|
||||
return self._initial_file
|
||||
|
||||
def _reload_frontend(self, event): # pylint: disable=unused-argument
|
||||
self.send_message({'key': 'ide.reload'})
|
||||
|
||||
@property
|
||||
def initial_file(self):
|
||||
if not self.filemanager.is_allowed(self._initial_file):
|
||||
self._initial_file = self.filemanager.files[0]
|
||||
return self._initial_file
|
||||
|
||||
def send_message(self, message):
|
||||
self.connector.send_message(message, scope=Scope.WEBSOCKET)
|
||||
|
||||
def read(self, message):
|
||||
if message.get('files'):
|
||||
self.filemanager.patterns = message['files']
|
||||
try:
|
||||
message['content'] = self.filemanager.read_file(message['filename'])
|
||||
except PermissionError:
|
||||
message['content'] = 'You have no permission to open that file :('
|
||||
except FileNotFoundError:
|
||||
message['content'] = 'This file was removed :('
|
||||
except Exception: # pylint: disable=broad-except
|
||||
message['content'] = 'Failed to read file :('
|
||||
|
||||
def write(self, message):
|
||||
try:
|
||||
self.filemanager.write_file(message['filename'], message['content'])
|
||||
except Exception: # pylint: disable=broad-except
|
||||
LOG.exception('Error writing file!')
|
||||
del message['content']
|
||||
|
||||
def handle_event(self, message, _):
|
||||
try:
|
||||
if message['filename'] == '':
|
||||
message['filename'] = self.initial_file
|
||||
self.commands[message['key']](message)
|
||||
message['files'] = self.filemanager.files
|
||||
self.send_message(message)
|
||||
except KeyError:
|
||||
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
||||
|
||||
def read(self, message):
|
||||
if message.get('files'):
|
||||
self.filemanager.patterns = message['files']
|
||||
try:
|
||||
message['filename'] = message.get('filename') or self.initial_file
|
||||
message['content'] = self.filemanager.read_file(message['filename'])
|
||||
except (PermissionError, ValueError):
|
||||
message['content'] = 'You have no permission to open that file :('
|
||||
except FileNotFoundError:
|
||||
message['content'] = 'This file does not exist :('
|
||||
except Exception: # pylint: disable=broad-except
|
||||
message['content'] = 'Failed to read file :('
|
||||
LOG.exception('Error reading file!')
|
||||
|
||||
def write(self, message):
|
||||
try:
|
||||
self.filemanager.write_file(message['filename'], message['content'])
|
||||
except KeyError:
|
||||
LOG.error('You must provide a filename to write!')
|
||||
except Exception: # pylint: disable=broad-except
|
||||
LOG.exception('Error writing file!')
|
||||
del message['content']
|
||||
|
||||
def cleanup(self):
|
||||
self.monitor.stop()
|
||||
|
Loading…
Reference in New Issue
Block a user