1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-05 15:33:54 +00:00

Merge pull request #227 from deadc0de6/templatenotfound

Templatenotfound
This commit is contained in:
deadc0de
2020-05-03 19:18:13 +02:00
committed by GitHub
3 changed files with 75 additions and 5 deletions

View File

@@ -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

View File

@@ -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,16 @@ 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)
path = os.path.normpath(path)
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)