summaryrefslogtreecommitdiff
path: root/cloudinit/distros/parsers/quoting_conf.py
blob: 953ccfe911f5a8802f518d382e258801981b7953 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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/>.

# This library is used to parse/write
# out the various sysconfig files edited
#
# It has to be slightly modified though
# to ensure that all values are quoted
# since these configs are usually sourced into
# bash scripts...
from configobj import ConfigObj

# See: http://tiny.cc/oezbgw
D_QUOTE_CHARS = {
    "\"": "\\\"",
    "(": "\\(",
    ")": "\\)",
    "$": '\$',
    '`': '\`',
}

# This class helps adjust the configobj
# writing to ensure that when writing a k/v
# on a line, that they are properly quoted
# and have no spaces between the '=' sign.
# - This is mainly due to the fact that
# the sysconfig scripts are often sourced
# directly into bash/shell scripts so ensure
# that it works for those types of use cases.
class QuotingConfigObj(ConfigObj):
    def __init__(self, lines):
        ConfigObj.__init__(self, lines,
                           interpolation=False,
                           write_empty_values=True)

    def _quote_posix(self, text):
        if not text:
            return ''
        for (k, v) in D_QUOTE_CHARS.iteritems():
            text = text.replace(k, v)
        return '"%s"' % (text)

    def _quote_special(self, text):
        if text.lower() in ['yes', 'no', 'true', 'false']:
            return text
        else:
            return self._quote_posix(text)

    def _write_line(self, indent_string, entry, this_entry, comment):
        # Ensure it is formatted fine for
        # how these sysconfig scripts are used
        val = self._decode_element(self._quote(this_entry))
        # Single quoted strings should
        # always work.
        if not val.startswith("'"):
            # Perform any special quoting
            val = self._quote_special(val)
        key = self._decode_element(self._quote(entry, multiline=False))
        cmnt = self._decode_element(comment)
        return '%s%s%s%s%s' % (indent_string,
                               key,
                               "=",
                               val,
                               cmnt)