From 5197bb5a5fa50fc04a68306b32f8abf7a411933b Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Mon, 9 Jun 2025 17:48:51 +0200 Subject: find_config(): Ignore paths which can't be open due to permission errors. This avoids crashing when `webmap-import` is run as another user on an homedir with restrictive permissions, for instance. --- common.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/common.py b/common.py index 0859ef0..74cd748 100644 --- a/common.py +++ b/common.py @@ -26,7 +26,7 @@ from pathlib import Path, PosixPath from stat import S_ISDIR import math import logging -from typing import Any, Iterator, Optional, Never +from typing import Any, Iterator, Optional, Never, TextIO from hashlib import sha256 from xdg.BaseDirectory import xdg_config_home @@ -64,8 +64,8 @@ class BadConfiguration(Exception): message = str(config_path) + ': ' + message super().__init__(message) -def find_config(filename : str = 'config.yml', appname : str = 'webmap') -> Path: - """Return the configuration file path""" +def open_config(filename : str = 'config.yml', appname : str = 'webmap') -> TextIO: + """Open the configuration file""" dirs = [ Path(), Path(xdg_config_home).joinpath(appname), @@ -73,15 +73,19 @@ def find_config(filename : str = 'config.yml', appname : str = 'webmap') -> Path ] for d in dirs: p = d.joinpath(filename) - if p.exists(): - return p + try: + return p.open(mode='r', encoding='utf-8') + except (FileNotFoundError, PermissionError) as e: + logging.debug('Ignoring exception %s', str(e)) raise MissingConfiguration(filename) def load_config(path : Optional[Path] = None) -> dict[str, Any]: """Load configuration file""" - config_path = find_config() if path is None else path - with config_path.open(mode='r', encoding='utf-8') as fp: + fp = open_config() if path is None else path.open(mode='r', encoding='utf-8') + try: return yaml.safe_load(fp) + finally: + fp.close() def layers_in_group(groupname : str, patterns : str|list[str], layernames : set[str]) -> Iterator[str]: -- cgit v1.2.3