Refactor FileManager.select_file() to @filename.setter property

This commit is contained in:
Kristóf Tóth 2018-03-15 12:18:39 +01:00
parent 0ef502a680
commit 1b87863997

View File

@ -10,9 +10,9 @@ log = logging.getLogger(__name__)
class FileManager: class FileManager:
def __init__(self, working_directory, selected_file=None, exclude=()): 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._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 @property
def exclude(self): def exclude(self):
@ -32,10 +32,15 @@ class FileManager:
raise EnvironmentError('"{}" is not a directory!'.format(directory)) raise EnvironmentError('"{}" is not a directory!'.format(directory))
self._workdir = 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: if not filename in self.files:
raise EnvironmentError('No such file in workdir!') raise EnvironmentError('No such file in workdir!')
self.filename = filename self._filename = filename
@property @property
def files(self): def files(self):
@ -99,7 +104,7 @@ class SourceCodeEventHandler(TriggerlessEventHandler):
return data return data
def select(self, 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'])) except EnvironmentError: log.exception('Failed to select file "{}"'.format(data['filename']))
return data return data
@ -108,7 +113,7 @@ class SourceCodeEventHandler(TriggerlessEventHandler):
self.filemanager.workdir = data['directory'] self.filemanager.workdir = data['directory']
self.reload_monitor() self.reload_monitor()
try: try:
self.filemanager.select_file(self.filemanager.files[0]) self.filemanager.filename = self.filemanager.files[0]
self.read(data) self.read(data)
except IndexError: except IndexError:
data['content'] = 'No files in this directory :(' data['content'] = 'No files in this directory :('