Conciliate FileManager attribute names and formatting

This commit is contained in:
Kristóf Tóth 2018-06-04 21:47:10 +02:00
parent 92e9812776
commit afc84e1d1a

View File

@ -41,8 +41,8 @@ class FileManager: # pylint: disable=too-many-instance-attributes
def workdir(self, directory): def workdir(self, directory):
if not exists(directory) or not isdir(directory): if not exists(directory) or not isdir(directory):
raise EnvironmentError(f'"{directory}" is not a directory!') raise EnvironmentError(f'"{directory}" is not a directory!')
if not self._is_in_whitelisted_dir(directory): if not self._is_in_allowed_dir(directory):
raise EnvironmentError(f'Directory "{directory}" is not in whitelist!') raise EnvironmentError(f'Directory "{directory}" is not allowed!')
self._workdir = directory self._workdir = directory
@property @property
@ -65,8 +65,11 @@ class FileManager: # pylint: disable=too-many-instance-attributes
@property @property
def files(self): def files(self):
return [self._relpath(file) for file in glob(join(self._workdir, '**/*'), recursive=True) return [self._relpath(file)
if isfile(file) and self._is_in_whitelisted_dir(file) and not self._is_blacklisted(file)] for file in glob(join(self._workdir, '**/*'), recursive=True)
if isfile(file)
and self._is_in_allowed_dir(file)
and not self._is_blacklisted(file)]
@property @property
def file_contents(self): def file_contents(self):
@ -78,8 +81,9 @@ class FileManager: # pylint: disable=too-many-instance-attributes
with open(self._filepath(self.filename), 'w', errors='surrogateescape') as ofile: with open(self._filepath(self.filename), 'w', errors='surrogateescape') as ofile:
ofile.write(value) ofile.write(value)
def _is_in_whitelisted_dir(self, path): def _is_in_allowed_dir(self, path):
return any(realpath(path).startswith(allowed_dir) for allowed_dir in self.allowed_directories) return any(realpath(path).startswith(allowed_dir)
for allowed_dir in self.allowed_directories)
def _is_blacklisted(self, file): def _is_blacklisted(self, file):
return any(fnmatchcase(file, blacklisted) for blacklisted in self.exclude) return any(fnmatchcase(file, blacklisted) for blacklisted in self.exclude)
@ -110,16 +114,22 @@ class IdeEventHandler(EventHandlerBase, MonitorManagerMixin):
""" """
:param key: the key this instance should listen to :param key: the key this instance should listen to
:param directory: working directory which the EventHandler should serve files from :param directory: working directory which the EventHandler should serve files from
:param allowed_directories: list of directories that can be switched to using the selectdir command :param allowed_directories: list of directories that can be switched to using selectdir
:param selected_file: file that is selected by default :param selected_file: file that is selected by default
:param exclude: list of filenames that should not appear between files (for .o, .pyc, etc.) :param exclude: list of filenames that should not appear between files (for .o, .pyc, etc.)
""" """
super().__init__(key) super().__init__(key)
try: try:
self.filemanager = FileManager(allowed_directories=allowed_directories, working_directory=directory, self.filemanager = FileManager(
selected_file=selected_file, exclude=exclude) allowed_directories=allowed_directories,
working_directory=directory,
selected_file=selected_file,
exclude=exclude
)
except IndexError: except IndexError:
raise EnvironmentError(f'No file(s) in IdeEventHandler working_directory "{directory}"!') raise EnvironmentError(
f'No file(s) in IdeEventHandler working_directory "{directory}"!'
)
MonitorManagerMixin.__init__( MonitorManagerMixin.__init__(
self, self,