1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-07 05:20:43 +00:00

adding dotfile specific variables for templating

This commit is contained in:
deadc0de6
2019-05-02 11:23:52 +02:00
parent d7168285a5
commit ad10adef5d
7 changed files with 122 additions and 35 deletions

View File

@@ -68,12 +68,12 @@ class Action(Cmd):
def __repr__(self):
return 'action({})'.format(self.__str__())
def execute(self, templater=None, newvars={}):
def execute(self, templater=None):
"""execute the action in the shell"""
ret = 1
action = self.action
if templater:
action = templater.generate_string(self.action, tmpvars=newvars)
action = templater.generate_string(self.action)
try:
cmd = action.format(*self.args)
except IndexError:

View File

@@ -175,21 +175,23 @@ class Cfg:
"""resolve dotfiles src/dst/actions templating for this profile"""
t = Templategen(variables=variables)
dotfiles = self._get_dotfiles(profile)
tvars = t.add_tmp_vars()
for d in dotfiles:
# add dotfile variables
t.restore_vars(tvars)
newvar = d.get_vars()
t.add_tmp_vars(newvars=newvar)
# src and dst path
d.src = t.generate_string(d.src)
d.dst = t.generate_string(d.dst)
# pre actions
var = d.get_vars()
if self.key_actions_pre in d.actions:
for action in d.actions[self.key_actions_pre]:
action.action = t.generate_string(action.action,
tmpvars=var)
action.action = t.generate_string(action.action)
# post actions
if self.key_actions_post in d.actions:
for action in d.actions[self.key_actions_post]:
action.action = t.generate_string(action.action,
tmpvars=var)
action.action = t.generate_string(action.action)
return dotfiles
def _load_config(self, profile=None):

View File

@@ -45,8 +45,7 @@ def action_executor(o, dotfile, actions, defactions, templater, post=False):
continue
if o.debug:
LOG.dbg('executing def-{}-action {}'.format(s, action))
newvars = dotfile.get_vars()
ret = action.execute(templater=templater, newvars=newvars)
ret = action.execute(templater=templater)
if not ret:
err = 'def-{}-action \"{}\" failed'.format(s, action.key)
LOG.err(err)
@@ -59,8 +58,7 @@ def action_executor(o, dotfile, actions, defactions, templater, post=False):
continue
if o.debug:
LOG.dbg('executing {}-action {}'.format(s, action))
newvars = dotfile.get_vars()
ret = action.execute(templater=templater, newvars=newvars)
ret = action.execute(templater=templater)
if not ret:
err = '{}-action \"{}\" failed'.format(s, action.key)
LOG.err(err)
@@ -93,7 +91,13 @@ def cmd_install(o):
showdiff=o.install_showdiff,
backup_suffix=o.install_backup_suffix)
installed = 0
tvars = t.add_tmp_vars()
for dotfile in dotfiles:
# add dotfile variables
t.restore_vars(tvars)
newvars = dotfile.get_vars()
t.add_tmp_vars(newvars=newvars)
preactions = []
if not o.install_temporary and dotfile.actions \
and Cfg.key_actions_pre in dotfile.actions:

View File

@@ -43,8 +43,8 @@ class Dotfile:
def get_vars(self):
"""return this dotfile templating vars"""
_vars = {}
_vars['_dotfile_src'] = self.src
_vars['_dotfile_dst'] = self.dst
_vars['_dotfile_abs_src'] = self.src
_vars['_dotfile_abs_dst'] = self.dst
return _vars
def __str__(self):

View File

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