Better processing of configuration file parameters.

- Do `abspath`+`expanduser` on the configuration file path before
  checking if it exists.

- If the path doesn't exist, but the user explicitly passed a
  configuration file, then copy/create the default configuration
  under the specified directory.
This commit is contained in:
Fabio Manganiello 2023-09-14 00:24:52 +02:00
parent ddd8f1afdc
commit 3104a59f44
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -106,10 +106,11 @@ class Config:
if cfgfile is None: if cfgfile is None:
cfgfile = self._get_default_cfgfile() cfgfile = self._get_default_cfgfile()
cfgfile = os.path.abspath(os.path.expanduser(cfgfile))
if cfgfile is None or not os.path.exists(cfgfile): if cfgfile is None or not os.path.exists(cfgfile):
cfgfile = self._create_default_config() cfgfile = self._create_default_config(cfgfile)
self.config_file = os.path.abspath(os.path.expanduser(cfgfile)) self.config_file = cfgfile
def _init_logging(self): def _init_logging(self):
logging_config = { logging_config = {
@ -211,8 +212,11 @@ class Config:
'variable': {}, 'variable': {},
} }
def _create_default_config(self): @staticmethod
def _create_default_config(cfgfile: Optional[str] = None):
cfg_mod_dir = os.path.dirname(os.path.abspath(__file__)) cfg_mod_dir = os.path.dirname(os.path.abspath(__file__))
if not cfgfile:
# Use /etc/platypush/config.yaml if the user is running as root, # Use /etc/platypush/config.yaml if the user is running as root,
# otherwise ~/.config/platypush/config.yaml # otherwise ~/.config/platypush/config.yaml
cfgfile = ( cfgfile = (
@ -526,6 +530,7 @@ class Config:
Get a config value or the whole configuration object. Get a config value or the whole configuration object.
:param key: Configuration entry to get (default: all entries). :param key: Configuration entry to get (default: all entries).
:param default: Default value to return if the key is missing.
""" """
# pylint: disable=protected-access # pylint: disable=protected-access
config = cls._get_instance()._config.copy() config = cls._get_instance()._config.copy()