config: allow to customize default config file mode
Some checks failed
Run tests / tests (push) Failing after 1m0s

This commit is contained in:
Benjamin Renard 2024-04-16 10:39:35 +02:00
parent 28103836ac
commit 296618a34e
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -6,7 +6,6 @@ import argparse
import logging import logging
import os import os
import re import re
import stat
import sys import sys
import textwrap import textwrap
import traceback import traceback
@ -24,6 +23,7 @@ log = logging.getLogger(__name__)
# Constants # Constants
DEFAULT_ENCODING = "utf-8" DEFAULT_ENCODING = "utf-8"
DEFAULT_CONFIG_DIRPATH = os.path.expanduser("./") 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_LOG_FORMAT = "%(asctime)s - %(module)s:%(lineno)d - %(levelname)s - %(message)s"
DEFAULT_CONSOLE_LOG_FORMAT = DEFAULT_LOG_FORMAT DEFAULT_CONSOLE_LOG_FORMAT = DEFAULT_LOG_FORMAT
DEFAULT_FILELOG_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, config_file_env_variable=None,
default_config_dirpath=None, default_config_dirpath=None,
default_config_filename=None, default_config_filename=None,
default_config_file_mode=None,
): ):
self.appname = appname self.appname = appname
self.shortname = shortname 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.config_file_env_variable = config_file_env_variable
self.default_config_dirpath = default_config_dirpath self.default_config_dirpath = default_config_dirpath
self.default_config_filename = default_config_filename 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.add_logging_sections()
self._init_config_parser() self._init_config_parser()
@ -894,7 +896,7 @@ class Config: # pylint: disable=too-many-instance-attributes
fd.write("\n".join(lines).encode(self.encoding)) fd.write("\n".join(lines).encode(self.encoding))
# Privacy! # Privacy!
os.chmod(filepath, stat.S_IRUSR | stat.S_IWUSR) os.chmod(filepath, self.default_config_file_mode)
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
log.exception("Failed to write generated configuration file %s", filepath) log.exception("Failed to write generated configuration file %s", filepath)
return False return False