1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-11 18:54:00 +00:00

adding ability to use dynamic variables in dotfile src/dst

This commit is contained in:
deadc0de6
2018-11-27 13:15:35 +01:00
parent d569201b0b
commit c45c17184e
5 changed files with 145 additions and 7 deletions

View File

@@ -11,6 +11,7 @@ import shlex
# local import
from dotdrop.dotfile import Dotfile
from dotdrop.templategen import Templategen
from dotdrop.logger import Logger
from dotdrop.action import Action, Transform
from dotdrop.utils import *
@@ -110,6 +111,15 @@ class Cfg:
if not self._load_file():
raise ValueError('config is not valid')
def eval_dotfiles(self, profile, debug=False):
"""resolve dotfiles src/dst templates"""
t = Templategen(profile=profile,
variables=self.get_variables(),
debug=debug)
for d in self.get_dotfiles(profile):
d.src = t.generate_string(d.src)
d.dst = t.generate_string(d.dst)
def _load_file(self):
"""load the yaml file"""
with open(self.cfgpath, 'r') as f:

View File

@@ -90,7 +90,7 @@ def cmd_install(opts, conf, temporary=False, keys=[]):
LOG.warn(msg.format(opts['profile']))
return False
t = Templategen(opts['profile'], base=opts['dotpath'],
t = Templategen(profile=opts['profile'], base=opts['dotpath'],
variables=opts['variables'], debug=opts['debug'])
tmpdir = None
if temporary:
@@ -158,7 +158,7 @@ def cmd_compare(opts, conf, tmp, focus=[], ignore=[]):
if len(selected) < 1:
return False
t = Templategen(opts['profile'], base=opts['dotpath'],
t = Templategen(profile=opts['profile'], base=opts['dotpath'],
variables=opts['variables'], debug=opts['debug'])
inst = Installer(create=opts['create'], backup=opts['backup'],
dry=opts['dry'], base=opts['dotpath'],
@@ -439,6 +439,9 @@ def main():
LOG.dbg('config file: {}'.format(args['--cfg']))
LOG.dbg('opts: {}'.format(opts))
# resolve dynamic paths
conf.eval_dotfiles(opts['profile'], debug=opts['debug'])
if ENV_NOBANNER not in os.environ \
and opts['banner'] \
and not args['--no-banner']:

View File

@@ -23,7 +23,7 @@ COMMENT_END = '@@#}'
class Templategen:
def __init__(self, profile, base='.', variables={}, debug=False):
def __init__(self, profile='', base='.', variables={}, debug=False):
self.base = base.rstrip(os.sep)
self.debug = debug
self.log = Logger()
@@ -39,7 +39,8 @@ class Templategen:
comment_end_string=COMMENT_END)
# adding variables
self.env.globals['env'] = os.environ
self.env.globals['profile'] = profile
if profile:
self.env.globals['profile'] = profile
self.env.globals.update(variables)
# adding header method
self.env.globals['header'] = self._header
@@ -47,10 +48,17 @@ class Templategen:
self.env.globals['exists'] = jhelpers.exists
def generate(self, src):
"""render template from path"""
if not os.path.exists(src):
return ''
return self._handle_file(src)
def generate_string(self, string):
"""render template from string"""
if not string:
return ''
return self.env.from_string(string).render()
def _header(self, prepend=''):
"""add a comment usually in the header of a dotfile"""
return '{}{}'.format(prepend, utils.header())