config & rights: use open() for better error messages

ConfigParser().read() doesn't differentiate between different types of
failure to read files, causing eg. "No such file" to be logged in all
cases, for example if permissions are insufficient. fix that by using
open() and ConfigParser().read_file() instead.
This commit is contained in:
Lauri Tirkkonen 2022-01-11 20:22:19 +02:00 committed by Unrud
parent 8fa4345b6f
commit 4c44940ec1
2 changed files with 10 additions and 9 deletions

View File

@ -276,17 +276,18 @@ def load(paths: Optional[Iterable[Tuple[str, bool]]] = None
for path, ignore_if_missing in paths: for path, ignore_if_missing in paths:
parser = RawConfigParser() parser = RawConfigParser()
config_source = "config file %r" % path config_source = "config file %r" % path
config: types.CONFIG
try: try:
if not parser.read(path): with open(path, "r") as f:
config = Configuration.SOURCE_MISSING parser.read_file(f)
if not ignore_if_missing:
raise RuntimeError("No such file: %r" % path)
else:
config = {s: {o: parser[s][o] for o in parser.options(s)} config = {s: {o: parser[s][o] for o in parser.options(s)}
for s in parser.sections()} for s in parser.sections()}
except Exception as e: except Exception as e:
raise RuntimeError("Failed to load %s: %s" % (config_source, e) if isinstance(e, FileNotFoundError) and ignore_if_missing:
) from e config = Configuration.SOURCE_MISSING
else:
raise RuntimeError("Failed to load %s: %s" % (config_source, e)
) from e
configuration.update(config, config_source) configuration.update(config, config_source)
return configuration return configuration

View File

@ -56,8 +56,8 @@ class Rights(rights.BaseRights):
escaped_user = re.escape(user) escaped_user = re.escape(user)
rights_config = configparser.ConfigParser() rights_config = configparser.ConfigParser()
try: try:
if not rights_config.read(self._filename): with open(self._filename, "r") as f:
raise RuntimeError("No such file: %r" % self._filename) rights_config.read_file(f)
except Exception as e: except Exception as e:
raise RuntimeError("Failed to load rights file %r: %s" % raise RuntimeError("Failed to load rights file %r: %s" %
(self._filename, e)) from e (self._filename, e)) from e