From 4b8b71d96715266c149fd1354794041be27d784d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 15 Mar 2018 12:02:56 +0100 Subject: [PATCH 1/7] Expose FileManager exclude as a property --- lib/tfw/components/source_code_event_handler.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/tfw/components/source_code_event_handler.py b/lib/tfw/components/source_code_event_handler.py index 5ff61e8..f700324 100644 --- a/lib/tfw/components/source_code_event_handler.py +++ b/lib/tfw/components/source_code_event_handler.py @@ -9,12 +9,21 @@ log = logging.getLogger(__name__) class FileManager: - def __init__(self, working_directory, selected_file=None, exclude=None): - self.exclude = exclude if exclude is not None else [] + def __init__(self, working_directory, selected_file=None, exclude=()): + self._exclude = None + self.exclude = exclude self._workdir = None self.workdir = working_directory self.filename = selected_file or self.files[0] + @property + def exclude(self): + return self._exclude + + @exclude.setter + def exclude(self, *exclude): + self._exclude = exclude + @property def workdir(self): return self._workdir From 0ef502a680348188418f90a6ae99a752a2f55305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 15 Mar 2018 12:04:39 +0100 Subject: [PATCH 2/7] Make property setup stuff in FileManager.__init__() more readable --- lib/tfw/components/source_code_event_handler.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/tfw/components/source_code_event_handler.py b/lib/tfw/components/source_code_event_handler.py index f700324..1230aa6 100644 --- a/lib/tfw/components/source_code_event_handler.py +++ b/lib/tfw/components/source_code_event_handler.py @@ -10,10 +10,8 @@ log = logging.getLogger(__name__) class FileManager: def __init__(self, working_directory, selected_file=None, exclude=()): - self._exclude = None - self.exclude = exclude - self._workdir = None - self.workdir = working_directory + self._exclude, self.exclude = None, exclude + self._workdir, self.workdir = None, working_directory self.filename = selected_file or self.files[0] @property From 1b87863997374f2653be6c633b988ef80f5e495e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 15 Mar 2018 12:18:39 +0100 Subject: [PATCH 3/7] Refactor FileManager.select_file() to @filename.setter property --- lib/tfw/components/source_code_event_handler.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/tfw/components/source_code_event_handler.py b/lib/tfw/components/source_code_event_handler.py index 1230aa6..4823333 100644 --- a/lib/tfw/components/source_code_event_handler.py +++ b/lib/tfw/components/source_code_event_handler.py @@ -10,9 +10,9 @@ log = logging.getLogger(__name__) class FileManager: def __init__(self, working_directory, selected_file=None, exclude=()): - self._exclude, self.exclude = None, exclude + self._exclude, self.exclude = None, *exclude self._workdir, self.workdir = None, working_directory - self.filename = selected_file or self.files[0] + self._filename, self.filename = None, selected_file or self.files[0] @property def exclude(self): @@ -32,10 +32,15 @@ class FileManager: raise EnvironmentError('"{}" is not a directory!'.format(directory)) self._workdir = directory - def select_file(self, filename): + @property + def filename(self): + return self._filename + + @filename.setter + def filename(self, filename): if not filename in self.files: raise EnvironmentError('No such file in workdir!') - self.filename = filename + self._filename = filename @property def files(self): @@ -99,7 +104,7 @@ class SourceCodeEventHandler(TriggerlessEventHandler): return data def select(self, data): - try: self.filemanager.select_file(data['filename']) + try: self.filemanager.filename = data['filename'] except EnvironmentError: log.exception('Failed to select file "{}"'.format(data['filename'])) return data @@ -108,7 +113,7 @@ class SourceCodeEventHandler(TriggerlessEventHandler): self.filemanager.workdir = data['directory'] self.reload_monitor() try: - self.filemanager.select_file(self.filemanager.files[0]) + self.filemanager.filename = self.filemanager.files[0] self.read(data) except IndexError: data['content'] = 'No files in this directory :(' From aa54e40f147439582fee0e9a096808ba3791df14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 15 Mar 2018 15:43:42 +0100 Subject: [PATCH 4/7] Refactor SourceCodeEventHandler blacklisting to use unix pattern matching --- lib/tfw/components/source_code_event_handler.py | 12 ++++++++---- src/demo/event_handler_main.py | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/tfw/components/source_code_event_handler.py b/lib/tfw/components/source_code_event_handler.py index 4823333..46264cc 100644 --- a/lib/tfw/components/source_code_event_handler.py +++ b/lib/tfw/components/source_code_event_handler.py @@ -1,5 +1,7 @@ from os.path import isfile, join, relpath, exists, isdir from glob import glob +from fnmatch import fnmatchcase +from collections import Iterable from tfw.event_handler_base import TriggerlessEventHandler from tfw.components.directory_monitor import DirectoryMonitor @@ -9,8 +11,8 @@ log = logging.getLogger(__name__) class FileManager: - def __init__(self, working_directory, selected_file=None, exclude=()): - self._exclude, self.exclude = None, *exclude + def __init__(self, working_directory, selected_file=None, exclude=None): + self._exclude, self.exclude = None, exclude self._workdir, self.workdir = None, working_directory self._filename, self.filename = None, selected_file or self.files[0] @@ -19,7 +21,9 @@ class FileManager: return self._exclude @exclude.setter - def exclude(self, *exclude): + def exclude(self, exclude): + if exclude is None: return + if not isinstance(exclude, Iterable): raise TypeError('Exclude must be Iterable!') self._exclude = exclude @property @@ -46,7 +50,7 @@ class FileManager: def files(self): return [self._relpath(file) for file in glob(join(self._workdir, '**/*'), recursive=True) if isfile(file) and - not any(word in file for word in self.exclude)] + not any(fnmatchcase(file, blacklisted) for blacklisted in self.exclude)] @property def file_contents(self): diff --git a/src/demo/event_handler_main.py b/src/demo/event_handler_main.py index 730cf18..430b93b 100644 --- a/src/demo/event_handler_main.py +++ b/src/demo/event_handler_main.py @@ -40,7 +40,7 @@ toggle_next.button_state = False if __name__ == '__main__': - ide = SourceCodeEventHandler(key='webide', directory=tfwenv.WEBIDE_WD, exclude=['__pycache__']) + ide = SourceCodeEventHandler(key='webide', directory=tfwenv.WEBIDE_WD, exclude=['*.pyc']) terminado = TerminadoEventHandler(key='shell') terminado.historymonitor.subscribe_callback(cenator) terminado.historymonitor.subscribe_callback(selectdir) From cc8754017180dc0d83d1859a6a4636b44f658edc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 15 Mar 2018 15:54:07 +0100 Subject: [PATCH 5/7] Implement SourceCodeEventHandler blacklist manipulating API --- lib/tfw/components/source_code_event_handler.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/tfw/components/source_code_event_handler.py b/lib/tfw/components/source_code_event_handler.py index 46264cc..760b28b 100644 --- a/lib/tfw/components/source_code_event_handler.py +++ b/lib/tfw/components/source_code_event_handler.py @@ -77,7 +77,8 @@ class SourceCodeEventHandler(TriggerlessEventHandler): self.commands = {'read': self.read, 'write': self.write, 'select': self.select, - 'selectdir': self.select_dir} + 'selectdir': self.select_dir, + 'exclude': self.exclude} self._monitor = None self.reload_monitor() @@ -125,6 +126,11 @@ class SourceCodeEventHandler(TriggerlessEventHandler): log.error('Failed to select directory "{}"'.format(data['directory'])) return data + def exclude(self, data): + try: self.filemanager.exclude = list(data['exclude']) + except TypeError: log.error('Exclude must be Iterable!') + return data + def attach_fileinfo(self, data): data['filename'] = self.filemanager.filename data['files'] = self.filemanager.files From 785edd56b97558164e62d94fbd9d476aeb3c88f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 15 Mar 2018 16:11:35 +0100 Subject: [PATCH 6/7] Upgrade python packages --- requirements.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index e057890..cdc46e3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ -tornado==4.5.3 -pyzmq==16.0.4 +tornado==5.0 +pyzmq==17.0.0 transitions==0.6.4 terminado==0.8.1 watchdog==0.8.3 From ccb076c885819a28910a646c06e36d82a5fdd405 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Thu, 15 Mar 2018 16:11:53 +0100 Subject: [PATCH 7/7] Remove depecrated ioloop.install() calls --- lib/tfw/networking/controller_connector.py | 3 --- lib/tfw/networking/solvable_connector.py | 3 --- 2 files changed, 6 deletions(-) diff --git a/lib/tfw/networking/controller_connector.py b/lib/tfw/networking/controller_connector.py index dcafa70..5646b8d 100644 --- a/lib/tfw/networking/controller_connector.py +++ b/lib/tfw/networking/controller_connector.py @@ -1,12 +1,9 @@ import zmq -from zmq.eventloop import ioloop from zmq.eventloop.zmqstream import ZMQStream from tfw.config import tfwenv from tfw.networking.zmq_connector_base import ZMQConnectorBase -ioloop.install() - class ControllerConnector(ZMQConnectorBase): def __init__(self, zmq_context=None): diff --git a/lib/tfw/networking/solvable_connector.py b/lib/tfw/networking/solvable_connector.py index a207a21..0225b39 100644 --- a/lib/tfw/networking/solvable_connector.py +++ b/lib/tfw/networking/solvable_connector.py @@ -1,12 +1,9 @@ import zmq -from zmq.eventloop import ioloop from tfw.config import tfwenv from tfw.networking.zmq_connector_base import ZMQConnectorBase from tfw.networking.serialization import serialize_all, deserialize_all -ioloop.install() - class SolvableConnector(ZMQConnectorBase): def __init__(self, zmq_context=None):