mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-11 07:54:15 +00:00
adding dotfile specific variables for templating
This commit is contained in:
@@ -51,36 +51,28 @@ class Templategen:
|
||||
self.env.globals['exists'] = jhelpers.exists
|
||||
self.env.globals['exists_in_path'] = jhelpers.exists_in_path
|
||||
|
||||
def generate(self, src, tmpvars={}):
|
||||
def generate(self, src):
|
||||
"""render template from path"""
|
||||
if not os.path.exists(src):
|
||||
return ''
|
||||
saved = self._patch_globals(tmpvars)
|
||||
ret = self._handle_file(src)
|
||||
self._restore_globals(saved)
|
||||
return ret
|
||||
return self._handle_file(src)
|
||||
|
||||
def generate_string(self, string, tmpvars={}):
|
||||
def generate_string(self, string):
|
||||
"""render template from string"""
|
||||
if not string:
|
||||
return ''
|
||||
saved = self._patch_globals(tmpvars)
|
||||
if self.debug:
|
||||
self.log.dbg('new vars: {}'.format(tmpvars))
|
||||
ret = self.env.from_string(string).render()
|
||||
self._restore_globals(saved)
|
||||
return ret
|
||||
return self.env.from_string(string).render()
|
||||
|
||||
def _patch_globals(self, newvars={}):
|
||||
"""add vars to the globals, make sure to call _restore_globals"""
|
||||
def add_tmp_vars(self, newvars={}):
|
||||
"""add vars to the globals, make sure to call restore_vars"""
|
||||
saved_globals = self.env.globals.copy()
|
||||
if not newvars:
|
||||
return saved_globals
|
||||
self.env.globals.update(newvars)
|
||||
return saved_globals
|
||||
|
||||
def _restore_globals(self, saved_globals):
|
||||
"""restore globals from _patch_globals"""
|
||||
def restore_vars(self, saved_globals):
|
||||
"""restore globals from add_tmp_vars"""
|
||||
self.env.globals = saved_globals.copy()
|
||||
|
||||
def update_variables(self, variables):
|
||||
|
||||
Reference in New Issue
Block a user