1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-10 09:14:16 +00:00

implement recursive variables for #83

This commit is contained in:
deadc0de6
2019-02-02 20:16:44 +01:00
parent 45f29679b2
commit 8e9f1b2468
5 changed files with 197 additions and 15 deletions

View File

@@ -119,9 +119,8 @@ class Cfg:
raise ValueError('config is not valid')
def eval_dotfiles(self, profile, debug=False):
"""resolve dotfiles src/dst templating"""
t = Templategen(variables=self.get_variables(profile),
debug=debug)
"""resolve dotfiles src/dst templating for this profile"""
t = Templategen(variables=self.get_variables(profile, debug=debug))
for d in self.get_dotfiles(profile):
d.src = t.generate_string(d.src)
d.dst = t.generate_string(d.dst)
@@ -622,8 +621,44 @@ class Cfg:
"""return all defined settings"""
return self.lnk_settings.copy()
def get_variables(self, profile):
def get_variables(self, profile, debug=False):
"""return the variables for this profile"""
# get flat variables
variables = self._get_variables(profile)
# get interpreted variables
dvariables = self._get_dynvariables(profile)
# recursive resolve variables
allvars = variables.copy()
allvars.update(dvariables)
var = self._rec_resolve_vars(allvars)
# execute dynvariables
for k in dvariables.keys():
var[k] = shell(var[k])
if debug:
self.log.dbg('variables:')
for k, v in var.items():
self.log.dbg('\t\"{}\": {}'.format(k, v))
return var
def _rec_resolve_vars(self, variables):
"""recursive resolve all variables"""
t = Templategen(variables=variables)
for k in variables.keys():
val = variables[k]
while Templategen.var_is_template(val):
val = t.generate_string(val)
variables[k] = val
t.update_variables(variables)
return variables
def _get_variables(self, profile):
"""return the flat variables"""
variables = {}
# profile variable
@@ -633,13 +668,6 @@ class Cfg:
if self.key_variables in self.content:
variables.update(self.content[self.key_variables])
# global dynvariables
if self.key_dynvariables in self.content:
# interpret dynamic variables
dynvars = self.content[self.key_dynvariables]
for k, v in dynvars.items():
variables[k] = shell(v)
if profile not in self.lnk_profiles:
return variables
@@ -649,10 +677,24 @@ class Cfg:
for k, v in var[self.key_variables].items():
variables[k] = v
return variables
def _get_dynvariables(self, profile):
"""return the dyn variables"""
variables = {}
# global dynvariables
if self.key_dynvariables in self.content:
# interpret dynamic variables
variables.update(self.content[self.key_dynvariables])
if profile not in self.lnk_profiles:
return variables
# profile dynvariables
var = self.lnk_profiles[profile]
if self.key_dynvariables in var.keys():
for k, v in var[self.key_dynvariables].items():
variables[k] = shell(v)
variables.update(var[self.key_dynvariables])
return variables

View File

@@ -449,7 +449,8 @@ def main():
opts['link'] = LinkTypes.NOLINK
opts['debug'] = args['--verbose']
opts['variables'] = conf.get_variables(opts['profile'])
opts['variables'] = conf.get_variables(opts['profile'],
debug=opts['debug'])
opts['showdiff'] = opts['showdiff'] or args['--showdiff']
if opts['debug']:

View File

@@ -57,6 +57,10 @@ class Templategen:
return ''
return self.env.from_string(string).render()
def update_variables(self, variables):
"""update variables"""
self.env.globals.update(variables)
def _header(self, prepend=''):
"""add a comment usually in the header of a dotfile"""
return '{}{}'.format(prepend, utils.header())
@@ -125,6 +129,11 @@ class Templategen:
return True
return False
@staticmethod
def var_is_template(string):
"""check if variable contains template(s)"""
return VAR_START in str(string)
@staticmethod
def _is_template(path):
"""test if file pointed by path is a template"""