~cloud-init-dev/cloud-init/trunk

« back to all changes in this revision

Viewing changes to cloudinit/mergers/m_str.py

  • Committer: Scott Moser
  • Date: 2016-08-10 15:06:15 UTC
  • Revision ID: smoser@ubuntu.com-20160810150615-ma2fv107w3suy1ma
README: Mention move of revision control to git.

cloud-init development has moved its revision control to git.
It is available at 
  https://code.launchpad.net/cloud-init

Clone with 
  git clone https://git.launchpad.net/cloud-init
or
  git clone git+ssh://git.launchpad.net/cloud-init

For more information see
  https://git.launchpad.net/cloud-init/tree/HACKING.rst

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
# vi: ts=4 expandtab
3
 
#
4
 
# Copyright (C) 2012 Yahoo! Inc.
5
 
#
6
 
# Author: Joshua Harlow <harlowja@yahoo-inc.com>
7
 
#
8
 
# This program is free software: you can redistribute it and/or modify
9
 
# it under the terms of the GNU General Public License version 3, as
10
 
# published by the Free Software Foundation.
11
 
#
12
 
# This program is distributed in the hope that it will be useful,
13
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 
# GNU General Public License for more details.
16
 
#
17
 
# You should have received a copy of the GNU General Public License
18
 
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
 
 
20
 
import six
21
 
 
22
 
 
23
 
class Merger(object):
24
 
    def __init__(self, _merger, opts):
25
 
        self._append = 'append' in opts
26
 
 
27
 
    def __str__(self):
28
 
        return 'StringMerger: (append=%s)' % (self._append)
29
 
 
30
 
    # On encountering a unicode object to merge value with
31
 
    # we will for now just proxy into the string method to let it handle it.
32
 
    def _on_unicode(self, value, merge_with):
33
 
        return self._on_str(value, merge_with)
34
 
 
35
 
    # On encountering a string object to merge with we will
36
 
    # perform the following action, if appending we will
37
 
    # merge them together, otherwise we will just return value.
38
 
    def _on_str(self, value, merge_with):
39
 
        if not isinstance(value, six.string_types):
40
 
            return merge_with
41
 
        if not self._append:
42
 
            return merge_with
43
 
        if isinstance(value, six.text_type):
44
 
            return value + six.text_type(merge_with)
45
 
        else:
46
 
            return value + six.binary_type(merge_with)