From 9fe0f49bbd0699a5660c130bcb100a498af13e18 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Wed, 23 Oct 2024 11:58:51 +0200 Subject: [PATCH 1/5] properly handle bad config --- dotdrop/cfg_yaml.py | 8 ++++---- dotdrop/dotdrop.py | 8 ++++---- dotdrop/options.py | 4 +++- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/dotdrop/cfg_yaml.py b/dotdrop/cfg_yaml.py index 5d2c701c..413b1601 100644 --- a/dotdrop/cfg_yaml.py +++ b/dotdrop/cfg_yaml.py @@ -1340,7 +1340,7 @@ def _load_yaml(self, path): def _validate(self, yamldict): """validate entries""" if not yamldict: - return + raise YamlException('empty config file') # check top entries for entry in self.top_entries: @@ -1352,15 +1352,15 @@ def _validate(self, yamldict): # check link_dotfile_default if self.key_settings not in yamldict: # no configs top entry - return + raise YamlException(f'no \"{self.key_settings}\" key found') if not yamldict[self.key_settings]: # configs empty - return + raise YamlException(f'empty \"{self.key_settings}\" key') # check settings values settings = yamldict[self.key_settings] if self.key_settings_link_dotfile_default not in settings: - return + raise YamlException(f'no \"{self.key_settings_link_dotfile_default}\" key found') val = settings[self.key_settings_link_dotfile_default] if val not in self.allowed_link_val: err = f'bad link value: {val}' diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index 9f605925..ade54f21 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -935,16 +935,16 @@ def main(): try: opts = Options() except YamlException as exc: - LOG.err(f'error (yaml): {exc}') + LOG.err(f'yaml error: {exc}') return False except ConfigException as exc: - LOG.err(f'error (config): {exc}') + LOG.err(f'config error: {exc}') return False except UndefinedException as exc: - LOG.err(f'error (deps): {exc}') + LOG.err(f'dependencies error: {exc}') return False except OptionsException as exc: - LOG.err(f'error (options): {exc}') + LOG.err(f'options error: {exc}') return False if opts.debug: diff --git a/dotdrop/options.py b/dotdrop/options.py index 3ead9714..972bf151 100644 --- a/dotdrop/options.py +++ b/dotdrop/options.py @@ -159,12 +159,14 @@ def __init__(self, args=None): # selected profile self.profile = self.args['--profile'] self.confpath = self._get_config_path() + if not self.confpath: + raise YamlException('no config file found') self.confpath = os.path.abspath(self.confpath) self.log.dbg(f'config abs path: {self.confpath}') if not self.confpath: raise YamlException('no config file found') if not os.path.exists(self.confpath): - err = f'bad config file path: {self.confpath}' + err = f'config does not exist \"{self.confpath}\"' raise YamlException(err) self.log.dbg('#################################################') self.log.dbg('#################### DOTDROP ####################') From 0624a6119b05dbcb1e83e3966aec847cf5700342 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Wed, 23 Oct 2024 14:23:51 +0200 Subject: [PATCH 2/5] linting --- dotdrop/cfg_yaml.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dotdrop/cfg_yaml.py b/dotdrop/cfg_yaml.py index 413b1601..5f0b844b 100644 --- a/dotdrop/cfg_yaml.py +++ b/dotdrop/cfg_yaml.py @@ -1360,7 +1360,8 @@ def _validate(self, yamldict): # check settings values settings = yamldict[self.key_settings] if self.key_settings_link_dotfile_default not in settings: - raise YamlException(f'no \"{self.key_settings_link_dotfile_default}\" key found') + msg = f'no \"{self.key_settings_link_dotfile_default}\" key found' + raise YamlException(msg) val = settings[self.key_settings_link_dotfile_default] if val not in self.allowed_link_val: err = f'bad link value: {val}' From adc0ab56a51722d726c2ae5fadd29f2deabdaa34 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Wed, 23 Oct 2024 20:34:55 +0200 Subject: [PATCH 3/5] fix bug --- dotdrop/cfg_yaml.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/dotdrop/cfg_yaml.py b/dotdrop/cfg_yaml.py index 5f0b844b..17f650ca 100644 --- a/dotdrop/cfg_yaml.py +++ b/dotdrop/cfg_yaml.py @@ -1359,15 +1359,13 @@ def _validate(self, yamldict): # check settings values settings = yamldict[self.key_settings] - if self.key_settings_link_dotfile_default not in settings: - msg = f'no \"{self.key_settings_link_dotfile_default}\" key found' - raise YamlException(msg) - val = settings[self.key_settings_link_dotfile_default] - if val not in self.allowed_link_val: - err = f'bad link value: {val}' - self._log.err(err) - self._log.err(f'allowed: {self.allowed_link_val}') - raise YamlException(f'config content error: {err}') + if self.key_settings_link_dotfile_default in settings: + val = settings[self.key_settings_link_dotfile_default] + if val not in self.allowed_link_val: + err = f'bad link value: {val}' + self._log.err(err) + self._log.err(f'allowed: {self.allowed_link_val}') + raise YamlException(f'config content error: {err}') @classmethod def _yaml_load(cls, path): From 370e0939aa9293392add724689dfec89a6505e56 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Thu, 24 Oct 2024 22:37:53 +0200 Subject: [PATCH 4/5] fix sub config import error --- dotdrop/cfg_yaml.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/dotdrop/cfg_yaml.py b/dotdrop/cfg_yaml.py index 17f650ca..9ff0450e 100644 --- a/dotdrop/cfg_yaml.py +++ b/dotdrop/cfg_yaml.py @@ -115,7 +115,8 @@ class CfgYaml: top_entries = [key_dotfiles, key_settings, key_profiles] def __init__(self, path, profile=None, addprofiles=None, - reloading=False, debug=False, imported_configs=None): + reloading=False, debug=False, imported_configs=None, + fail_on_missing_keys=True): """ config parser @path: config file path @@ -168,7 +169,7 @@ def __init__(self, path, profile=None, addprofiles=None, # live patch deprecated entries self._fix_deprecated(self._yaml_dict) # validate content - self._validate(self._yaml_dict) + self._validate(self._yaml_dict, fail_on_missing_keys) ################################################## # parse the config and variables @@ -1080,7 +1081,8 @@ def _import_config(self, path): sub = CfgYaml(path, profile=self._profile, addprofiles=self._inc_profiles, debug=self._debug, - imported_configs=self.imported_configs) + imported_configs=self.imported_configs, + fail_on_missing_keys=False) # settings are ignored from external file # except for filter_file and func_file @@ -1337,7 +1339,7 @@ def _load_yaml(self, path): self._dbg(f'format: {self._config_format}') return content - def _validate(self, yamldict): + def _validate(self, yamldict, fail_on_missing_key): """validate entries""" if not yamldict: raise YamlException('empty config file') @@ -1353,7 +1355,7 @@ def _validate(self, yamldict): if self.key_settings not in yamldict: # no configs top entry raise YamlException(f'no \"{self.key_settings}\" key found') - if not yamldict[self.key_settings]: + if fail_on_missing_key and not yamldict[self.key_settings]: # configs empty raise YamlException(f'empty \"{self.key_settings}\" key') From 77b70454fb2c3b43916368cee4de0324b1841569 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Thu, 24 Oct 2024 23:24:39 +0200 Subject: [PATCH 5/5] do not fail on sub config --- dotdrop/cfg_yaml.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/dotdrop/cfg_yaml.py b/dotdrop/cfg_yaml.py index 9ff0450e..de532d0a 100644 --- a/dotdrop/cfg_yaml.py +++ b/dotdrop/cfg_yaml.py @@ -116,7 +116,7 @@ class CfgYaml: def __init__(self, path, profile=None, addprofiles=None, reloading=False, debug=False, imported_configs=None, - fail_on_missing_keys=True): + fail_on_error=True): """ config parser @path: config file path @@ -169,7 +169,7 @@ def __init__(self, path, profile=None, addprofiles=None, # live patch deprecated entries self._fix_deprecated(self._yaml_dict) # validate content - self._validate(self._yaml_dict, fail_on_missing_keys) + self._validate(self._yaml_dict, fail_on_error) ################################################## # parse the config and variables @@ -1082,7 +1082,7 @@ def _import_config(self, path): addprofiles=self._inc_profiles, debug=self._debug, imported_configs=self.imported_configs, - fail_on_missing_keys=False) + fail_on_error=False) # settings are ignored from external file # except for filter_file and func_file @@ -1339,10 +1339,12 @@ def _load_yaml(self, path): self._dbg(f'format: {self._config_format}') return content - def _validate(self, yamldict, fail_on_missing_key): + def _validate(self, yamldict, fail_on_error): """validate entries""" if not yamldict: - raise YamlException('empty config file') + if fail_on_error: + raise YamlException('empty config file') + return # check top entries for entry in self.top_entries: @@ -1354,10 +1356,14 @@ def _validate(self, yamldict, fail_on_missing_key): # check link_dotfile_default if self.key_settings not in yamldict: # no configs top entry - raise YamlException(f'no \"{self.key_settings}\" key found') - if fail_on_missing_key and not yamldict[self.key_settings]: + if fail_on_error: + raise YamlException(f'no \"{self.key_settings}\" key found') + return + if not yamldict[self.key_settings]: # configs empty - raise YamlException(f'empty \"{self.key_settings}\" key') + if fail_on_error: + raise YamlException(f'empty \"{self.key_settings}\" key') + return # check settings values settings = yamldict[self.key_settings]