Refactor SourceCodeEventHandler blacklisting to use unix pattern matching

This commit is contained in:
Kristóf Tóth 2018-03-15 15:43:42 +01:00
parent 1b87863997
commit aa54e40f14
2 changed files with 9 additions and 5 deletions

View File

@ -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):

View File

@ -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)