summaryrefslogtreecommitdiff
path: root/filter_plugins/ini.py
blob: 89f553cfe0d224295ec5fc9204a0de103e89f325 (plain) (blame)
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
# this is from: https://github.com/sivel/toiletwater/tree/master/plugins/filter
#
# (c) 2017, Matt Martz <matt@sivel.net>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import copy


from functools import partial

from ansible.errors import AnsibleFilterError
from ansible.module_utils._text import to_text
from ansible.module_utils.common._collections_compat import MutableMapping
from ansible.module_utils.six import string_types
from ansible.module_utils.six.moves import configparser, StringIO


def from_ini(o):
    if not isinstance(o, string_types):
        raise AnsibleFilterError('from_ini requires a string, got %s' % type(o))
    parser = configparser.RawConfigParser()
    parser.optionxform = partial(to_text, errors='surrogate_or_strict')
    parser.read_file(StringIO(o))
    d = dict(parser._sections)
    for k in d:
        d[k] = dict(d[k])
        d[k].pop('__name__', None)
    if parser._defaults:
        d['DEFAULT'] = dict(parser._defaults)
    return d


def to_ini(o):
    if not isinstance(o, MutableMapping):
        raise AnsibleFilterError('to_ini requires a dict, got %s' % type(o))
    data = copy.deepcopy(o)
    defaults = configparser.RawConfigParser(data.pop('DEFAULT', {}))
    parser = configparser.RawConfigParser()
    parser.optionxform = partial(to_text, errors='surrogate_or_strict')
    for section, items in data.items():
        parser.add_section(section)
        for k, v in items.items():
            parser.set(section, k, v)
    out = StringIO()
    defaults.write(out)
    parser.write(out)
    return out.getvalue().rstrip()


class FilterModule(object):
    def filters(self):
        return {
            'to_ini': to_ini,
            'from_ini': from_ini
        }