1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-06 10:12:59 +00:00

allow transformation to be templated (#177)

This commit is contained in:
deadc0de6
2019-07-10 20:47:16 +02:00
parent a4a831195e
commit 3a4552da55
5 changed files with 170 additions and 12 deletions

View File

@@ -121,14 +121,20 @@ class Action(Cmd):
class Transform(Cmd):
def transform(self, arg0, arg1):
def transform(self, arg0, arg1, templater=None, debug=False):
"""
execute transformation with {0} and {1}
where {0} is the file to transform
and {1} is the result file
"""
ret = 1
cmd = self.action.format(arg0, arg1)
action = self.action
if templater:
action = templater.generate_string(action)
if debug:
self.log.dbg('trans \"{}\" -> \"{}\"'.format(self.action,
action))
cmd = action.format(arg0, arg1)
if os.path.exists(arg1):
msg = 'transformation \"{}\": destination exists: {}'
self.log.warn(msg.format(cmd, arg1))

View File

@@ -133,7 +133,7 @@ def cmd_install(o):
src = dotfile.src
tmp = None
if dotfile.trans_r:
tmp = apply_trans(o.dotpath, dotfile, debug=o.debug)
tmp = apply_trans(o.dotpath, dotfile, t, debug=o.debug)
if not tmp:
continue
src = tmp
@@ -224,7 +224,7 @@ def cmd_compare(o, tmp):
# apply transformation
if o.debug:
LOG.dbg('applying transformation before comparing')
tmpsrc = apply_trans(o.dotpath, dotfile, debug=o.debug)
tmpsrc = apply_trans(o.dotpath, dotfile, t, debug=o.debug)
if not tmpsrc:
# could not apply trans
same = False
@@ -561,7 +561,7 @@ def _select(selections, dotfiles):
return selected
def apply_trans(dotpath, dotfile, debug=False):
def apply_trans(dotpath, dotfile, templater, debug=False):
"""
apply the read transformation to the dotfile
return None if fails and new source if succeed
@@ -573,7 +573,7 @@ def apply_trans(dotpath, dotfile, debug=False):
LOG.dbg('executing transformation {}'.format(trans))
s = os.path.join(dotpath, src)
temp = os.path.join(dotpath, new_src)
if not trans.transform(s, temp):
if not trans.transform(s, temp, templater=templater, debug=debug):
msg = 'transformation \"{}\" failed for {}'
LOG.err(msg.format(trans.key, dotfile.key))
if new_src and os.path.exists(new_src):

View File

@@ -48,6 +48,11 @@ class Updater:
self.debug = debug
self.ignore = ignore
self.showpatch = showpatch
self.templater = Templategen(variables=self.variables,
base=self.dotpath,
debug=self.debug)
# save template vars
self.tvars = self.templater.add_tmp_vars()
self.log = Logger()
def update_path(self, path):
@@ -110,7 +115,11 @@ class Updater:
if self.debug:
self.log.dbg('executing write transformation {}'.format(trans))
tmp = get_unique_tmp_name()
if not trans.transform(path, tmp):
self.templater.restore_vars(self.tvars)
newvars = dotfile.get_dotfile_variables()
self.templater.add_tmp_vars(newvars=newvars)
if not trans.transform(path, tmp, templater=self.templater,
debug=self.debug):
msg = 'transformation \"{}\" failed for {}'
self.log.err(msg.format(trans.key, dotfile.key))
if os.path.exists(tmp):
@@ -136,9 +145,8 @@ class Updater:
def _resolve_template(self, tpath):
"""resolve the template to a temporary file"""
t = Templategen(variables=self.variables, base=self.dotpath,
debug=self.debug)
return t.generate(tpath)
self.templater.restore_vars(self.tvars)
return self.templater.generate(tpath)
def _handle_file(self, path, dtpath, compare=True):
"""sync path (deployed file) and dtpath (dotdrop dotfile path)"""