From 9f27c6bc481e87f4a41cd9e920c22674585a927a Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Sun, 3 May 2020 14:40:37 +0200 Subject: [PATCH] fix templatenotfound for #226 --- dotdrop/dotdrop.py | 6 ++++++ dotdrop/templategen.py | 16 ++++++++++++++-- tests-ng/import-configs.sh | 20 +++++++++++++++++--- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index 4bddfe6..2bd1968 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -209,6 +209,7 @@ def cmd_compare(o, tmp): t = Templategen(base=o.dotpath, variables=o.variables, func_file=o.func_file, filter_file=o.filter_file, debug=o.debug) + tvars = t.add_tmp_vars() inst = Installer(create=o.create, backup=o.backup, dry=o.dry, base=o.dotpath, workdir=o.workdir, debug=o.debug, @@ -217,6 +218,11 @@ def cmd_compare(o, tmp): comp = Comparator(diff_cmd=o.diff_command, debug=o.debug) for dotfile in selected: + # add dotfile variables + t.restore_vars(tvars) + newvars = dotfile.get_dotfile_variables() + t.add_tmp_vars(newvars=newvars) + if o.debug: LOG.dbg('comparing {}'.format(dotfile)) src = dotfile.src diff --git a/dotdrop/templategen.py b/dotdrop/templategen.py index ee0e3cb..15522e3 100644 --- a/dotdrop/templategen.py +++ b/dotdrop/templategen.py @@ -6,7 +6,8 @@ jinja2 template generator """ import os -from jinja2 import Environment, FileSystemLoader +from jinja2 import Environment, FileSystemLoader, \ + ChoiceLoader, FunctionLoader, TemplateNotFound # local imports import dotdrop.utils as utils @@ -35,7 +36,9 @@ class Templategen: self.base = base.rstrip(os.sep) self.debug = debug self.log = Logger() - loader = FileSystemLoader(self.base) + loader1 = FileSystemLoader(self.base) + loader2 = FunctionLoader(self._template_loader) + loader = ChoiceLoader([loader1, loader2]) self.env = Environment(loader=loader, trim_blocks=True, lstrip_blocks=True, keep_trailing_newline=True, @@ -142,6 +145,15 @@ class Templategen: return True return False + def _template_loader(self, relpath): + """manually load template when outside of base""" + path = os.path.join(self.base, relpath) + if not os.path.exists(path): + raise TemplateNotFound(path) + with open(path, 'r') as f: + content = f.read() + return content + def _handle_text_file(self, src): """write text to file""" template_rel_path = os.path.relpath(src, self.base) diff --git a/tests-ng/import-configs.sh b/tests-ng/import-configs.sh index a8278b1..b521a17 100755 --- a/tests-ng/import-configs.sh +++ b/tests-ng/import-configs.sh @@ -47,6 +47,7 @@ echo -e "$(tput setaf 6)==> RUNNING $(basename $BASH_SOURCE) <==$(tput sgr0)" # the dotfile source tmps=`mktemp -d --suffix='-dotdrop-tests' || mktemp -d` mkdir -p ${tmps}/dotfiles +mkdir -p ${tmps}/dotfiles-other # the dotfile destination tmpd=`mktemp -d --suffix='-dotdrop-tests' || mktemp -d` @@ -90,7 +91,7 @@ cat > ${cfg2} << _EOF config: backup: true create: true - dotpath: dotfiles + dotpath: dotfiles-other dotfiles: f_def: dst: ${tmpd}/def @@ -110,10 +111,19 @@ _EOF # create the source mkdir -p ${tmps}/dotfiles/ echo "abc" > ${tmps}/dotfiles/abc -echo "def" > ${tmps}/dotfiles/def -echo "ghi" > ${tmps}/dotfiles/ghi +echo "{{@@ _dotfile_abs_dst @@}}" >> ${tmps}/dotfiles/abc + +echo "def" > ${tmps}/dotfiles-other/def +echo "{{@@ _dotfile_abs_dst @@}}" >> ${tmps}/dotfiles-other/def + +echo "ghi" > ${tmps}/dotfiles-other/ghi +echo "{{@@ _dotfile_abs_dst @@}}" >> ${tmps}/dotfiles-other/ghi + echo "zzz" > ${tmps}/dotfiles/zzz +echo "{{@@ _dotfile_abs_dst @@}}" >> ${tmps}/dotfiles/zzz + echo "sub" > ${tmps}/dotfiles/sub +echo "{{@@ _dotfile_abs_dst @@}}" >> ${tmps}/dotfiles/sub # install cd ${ddpath} | ${bin} files -c ${cfg1} -p p0 -V | grep f_def @@ -123,6 +133,10 @@ cd ${ddpath} | ${bin} files -c ${cfg1} -p p3 -V | grep f_zzz cd ${ddpath} | ${bin} files -c ${cfg1} -p pup -V | grep f_sub cd ${ddpath} | ${bin} files -c ${cfg1} -p psubsub -V | grep f_sub +# test compare too +cd ${ddpath} | ${bin} install -c ${cfg1} -p p2 -V +cd ${ddpath} | ${bin} compare -c ${cfg1} -p p2 -V + ## CLEANING rm -rf ${tmps} ${tmpd}