From 296618a34e4329ffa850079f817cdcefece4b3a9 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Tue, 16 Apr 2024 10:39:35 +0200 Subject: [PATCH] config: allow to customize default config file mode --- mylib/config.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mylib/config.py b/mylib/config.py index 7fff4b4..327186b 100644 --- a/mylib/config.py +++ b/mylib/config.py @@ -6,7 +6,6 @@ import argparse import logging import os import re -import stat import sys import textwrap import traceback @@ -24,6 +23,7 @@ log = logging.getLogger(__name__) # Constants DEFAULT_ENCODING = "utf-8" DEFAULT_CONFIG_DIRPATH = os.path.expanduser("./") +DEFAULT_CONFIG_FILE_MODE = 0o600 DEFAULT_LOG_FORMAT = "%(asctime)s - %(module)s:%(lineno)d - %(levelname)s - %(message)s" DEFAULT_CONSOLE_LOG_FORMAT = DEFAULT_LOG_FORMAT DEFAULT_FILELOG_FORMAT = DEFAULT_LOG_FORMAT @@ -679,6 +679,7 @@ class Config: # pylint: disable=too-many-instance-attributes config_file_env_variable=None, default_config_dirpath=None, default_config_filename=None, + default_config_file_mode=None, ): self.appname = appname self.shortname = shortname @@ -694,6 +695,7 @@ class Config: # pylint: disable=too-many-instance-attributes self.config_file_env_variable = config_file_env_variable self.default_config_dirpath = default_config_dirpath self.default_config_filename = default_config_filename + self.default_config_file_mode = default_config_file_mode or DEFAULT_CONFIG_FILE_MODE self.add_logging_sections() self._init_config_parser() @@ -894,7 +896,7 @@ class Config: # pylint: disable=too-many-instance-attributes fd.write("\n".join(lines).encode(self.encoding)) # Privacy! - os.chmod(filepath, stat.S_IRUSR | stat.S_IWUSR) + os.chmod(filepath, self.default_config_file_mode) except Exception: # pylint: disable=broad-except log.exception("Failed to write generated configuration file %s", filepath) return False