From 566d2baac470797b81a24e0e96d8192c13124b47 Mon Sep 17 00:00:00 2001 From: Davide Laezza Date: Sat, 21 Mar 2020 17:49:11 +0100 Subject: [PATCH] Using rpartition to split attributes in import paths --- dotdrop/cfg_yaml.py | 37 ++++++++++++------ tests-ng/import-non-existing.sh | 69 +++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 12 deletions(-) diff --git a/dotdrop/cfg_yaml.py b/dotdrop/cfg_yaml.py index dc5fc76..bbe0898 100644 --- a/dotdrop/cfg_yaml.py +++ b/dotdrop/cfg_yaml.py @@ -61,6 +61,7 @@ class CfgYaml: key_import_profile_dfs = 'import' key_import_sep = ':' key_import_ignore_key = 'optional' + key_import_ignore_default = True # settings key_settings_dotpath = Settings.key_dotpath @@ -589,25 +590,37 @@ class CfgYaml: def _clear_profile_vars(self, dic): """remove profile variables from dic if found""" - [dic.pop(k, None) for k in self.prokeys] + for k in self.prokeys: + dic.pop(k, None) - def _parse_extended_import_path(self, path): + def _parse_extended_import_path(self, path_entry): """Parse an import path in a tuple (path, fatal_not_found).""" if self.debug: - self.log.dbg('parsing path entry {}'.format(path)) + self.log.dbg('parsing path entry {}'.format(path_entry)) - fields = path.split(self.key_import_sep) - fatal_not_found = True - filepath = path + path, _, attribute = path_entry.rpartition(self.key_import_sep) - if len(fields) > 1 and fields[-1] == self.key_import_ignore_key: + fatal_not_found = attribute != self.key_import_ignore_key + + is_valid_attribute = attribute in ('', self.key_import_ignore_key) + if not is_valid_attribute: + # If attribute is not valid it can mean that: + # - path_entry doesn't contain the separator, and attribute is set + # to the whole path by str.rpartition + # - path_entry contains a separator, but it's in the file path, so + # attribute is set to whatever comes after the separator by + # str.rpartition + # In both cases, path_entry is the path we're looking for. if self.debug: - self.log.dbg('path entry {} has fatal_not_found flag' - .format(path)) - fatal_not_found = False - filepath = ''.join(fields[:-1]) + self.log.dbg('using attribute default values for path {}' + .format(path_entry)) + path = path_entry + fatal_not_found = self.key_import_ignore_default + elif self.debug: + self.log.dbg('path entry {} has fatal_not_found flag set to {}' + .format(path_entry, fatal_not_found)) - return filepath, fatal_not_found + return path, fatal_not_found def _is_glob(self, path): """Quick test if path is a glob.""" diff --git a/tests-ng/import-non-existing.sh b/tests-ng/import-non-existing.sh index 6bc5d73..8b0f897 100755 --- a/tests-ng/import-non-existing.sh +++ b/tests-ng/import-non-existing.sh @@ -111,6 +111,29 @@ cd ${ddpath} | ${bin} files -c ${cfg} -p p1 -V [ "$?" = "0" ] && echo "variables" && exit 1 set -e +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles + import_variables: + - /variables/does/not/exist:with/separator +dotfiles: + f_abc: + dst: ${tmpd}/abc + src: abc +profiles: + p1: + dotfiles: + - f_abc +_EOF + +# dummy call +set +e +cd ${ddpath} | ${bin} files -c ${cfg} -p p1 -V +[ "$?" = "0" ] && echo "variables with separator" && exit 1 +set -e + cat > ${cfg} << _EOF config: backup: true @@ -157,6 +180,29 @@ cd ${ddpath} | ${bin} files -c ${cfg} -p p1 -V [ "$?" = "0" ] && echo "actions" && exit 1 set -e +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles + import_actions: + - /actions/does/not:exist/with/separator +dotfiles: + f_abc: + dst: ${tmpd}/abc + src: abc +profiles: + p1: + dotfiles: + - f_abc +_EOF + +# dummy call +set +e +cd ${ddpath} | ${bin} files -c ${cfg} -p p1 -V +[ "$?" = "0" ] && echo "actions with separator" && exit 1 +set -e + cat > ${cfg} << _EOF config: backup: true @@ -203,6 +249,29 @@ cd ${ddpath} | ${bin} files -c ${cfg} -p p1 -V [ "$?" = "0" ] && echo "configs" && exit 1 set -e +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles + import_configs: + - /configs/does:not/exist/with/separator +dotfiles: + f_abc: + dst: ${tmpd}/abc + src: abc +profiles: + p1: + dotfiles: + - f_abc +_EOF + +# dummy call +set +e +cd ${ddpath} | ${bin} files -c ${cfg} -p p1 -V +[ "$?" = "0" ] && echo "configs with separator" && exit 1 +set -e + cat > ${cfg} << _EOF config: backup: true