1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-10 05:44:17 +00:00

fix templatenotfound for #226

This commit is contained in:
deadc0de6
2020-05-03 14:40:37 +02:00
parent 6ade21d7b6
commit 9f27c6bc48
3 changed files with 37 additions and 5 deletions

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,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)