summaryrefslogtreecommitdiff
path: root/cloudinit/mergers
diff options
context:
space:
mode:
authorzdc <zdc@users.noreply.github.com>2022-03-26 15:41:59 +0200
committerGitHub <noreply@github.com>2022-03-26 15:41:59 +0200
commitaa60d48c2711cdcd9f88a4e5c77379adb0408231 (patch)
tree349631a02467dae0158f6f663cc8aa8537974a97 /cloudinit/mergers
parent5c4b3943343a85fbe517e5ec1fc670b3a8566b4b (diff)
parent31448cccedd8f841fb3ac7d0f2e3cdefe08a53ba (diff)
downloadvyos-cloud-init-aa60d48c2711cdcd9f88a4e5c77379adb0408231.tar.gz
vyos-cloud-init-aa60d48c2711cdcd9f88a4e5c77379adb0408231.zip
Merge pull request #51 from zdc/T2117-sagitta-22.1
T2117: Cloud-init updated to 22.1
Diffstat (limited to 'cloudinit/mergers')
-rw-r--r--cloudinit/mergers/__init__.py43
-rw-r--r--cloudinit/mergers/m_dict.py34
-rw-r--r--cloudinit/mergers/m_list.py37
-rw-r--r--cloudinit/mergers/m_str.py5
4 files changed, 70 insertions, 49 deletions
diff --git a/cloudinit/mergers/__init__.py b/cloudinit/mergers/__init__.py
index 7fa493a6..a7a6a47f 100644
--- a/cloudinit/mergers/__init__.py
+++ b/cloudinit/mergers/__init__.py
@@ -6,14 +6,13 @@
import re
-from cloudinit import importer
-from cloudinit import type_utils
+from cloudinit import importer, type_utils
NAME_MTCH = re.compile(r"(^[a-zA-Z_][A-Za-z0-9_]*)\((.*?)\)$")
DEF_MERGE_TYPE = "list()+dict()+str()"
-MERGER_PREFIX = 'm_'
-MERGER_ATTR = 'Merger'
+MERGER_PREFIX = "m_"
+MERGER_ATTR = "Merger"
class UnknownMerger(object):
@@ -53,7 +52,7 @@ class LookupMerger(UnknownMerger):
self._lookups = lookups
def __str__(self):
- return 'LookupMerger: (%s)' % (len(self._lookups))
+ return "LookupMerger: (%s)" % (len(self._lookups))
# For items which can not be merged by the parent this object
# will lookup in a internally maintained set of objects and
@@ -69,25 +68,26 @@ class LookupMerger(UnknownMerger):
meth = getattr(merger, meth_wanted)
break
if not meth:
- return UnknownMerger._handle_unknown(self, meth_wanted,
- value, merge_with)
+ return UnknownMerger._handle_unknown(
+ self, meth_wanted, value, merge_with
+ )
return meth(value, merge_with)
def dict_extract_mergers(config):
parsed_mergers = []
- raw_mergers = config.pop('merge_how', None)
+ raw_mergers = config.pop("merge_how", None)
if raw_mergers is None:
- raw_mergers = config.pop('merge_type', None)
+ raw_mergers = config.pop("merge_type", None)
if raw_mergers is None:
return parsed_mergers
if isinstance(raw_mergers, str):
return string_extract_mergers(raw_mergers)
for m in raw_mergers:
if isinstance(m, (dict)):
- name = m['name']
+ name = m["name"]
name = name.replace("-", "_").strip()
- opts = m['settings']
+ opts = m["settings"]
else:
name = m[0]
if len(m) >= 2:
@@ -110,8 +110,9 @@ def string_extract_mergers(merge_how):
continue
match = NAME_MTCH.match(m_name)
if not match:
- msg = ("Matcher identifer '%s' is not in the right format" %
- (m_name))
+ msg = "Matcher identifer '%s' is not in the right format" % (
+ m_name
+ )
raise ValueError(msg)
(m_name, m_ops) = match.groups()
m_ops = m_ops.strip().split(",")
@@ -129,14 +130,15 @@ def construct(parsed_mergers):
for (m_name, m_ops) in parsed_mergers:
if not m_name.startswith(MERGER_PREFIX):
m_name = MERGER_PREFIX + str(m_name)
- merger_locs, looked_locs = importer.find_module(m_name,
- [__name__],
- [MERGER_ATTR])
+ merger_locs, looked_locs = importer.find_module(
+ m_name, [__name__], [MERGER_ATTR]
+ )
if not merger_locs:
- msg = ("Could not find merger module named '%s' "
- "with attribute '%s' (searched %s)") % (m_name,
- MERGER_ATTR,
- looked_locs)
+ msg = (
+ "Could not find merger module named '%s' "
+ "with attribute '%s' (searched %s)"
+ % (m_name, MERGER_ATTR, looked_locs)
+ )
raise ImportError(msg)
else:
mod = importer.import_module(merger_locs[0])
@@ -149,4 +151,5 @@ def construct(parsed_mergers):
mergers.append(attr(root, opts))
return root
+
# vi: ts=4 expandtab
diff --git a/cloudinit/mergers/m_dict.py b/cloudinit/mergers/m_dict.py
index 93472f13..274ccafc 100644
--- a/cloudinit/mergers/m_dict.py
+++ b/cloudinit/mergers/m_dict.py
@@ -4,8 +4,11 @@
#
# This file is part of cloud-init. See LICENSE file for license information.
-DEF_MERGE_TYPE = 'no_replace'
-MERGE_TYPES = ('replace', DEF_MERGE_TYPE,)
+DEF_MERGE_TYPE = "no_replace"
+MERGE_TYPES = (
+ "replace",
+ DEF_MERGE_TYPE,
+)
def _has_any(what, *keys):
@@ -25,21 +28,27 @@ class Merger(object):
self._method = m
break
# Affect how recursive merging is done on other primitives.
- self._recurse_str = 'recurse_str' in opts
- self._recurse_array = _has_any(opts, 'recurse_array', 'recurse_list')
- self._allow_delete = 'allow_delete' in opts
+ self._recurse_str = "recurse_str" in opts
+ self._recurse_array = _has_any(opts, "recurse_array", "recurse_list")
+ self._allow_delete = "allow_delete" in opts
# Backwards compat require this to be on.
self._recurse_dict = True
def __str__(self):
- s = ('DictMerger: (method=%s,recurse_str=%s,'
- 'recurse_dict=%s,recurse_array=%s,allow_delete=%s)')
- s = s % (self._method, self._recurse_str,
- self._recurse_dict, self._recurse_array, self._allow_delete)
+ s = (
+ "DictMerger: (method=%s,recurse_str=%s,"
+ "recurse_dict=%s,recurse_array=%s,allow_delete=%s)"
+ )
+ s = s % (
+ self._method,
+ self._recurse_str,
+ self._recurse_dict,
+ self._recurse_array,
+ self._allow_delete,
+ )
return s
def _do_dict_replace(self, value, merge_with, do_replace):
-
def merge_same_key(old_v, new_v):
if do_replace:
return new_v
@@ -65,12 +74,13 @@ class Merger(object):
def _on_dict(self, value, merge_with):
if not isinstance(merge_with, (dict)):
return value
- if self._method == 'replace':
+ if self._method == "replace":
merged = self._do_dict_replace(dict(value), merge_with, True)
- elif self._method == 'no_replace':
+ elif self._method == "no_replace":
merged = self._do_dict_replace(dict(value), merge_with, False)
else:
raise NotImplementedError("Unknown merge type %s" % (self._method))
return merged
+
# vi: ts=4 expandtab
diff --git a/cloudinit/mergers/m_list.py b/cloudinit/mergers/m_list.py
index 19f32771..9dfae8cd 100644
--- a/cloudinit/mergers/m_list.py
+++ b/cloudinit/mergers/m_list.py
@@ -4,8 +4,8 @@
#
# This file is part of cloud-init. See LICENSE file for license information.
-DEF_MERGE_TYPE = 'replace'
-MERGE_TYPES = ('append', 'prepend', DEF_MERGE_TYPE, 'no_replace')
+DEF_MERGE_TYPE = "replace"
+MERGE_TYPES = ("append", "prepend", DEF_MERGE_TYPE, "no_replace")
def _has_any(what, *keys):
@@ -25,38 +25,44 @@ class Merger(object):
self._method = m
break
# Affect how recursive merging is done on other primitives
- self._recurse_str = _has_any(opts, 'recurse_str')
- self._recurse_dict = _has_any(opts, 'recurse_dict')
- self._recurse_array = _has_any(opts, 'recurse_array', 'recurse_list')
+ self._recurse_str = _has_any(opts, "recurse_str")
+ self._recurse_dict = _has_any(opts, "recurse_dict")
+ self._recurse_array = _has_any(opts, "recurse_array", "recurse_list")
def __str__(self):
- return ('ListMerger: (method=%s,recurse_str=%s,'
- 'recurse_dict=%s,recurse_array=%s)') % (self._method,
- self._recurse_str,
- self._recurse_dict,
- self._recurse_array)
+ return (
+ "ListMerger: (method=%s,recurse_str=%s,"
+ "recurse_dict=%s,recurse_array=%s)"
+ % (
+ self._method,
+ self._recurse_str,
+ self._recurse_dict,
+ self._recurse_array,
+ )
+ )
def _on_tuple(self, value, merge_with):
return tuple(self._on_list(list(value), merge_with))
def _on_list(self, value, merge_with):
- if (self._method == 'replace' and
- not isinstance(merge_with, (tuple, list))):
+ if self._method == "replace" and not isinstance(
+ merge_with, (tuple, list)
+ ):
return merge_with
# Ok we now know that what we are merging with is a list or tuple.
merged_list = []
- if self._method == 'prepend':
+ if self._method == "prepend":
merged_list.extend(merge_with)
merged_list.extend(value)
return merged_list
- elif self._method == 'append':
+ elif self._method == "append":
merged_list.extend(value)
merged_list.extend(merge_with)
return merged_list
def merge_same_index(old_v, new_v):
- if self._method == 'no_replace':
+ if self._method == "no_replace":
# Leave it be...
return old_v
if isinstance(new_v, (list, tuple)) and self._recurse_array:
@@ -74,4 +80,5 @@ class Merger(object):
merged_list[i] = merge_same_index(merged_list[i], merge_with[i])
return merged_list
+
# vi: ts=4 expandtab
diff --git a/cloudinit/mergers/m_str.py b/cloudinit/mergers/m_str.py
index 539e3e29..a96bae5e 100644
--- a/cloudinit/mergers/m_str.py
+++ b/cloudinit/mergers/m_str.py
@@ -7,10 +7,10 @@
class Merger(object):
def __init__(self, _merger, opts):
- self._append = 'append' in opts
+ self._append = "append" in opts
def __str__(self):
- return 'StringMerger: (append=%s)' % (self._append)
+ return "StringMerger: (append=%s)" % (self._append)
# On encountering a unicode object to merge value with
# we will for now just proxy into the string method to let it handle it.
@@ -27,4 +27,5 @@ class Merger(object):
return merge_with
return value + merge_with
+
# vi: ts=4 expandtab