From 85b870124d084eb7fc2c7fcdcb0eda78d9a6e73b Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 3 Sep 2021 17:27:18 +0200 Subject: [PATCH] Seaching now config file on different places --- lib/cr_tf/app.py | 65 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/lib/cr_tf/app.py b/lib/cr_tf/app.py index 983272d..4f968ba 100644 --- a/lib/cr_tf/app.py +++ b/lib/cr_tf/app.py @@ -45,7 +45,7 @@ from .xlate import __base_dir__ as __xlate_base_dir__ from .xlate import __mo_file__ as __xlate_mo_file__ from .xlate import XLATOR, LOCALE_DIR, DOMAIN -__version__ = '1.1.3' +__version__ = '1.1.4' LOG = logging.getLogger(__name__) SIGNAL_NAMES = { @@ -195,6 +195,24 @@ class CrTfApplication(BaseApplication): """Configuration file.""" return self._cfg_file + # ------------------------------------------------------------------------- + def _search_cfg_file(self): + + search_dirs = [] + search_dirs.append(self.base_dir.parent) + search_dirs.append(parent_dir / 'etc') + search_dirs.append(self.base_dir / 'etc') + + for sdir in search_dirs: + cfg_file = sdir / CFGFILE_BASENAME + LOG.debug(_("Searching for config file {!r} ...").format(str(cfg_file))) + if cfg_file.exists() and cfg_file.is_file(): + self._cfg_dir = sdir + self._cfg_file = cfg_file + return + self._cfg_dir = self.base_dir / 'etc' + self._cfg_file = self.base_dir.parent / CFGFILE_BASENAME + # ------------------------------------------------------------------------- def post_init(self): """ @@ -212,40 +230,51 @@ class CrTfApplication(BaseApplication): self.init_logging() - self._cfg_dir = self.base_dir.joinpath('etc') - self._cfg_file = self.cfg_dir.joinpath(CFGFILE_BASENAME) + self._search_cfg_file() self.perform_arg_parser() if not self.cfg_file.exists(): - default_conf_file = self.cfg_dir.joinpath(CFGFILE_BASENAME + '.default') cur_dir = Path(os.getcwd()) - cfg_file_rel = os.path.relpath(str(self.cfg_file), str(cur_dir)) + default_conf_file = self.cfg_dir / (CFGFILE_BASENAME + '.default') default_cfg_file_rel = os.path.relpath(str(default_conf_file), str(cur_dir)) + + cfg1 = self.base_dir.parent / CFGFILE_BASENAME + cfg1 = os.path.relpath(str(cfg1), str(cur_dir)) + + cfg2 = self.base_dir.parent / 'etc' / CFGFILE_BASENAME + cfg2 = os.path.relpath(str(cfg2), str(cur_dir)) + + cfg3 = self.cfg_dir / CFGFILE_BASENAME + cfg3 = os.path.relpath(str(cfg3), str(cur_dir)) + + cfg_file_rel = os.path.relpath(str(self.cfg_file), str(cur_dir)) msg = (_( - "Configuration file {f!r} does not exists.\nPlease copy {d!r} to {f!r} and " - "fill out all necessary entries, e.g. the passwords and API keys.").format( - f=cfg_file_rel, d=default_cfg_file_rel)) - LOG.error(msg) - self.exit(1) + "Config file {f!r} not found, using defaults.\n" + "To avoid this message, you may copy {d!r} to {c1!r}, {c2!r} or {c3!r} " + "and fill out all necessary entries, e.g. the passwords and API keys.").format( + f=CFGFILE_BASENAME, d=default_cfg_file_rel, c1=cfg1, c2=cfg2, c3=cfg3)) + LOG.warn(msg) self.config = CrTfConfiguration( appname=self.appname, verbose=self.verbose, base_dir=self.base_dir, config_file=self.cfg_file) - try: - self.config.read() - except ConfigError as e: - LOG.error(_("Error in configuration:") + " " + str(e)) - self.exit(1) + if self.cfg_file.exists(): + try: + self.config.read() + except ConfigError as e: + LOG.error(_("Error in configuration:") + " " + str(e)) + self.exit(1) + if self.verbose > 3: + LOG.debug(_("Read configuration:") + '\n' + pp(self.config.as_dict())) + if self.config.verbose > self.verbose: self.verbose = self.config.verbose if self.config.simulate: self.simulate = True - self.config.initialized = True - if self.verbose > 3: - LOG.debug(_("Read configuration:") + '\n' + pp(self.config.as_dict())) + self.config.initialized = True if self.config.puppet_envs_delete: LOG.debug(_("Removing allowed puppet environments ...")) -- 2.39.5