From 3104a59f4404609af03c279df674b8b94a32f4be Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 14 Sep 2023 00:24:52 +0200 Subject: [PATCH] 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. --- platypush/config/__init__.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/platypush/config/__init__.py b/platypush/config/__init__.py index fce5fe92c..bf3774e16 100644 --- a/platypush/config/__init__.py +++ b/platypush/config/__init__.py @@ -106,10 +106,11 @@ class Config: if cfgfile is None: cfgfile = self._get_default_cfgfile() + cfgfile = os.path.abspath(os.path.expanduser(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): logging_config = { @@ -211,21 +212,24 @@ class Config: '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__)) - # Use /etc/platypush/config.yaml if the user is running as root, - # otherwise ~/.config/platypush/config.yaml - cfgfile = ( - ( - os.path.join(os.environ['XDG_CONFIG_HOME'], 'config.yaml') - if os.environ.get('XDG_CONFIG_HOME') - else os.path.join( - os.path.expanduser('~'), '.config', 'platypush', 'config.yaml' + + if not cfgfile: + # Use /etc/platypush/config.yaml if the user is running as root, + # otherwise ~/.config/platypush/config.yaml + cfgfile = ( + ( + os.path.join(os.environ['XDG_CONFIG_HOME'], 'config.yaml') + if os.environ.get('XDG_CONFIG_HOME') + else os.path.join( + os.path.expanduser('~'), '.config', 'platypush', 'config.yaml' + ) ) + if not is_root() + else os.path.join(os.sep, 'etc', 'platypush', 'config.yaml') ) - if not is_root() - else os.path.join(os.sep, 'etc', 'platypush', 'config.yaml') - ) cfgdir = pathlib.Path(cfgfile).parent cfgdir.mkdir(parents=True, exist_ok=True) @@ -526,6 +530,7 @@ class Config: Get a config value or the whole configuration object. :param key: Configuration entry to get (default: all entries). + :param default: Default value to return if the key is missing. """ # pylint: disable=protected-access config = cls._get_instance()._config.copy()