From 7963b5e11f61db00d2150ded0a8d847ee3fbc378 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Mon, 8 Jan 2018 10:08:55 +0100 Subject: [PATCH] Support for include directive in config.yaml, #32 --- platypush/config/__init__.py | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/platypush/config/__init__.py b/platypush/config/__init__.py index d98b27fcf3..5a5ab4553a 100644 --- a/platypush/config/__init__.py +++ b/platypush/config/__init__.py @@ -46,13 +46,8 @@ class Config(object): raise RuntimeError('No config file specified and nothing found in {}' .format(self._cfgfile_locations)) - with open(cfgfile, 'r') as fp: - self._config = yaml.load(fp) - - for section in self._config: - if 'disabled' in self._config[section] \ - and self._config[section]['disabled']: - del self._config[section] + self._cfgfile = cfgfile + self._config = self._read_config_file(self._cfgfile) if 'logging' not in self._config: self._config['logging'] = logging.INFO @@ -69,6 +64,27 @@ class Config(object): self._init_components() + def _read_config_file(self, cfgfile): + if not os.path.isabs(cfgfile): + cfgfile = os.path.join(os.path.dirname(self._cfgfile), cfgfile) + + config = {} + with open(cfgfile, 'r') as fp: + file_config = yaml.load(fp) + + for section in file_config: + if section == 'include': + included_config = self._read_config_file(file_config[section]) + + for incl_section in included_config.keys(): + config[incl_section] = included_config[incl_section] + elif 'disabled' not in file_config[section] \ + or file_config[section]['disabled'] is False: + config[section] = file_config[section] + + return config + + def _init_components(self): for key in self._config.keys(): if key.startswith('backend.'):