From c851e7aca7b2594873f85dbc876761784edddf7c Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Wed, 19 Jan 2022 19:17:32 +0100 Subject: [PATCH] config: fix save method when file exists and is writeable but not the directory --- mylib/config.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mylib/config.py b/mylib/config.py index 498efdd..c154f47 100644 --- a/mylib/config.py +++ b/mylib/config.py @@ -671,17 +671,17 @@ class Config: # pylint: disable=too-many-instance-attributes filepath = filepath if filepath else self._filepath assert filepath, 'Configuration filepath is not set or provided' - # Checking access of target directory + # Checking access of target file/directory dirpath = os.path.dirname(filepath) - if not os.path.isdir(dirpath) or not os.access(dirpath, os.R_OK | os.W_OK | os.X_OK): + if os.path.isfile(filepath): + if not os.access(filepath, os.W_OK): + log.error('Configuration file "%s" is not writable', filepath) + return False + elif not os.path.isdir(dirpath) or not os.access(dirpath, os.R_OK | os.W_OK | os.X_OK): log.error( 'Configuration directory "%s" does not exist (or not writable)', dirpath) return False - if os.path.isfile(filepath) and not os.access(filepath, os.W_OK): - log.error('Configuration file "%s" is not writable', filepath) - return False - lines = ['#\n# %s configuration\n#\n' % self.appname] for section_name in self._ordered_section_names: