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

trans_r and trans_w are not list anymore

This commit is contained in:
deadc0de6
2019-06-11 12:34:31 +02:00
parent d6d5ea2ccf
commit e0bbef6fb2
4 changed files with 25 additions and 36 deletions

View File

@@ -102,15 +102,14 @@ class CfgAggregator:
# patch trans_w/trans_r in dotfiles
self._patch_keys_to_objs(self.dotfiles,
"trans_r", self._get_trans_r)
"trans_r", self._get_trans_r, islist=False)
self._patch_keys_to_objs(self.dotfiles,
"trans_w", self._get_trans_w)
"trans_w", self._get_trans_w, islist=False)
def _patch_keys_to_objs(self, containers, keys, get_by_key):
def _patch_keys_to_objs(self, containers, keys, get_by_key, islist=True):
"""
patch each object in "containers" containing
a list of keys in the attribute "keys" with
the returned object of the function "get_by_key"
map for each key in the attribute 'keys' in 'containers'
the returned object from the method 'get_by_key'
"""
if not containers:
return
@@ -121,13 +120,17 @@ class CfgAggregator:
okeys = getattr(c, keys)
if not okeys:
continue
if not islist:
okeys = [okeys]
for k in okeys:
o = get_by_key(k)
if not o:
err = 'bad {} key for \"{}\": {}'.format(keys, c.key, k)
err = 'bad {} key for \"{}\": {}'.format(keys, c, k)
self.log.err(err)
raise Exception(err)
objects.append(o)
if not islist:
objects = objects[0]
if self.debug:
self.log.dbg('patching {}.{} with {}'.format(c, keys, objects))
setattr(c, keys, objects)

View File

@@ -545,17 +545,17 @@ def apply_trans(dotpath, dotfile, debug=False):
"""
src = dotfile.src
new_src = '{}.{}'.format(src, TRANS_SUFFIX)
for trans in dotfile.trans_r:
if debug:
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):
msg = 'transformation \"{}\" failed for {}'
LOG.err(msg.format(trans.key, dotfile.key))
if new_src and os.path.exists(new_src):
remove(new_src)
return None
trans = dotfile.trans_r
if debug:
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):
msg = 'transformation \"{}\" failed for {}'
LOG.err(msg.format(trans.key, dotfile.key))
if new_src and os.path.exists(new_src):
remove(new_src)
return None
return new_src

View File

@@ -18,7 +18,7 @@ class Dotfile(DictParser):
key_trans_w = 'trans_write'
def __init__(self, key, dst, src,
actions=[], trans_r=[], trans_w=[],
actions=[], trans_r=None, trans_w=None,
link=LinkTypes.NOLINK, cmpignore=[],
noempty=False, upignore=[]):
"""
@@ -42,11 +42,7 @@ class Dotfile(DictParser):
self.noempty = noempty
self.src = src
self.trans_r = trans_r
if trans_r and len(self.trans_r) > 1:
raise Exception('only one trans_read allowed')
self.trans_w = trans_w
if trans_w and len(self.trans_w) > 1:
raise Exception('only one trans_write allowed')
self.upignore = upignore
if link != LinkTypes.NOLINK and \
@@ -80,28 +76,18 @@ class Dotfile(DictParser):
def get_trans_r(self):
"""return trans_r object"""
if self.trans_r:
return self.trans_r[0]
return None
return self.trans_r
def get_trans_w(self):
"""return trans_w object"""
if self.trans_w:
return self.trans_w[0]
return None
return self.trans_w
@classmethod
def _adjust_yaml_keys(cls, value):
"""patch dict"""
value['noempty'] = value.get(cls.key_noempty, False)
value['trans_r'] = value.get(cls.key_trans_r)
if value['trans_r']:
# ensure is a list
value['trans_r'] = [value['trans_r']]
value['trans_w'] = value.get(cls.key_trans_w)
if value['trans_w']:
# ensure is a list
value['trans_w'] = [value['trans_w']]
# remove old entries
value.pop(cls.key_noempty, None)
value.pop(cls.key_trans_r, None)

View File

@@ -118,7 +118,7 @@ class TestUpdate(unittest.TestCase):
# retrieve the path of the sub in the dotpath
d1indotpath = os.path.join(o.dotpath, dotfile.src)
d1indotpath = os.path.expanduser(d1indotpath)
dotfile.trans_w = [trans]
dotfile.trans_w = trans
# update template
o.update_path = [d3t]