diff options
author | Joshua Harlow <harlowja@gmail.com> | 2012-11-22 19:45:43 -0800 |
---|---|---|
committer | Joshua Harlow <harlowja@gmail.com> | 2012-11-22 19:45:43 -0800 |
commit | 3941466b3e065c9ce7bb7500e41f464993861672 (patch) | |
tree | 152ff7635217238d1ee562fd72df688c4c924599 /cloudinit/mergers/list.py | |
parent | 180e83b20aa02dc9df903fa7e31121dd49a49b3a (diff) | |
download | vyos-cloud-init-3941466b3e065c9ce7bb7500e41f464993861672.tar.gz vyos-cloud-init-3941466b3e065c9ce7bb7500e41f464993861672.zip |
Allow mergers to take options.
Diffstat (limited to 'cloudinit/mergers/list.py')
-rw-r--r-- | cloudinit/mergers/list.py | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/cloudinit/mergers/list.py b/cloudinit/mergers/list.py index ad1b9793..0c65d053 100644 --- a/cloudinit/mergers/list.py +++ b/cloudinit/mergers/list.py @@ -18,8 +18,10 @@ class Merger(object): - def __init__(self, merger): + def __init__(self, merger, opts): self._merger = merger + self._discard_non = 'discard_non_list' in opts + self._append = 'append' in opts def _on_tuple(self, value, merge_with): return self._on_list(list(value), merge_with) @@ -27,15 +29,22 @@ class Merger(object): def _on_list(self, value, merge_with): if isinstance(merge_with, (tuple, list)): new_value = list(value) - for m_v in merge_with: - m_am = 0 - for (i, o_v) in enumerate(new_value): - if m_v == o_v: - new_value[i] = self._merger.merge(o_v, m_v) - m_am += 1 - if m_am == 0: - new_value.append(m_v) + if self._append: + new_value.extend(merge_with) + else: + # Merge instead + for m_v in merge_with: + m_am = 0 + for (i, o_v) in enumerate(new_value): + if m_v == o_v: + new_value[i] = self._merger.merge(o_v, m_v) + m_am += 1 + if m_am == 0: + new_value.append(m_v) else: new_value = list(value) - new_value.append(merge_with) + if self._discard_non: + pass + else: + new_value.append(merge_with) return new_value |