summaryrefslogtreecommitdiff
path: root/cloudinit
diff options
context:
space:
mode:
Diffstat (limited to 'cloudinit')
-rw-r--r--cloudinit/mergers/__init__.py2
-rw-r--r--cloudinit/mergers/m_dict.py27
-rw-r--r--cloudinit/mergers/m_list.py9
-rw-r--r--cloudinit/mergers/m_str.py44
4 files changed, 69 insertions, 13 deletions
diff --git a/cloudinit/mergers/__init__.py b/cloudinit/mergers/__init__.py
index 221e93b5..0978b2c6 100644
--- a/cloudinit/mergers/__init__.py
+++ b/cloudinit/mergers/__init__.py
@@ -25,7 +25,7 @@ from cloudinit import type_utils
NAME_MTCH = re.compile(r"(^[a-zA-Z_][A-Za-z0-9_]*)\((.*?)\)$")
LOG = logging.getLogger(__name__)
-DEF_MERGE_TYPE = "list()+dict()"
+DEF_MERGE_TYPE = "list()+dict()+str()"
MERGER_PREFIX = 'm_'
MERGER_ATTR = 'Merger'
diff --git a/cloudinit/mergers/m_dict.py b/cloudinit/mergers/m_dict.py
index 2c1c845f..82caa004 100644
--- a/cloudinit/mergers/m_dict.py
+++ b/cloudinit/mergers/m_dict.py
@@ -16,21 +16,32 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+DEF_MERGE_TYPE = 'no_replace'
+MERGE_TYPES = ('replace', DEF_MERGE_TYPE,)
+
+
+def _has_any(what, *keys):
+ for k in keys:
+ if k in what:
+ return True
+ return False
+
class Merger(object):
def __init__(self, merger, opts):
self._merger = merger
# Affects merging behavior...
- self._method = 'replace'
- for m in ['replace', 'no_replace']:
+ self._method = DEF_MERGE_TYPE
+ for m in MERGE_TYPES:
if m in opts:
self._method = m
break
- # Affect how recursive merging is done on other primitives
+ # Affect how recursive merging is done on other primitives.
self._recurse_str = 'recurse_str' in opts
- self._recurse_dict = True
- self._recurse_array = 'recurse_array' 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,'
@@ -42,14 +53,14 @@ class Merger(object):
self._allow_delete)
return s
- def _do_dict_replace(self, value, merge_with, do_replace=True):
+ def _do_dict_replace(self, value, merge_with, do_replace):
def merge_same_key(old_v, new_v):
if do_replace:
return new_v
if isinstance(new_v, (list, tuple)) and self._recurse_array:
return self._merger.merge(old_v, new_v)
- if isinstance(new_v, (str, basestring)) and self._recurse_str:
+ if isinstance(new_v, (basestring)) and self._recurse_str:
return self._merger.merge(old_v, new_v)
if isinstance(new_v, (dict)) and self._recurse_dict:
return self._merger.merge(old_v, new_v)
@@ -70,7 +81,7 @@ class Merger(object):
if not isinstance(merge_with, (dict)):
return value
if self._method == 'replace':
- merged = self._do_dict_replace(dict(value), merge_with)
+ merged = self._do_dict_replace(dict(value), merge_with, True)
elif self._method == 'no_replace':
merged = self._do_dict_replace(dict(value), merge_with, False)
else:
diff --git a/cloudinit/mergers/m_list.py b/cloudinit/mergers/m_list.py
index c6a23d85..50f279e8 100644
--- a/cloudinit/mergers/m_list.py
+++ b/cloudinit/mergers/m_list.py
@@ -32,10 +32,11 @@ class Merger(object):
self._recurse_array = 'recurse_array' in opts
def __str__(self):
- return 'ListMerger: (m=%s,rs=%s,rd=%s,ra=%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))
diff --git a/cloudinit/mergers/m_str.py b/cloudinit/mergers/m_str.py
new file mode 100644
index 00000000..e22ce28a
--- /dev/null
+++ b/cloudinit/mergers/m_str.py
@@ -0,0 +1,44 @@
+# -*- coding: utf-8 -*-
+# vi: ts=4 expandtab
+#
+# Copyright (C) 2012 Yahoo! Inc.
+#
+# Author: Joshua Harlow <harlowja@yahoo-inc.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3, as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+class Merger(object):
+ def __init__(self, _merger, opts):
+ self._append = 'append' in opts
+
+ def __str__(self):
+ 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.
+ def _on_unicode(self, value, merge_with):
+ return self._on_str(value, merge_with)
+
+ # On encountering a string object to merge with we will
+ # perform the following action, if appending we will
+ # merge them together, otherwise we will just return value.
+ def _on_str(self, value, merge_with):
+ if not isinstance(value, (basestring)):
+ return merge_with
+ if not self._append:
+ return merge_with
+ if isinstance(value, unicode):
+ return value + unicode(merge_with)
+ else:
+ return value + str(merge_with)