From e0bbef6fb2ef0fffda7da2e018ba807d10137f10 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Tue, 11 Jun 2019 12:34:31 +0200 Subject: [PATCH] trans_r and trans_w are not list anymore --- dotdrop/cfg_aggregator.py | 17 ++++++++++------- dotdrop/dotdrop.py | 22 +++++++++++----------- dotdrop/dotfile.py | 20 +++----------------- tests/test_update.py | 2 +- 4 files changed, 25 insertions(+), 36 deletions(-) diff --git a/dotdrop/cfg_aggregator.py b/dotdrop/cfg_aggregator.py index 4074fa8..747a2fb 100644 --- a/dotdrop/cfg_aggregator.py +++ b/dotdrop/cfg_aggregator.py @@ -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) diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index 98d442c..77ff1df 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -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 diff --git a/dotdrop/dotfile.py b/dotdrop/dotfile.py index 333ce74..0f3d5e8 100644 --- a/dotdrop/dotfile.py +++ b/dotdrop/dotfile.py @@ -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) diff --git a/tests/test_update.py b/tests/test_update.py index 885fbd6..81eb98f 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -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]