1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-10 02:19:14 +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 # patch trans_w/trans_r in dotfiles
self._patch_keys_to_objs(self.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, 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 map for each key in the attribute 'keys' in 'containers'
a list of keys in the attribute "keys" with the returned object from the method 'get_by_key'
the returned object of the function "get_by_key"
""" """
if not containers: if not containers:
return return
@@ -121,13 +120,17 @@ class CfgAggregator:
okeys = getattr(c, keys) okeys = getattr(c, keys)
if not okeys: if not okeys:
continue continue
if not islist:
okeys = [okeys]
for k in okeys: for k in okeys:
o = get_by_key(k) o = get_by_key(k)
if not o: if not o:
err = 'bad {} key for \"{}\": {}'.format(keys, c.key, k) err = 'bad {} key for \"{}\": {}'.format(keys, c, k)
self.log.err(err) self.log.err(err)
raise Exception(err) raise Exception(err)
objects.append(o) objects.append(o)
if not islist:
objects = objects[0]
if self.debug: if self.debug:
self.log.dbg('patching {}.{} with {}'.format(c, keys, objects)) self.log.dbg('patching {}.{} with {}'.format(c, keys, objects))
setattr(c, keys, objects) setattr(c, keys, objects)

View File

@@ -545,17 +545,17 @@ def apply_trans(dotpath, dotfile, debug=False):
""" """
src = dotfile.src src = dotfile.src
new_src = '{}.{}'.format(src, TRANS_SUFFIX) new_src = '{}.{}'.format(src, TRANS_SUFFIX)
for trans in dotfile.trans_r: trans = dotfile.trans_r
if debug: if debug:
LOG.dbg('executing transformation {}'.format(trans)) LOG.dbg('executing transformation {}'.format(trans))
s = os.path.join(dotpath, src) s = os.path.join(dotpath, src)
temp = os.path.join(dotpath, new_src) temp = os.path.join(dotpath, new_src)
if not trans.transform(s, temp): if not trans.transform(s, temp):
msg = 'transformation \"{}\" failed for {}' msg = 'transformation \"{}\" failed for {}'
LOG.err(msg.format(trans.key, dotfile.key)) LOG.err(msg.format(trans.key, dotfile.key))
if new_src and os.path.exists(new_src): if new_src and os.path.exists(new_src):
remove(new_src) remove(new_src)
return None return None
return new_src return new_src

View File

@@ -18,7 +18,7 @@ class Dotfile(DictParser):
key_trans_w = 'trans_write' key_trans_w = 'trans_write'
def __init__(self, key, dst, src, def __init__(self, key, dst, src,
actions=[], trans_r=[], trans_w=[], actions=[], trans_r=None, trans_w=None,
link=LinkTypes.NOLINK, cmpignore=[], link=LinkTypes.NOLINK, cmpignore=[],
noempty=False, upignore=[]): noempty=False, upignore=[]):
""" """
@@ -42,11 +42,7 @@ class Dotfile(DictParser):
self.noempty = noempty self.noempty = noempty
self.src = src self.src = src
self.trans_r = trans_r 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 self.trans_w = trans_w
if trans_w and len(self.trans_w) > 1:
raise Exception('only one trans_write allowed')
self.upignore = upignore self.upignore = upignore
if link != LinkTypes.NOLINK and \ if link != LinkTypes.NOLINK and \
@@ -80,28 +76,18 @@ class Dotfile(DictParser):
def get_trans_r(self): def get_trans_r(self):
"""return trans_r object""" """return trans_r object"""
if self.trans_r: return self.trans_r
return self.trans_r[0]
return None
def get_trans_w(self): def get_trans_w(self):
"""return trans_w object""" """return trans_w object"""
if self.trans_w: return self.trans_w
return self.trans_w[0]
return None
@classmethod @classmethod
def _adjust_yaml_keys(cls, value): def _adjust_yaml_keys(cls, value):
"""patch dict""" """patch dict"""
value['noempty'] = value.get(cls.key_noempty, False) value['noempty'] = value.get(cls.key_noempty, False)
value['trans_r'] = value.get(cls.key_trans_r) 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) 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 # remove old entries
value.pop(cls.key_noempty, None) value.pop(cls.key_noempty, None)
value.pop(cls.key_trans_r, 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 # retrieve the path of the sub in the dotpath
d1indotpath = os.path.join(o.dotpath, dotfile.src) d1indotpath = os.path.join(o.dotpath, dotfile.src)
d1indotpath = os.path.expanduser(d1indotpath) d1indotpath = os.path.expanduser(d1indotpath)
dotfile.trans_w = [trans] dotfile.trans_w = trans
# update template # update template
o.update_path = [d3t] o.update_path = [d3t]