mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-04 19:09:44 +00:00
better handle variables and raise exception when an used variable is undefined
This commit is contained in:
@@ -7,7 +7,8 @@ jinja2 template generator
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
from jinja2 import Environment, FileSystemLoader, \
|
from jinja2 import Environment, FileSystemLoader, \
|
||||||
ChoiceLoader, FunctionLoader, TemplateNotFound
|
ChoiceLoader, FunctionLoader, TemplateNotFound, \
|
||||||
|
StrictUndefined
|
||||||
|
|
||||||
# local imports
|
# local imports
|
||||||
import dotdrop.utils as utils
|
import dotdrop.utils as utils
|
||||||
@@ -36,6 +37,7 @@ class Templategen:
|
|||||||
self.base = base.rstrip(os.sep)
|
self.base = base.rstrip(os.sep)
|
||||||
self.debug = debug
|
self.debug = debug
|
||||||
self.log = Logger()
|
self.log = Logger()
|
||||||
|
self.variables = {}
|
||||||
loader1 = FileSystemLoader(self.base)
|
loader1 = FileSystemLoader(self.base)
|
||||||
loader2 = FunctionLoader(self._template_loader)
|
loader2 = FunctionLoader(self._template_loader)
|
||||||
loader = ChoiceLoader([loader1, loader2])
|
loader = ChoiceLoader([loader1, loader2])
|
||||||
@@ -47,11 +49,14 @@ class Templategen:
|
|||||||
variable_start_string=VAR_START,
|
variable_start_string=VAR_START,
|
||||||
variable_end_string=VAR_END,
|
variable_end_string=VAR_END,
|
||||||
comment_start_string=COMMENT_START,
|
comment_start_string=COMMENT_START,
|
||||||
comment_end_string=COMMENT_END)
|
comment_end_string=COMMENT_END,
|
||||||
|
undefined=StrictUndefined)
|
||||||
|
|
||||||
# adding variables
|
# adding variables
|
||||||
self.env.globals['env'] = os.environ
|
self.variables['env'] = os.environ
|
||||||
if variables:
|
if variables:
|
||||||
self.env.globals.update(variables)
|
self.variables.update(variables)
|
||||||
|
|
||||||
# adding header method
|
# adding header method
|
||||||
self.env.globals['header'] = self._header
|
self.env.globals['header'] = self._header
|
||||||
# adding helper methods
|
# adding helper methods
|
||||||
@@ -72,32 +77,40 @@ class Templategen:
|
|||||||
self._debug_dict('template additional variables', variables)
|
self._debug_dict('template additional variables', variables)
|
||||||
|
|
||||||
def generate(self, src):
|
def generate(self, src):
|
||||||
"""render template from path"""
|
"""
|
||||||
|
render template from path
|
||||||
|
may raise a jinja2.exceptions.UndefinedError
|
||||||
|
in case a variable is undefined
|
||||||
|
"""
|
||||||
if not os.path.exists(src):
|
if not os.path.exists(src):
|
||||||
return ''
|
return ''
|
||||||
return self._handle_file(src)
|
return self._handle_file(src)
|
||||||
|
|
||||||
def generate_string(self, string):
|
def generate_string(self, string):
|
||||||
"""render template from string"""
|
"""
|
||||||
|
render template from string
|
||||||
|
may raise a jinja2.exceptions.UndefinedError
|
||||||
|
in case a variable is undefined
|
||||||
|
"""
|
||||||
if not string:
|
if not string:
|
||||||
return ''
|
return ''
|
||||||
return self.env.from_string(string).render()
|
return self.env.from_string(string).render(self.variables)
|
||||||
|
|
||||||
def add_tmp_vars(self, newvars={}):
|
def add_tmp_vars(self, newvars={}):
|
||||||
"""add vars to the globals, make sure to call restore_vars"""
|
"""add vars to the globals, make sure to call restore_vars"""
|
||||||
saved_globals = self.env.globals.copy()
|
saved_variables = self.variables.copy()
|
||||||
if not newvars:
|
if not newvars:
|
||||||
return saved_globals
|
return saved_variables
|
||||||
self.env.globals.update(newvars)
|
self.variables.update(newvars)
|
||||||
return saved_globals
|
return saved_variables
|
||||||
|
|
||||||
def restore_vars(self, saved_globals):
|
def restore_vars(self, saved_globals):
|
||||||
"""restore globals from add_tmp_vars"""
|
"""restore globals from add_tmp_vars"""
|
||||||
self.env.globals = saved_globals.copy()
|
self.variables = saved_globals.copy()
|
||||||
|
|
||||||
def update_variables(self, variables):
|
def update_variables(self, variables):
|
||||||
"""update variables"""
|
"""update variables"""
|
||||||
self.env.globals.update(variables)
|
self.variables.update(variables)
|
||||||
|
|
||||||
def _load_path_to_dic(self, path, dic):
|
def _load_path_to_dic(self, path, dic):
|
||||||
mod = utils.get_module_from_path(path)
|
mod = utils.get_module_from_path(path)
|
||||||
@@ -160,7 +173,7 @@ class Templategen:
|
|||||||
template_rel_path = os.path.relpath(src, self.base)
|
template_rel_path = os.path.relpath(src, self.base)
|
||||||
try:
|
try:
|
||||||
template = self.env.get_template(template_rel_path)
|
template = self.env.get_template(template_rel_path)
|
||||||
content = template.render()
|
content = template.render(self.variables)
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
data = self._read_bad_encoded_text(src)
|
data = self._read_bad_encoded_text(src)
|
||||||
content = self.generate_string(data)
|
content = self.generate_string(data)
|
||||||
|
|||||||
Reference in New Issue
Block a user