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

adding ability to use variables from the config file in templates

This commit is contained in:
deadc0de6
2018-09-01 18:28:53 +02:00
parent 9f3da3c8b7
commit 8a3e7459f3
7 changed files with 137 additions and 5 deletions

View File

@@ -38,6 +38,9 @@ class Cfg:
# transformations keys
key_trans = 'trans'
# template variables
key_variables = 'variables'
# dotfiles keys
key_dotfiles = 'dotfiles'
key_dotfiles_src = 'src'
@@ -479,6 +482,11 @@ class Cfg:
"""return all defined settings"""
return self.lnk_settings.copy()
def get_variables(self):
if self.key_variables in self.content:
return self.content[self.key_variables]
return {}
def dump(self):
"""return a dump of the config"""
# temporary reset dotpath

View File

@@ -81,7 +81,8 @@ def install(opts, conf):
msg = 'no dotfiles defined for this profile (\"{}\")'
LOG.err(msg.format(opts['profile']))
return False
t = Templategen(base=opts['dotpath'], debug=opts['debug'])
t = Templategen(base=opts['dotpath'], variables=opts['variables'],
debug=opts['debug'])
inst = Installer(create=opts['create'], backup=opts['backup'],
dry=opts['dry'], safe=opts['safe'], base=opts['dotpath'],
diff=opts['installdiff'], debug=opts['debug'])
@@ -184,7 +185,8 @@ def compare(opts, conf, tmp, focus=None, ignore=[]):
if len(selected) < 1:
return False
t = Templategen(base=opts['dotpath'], debug=opts['debug'])
t = Templategen(base=opts['dotpath'], variables=opts['variables'],
debug=opts['debug'])
inst = Installer(create=opts['create'], backup=opts['backup'],
dry=opts['dry'], base=opts['dotpath'],
debug=opts['debug'])
@@ -339,6 +341,7 @@ def main():
opts['installdiff'] = not args['--nodiff']
opts['link'] = args['--link']
opts['debug'] = args['--verbose']
opts['variables'] = conf.get_variables()
if opts['debug']:
LOG.dbg('config file: {}'.format(args['--cfg']))

View File

@@ -22,7 +22,7 @@ COMMENT_END = '@@#}'
class Templategen:
def __init__(self, base='.', debug=False):
def __init__(self, base='.', variables={}, debug=False):
self.base = base.rstrip(os.sep)
self.debug = debug
loader = FileSystemLoader(self.base)
@@ -36,6 +36,7 @@ class Templategen:
comment_start_string=COMMENT_START,
comment_end_string=COMMENT_END)
self.env.globals['header'] = self._header
self.env.globals.update(variables)
self.log = Logger()
def generate(self, src, profile):