summaryrefslogtreecommitdiff
path: root/lib/ansible/module_utils/netcfg.py
blob: afd8be3a56f3f977c7f67cc0d209094f82021d0e (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
81
82
83
84
85
#
# (c) 2015 Peter Sprygada, <psprygada@ansible.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible 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 Ansible.  If not, see <http://www.gnu.org/licenses/>.
#

import re
import collections

class ConfigLine(object):

    def __init__(self, text):
        self.text = text
        self.children = list()
        self.parents = list()
        self.raw = None

    def __str__(self):
        return self.raw

    def __eq__(self, other):
        if self.text == other.text:
            return self.parents == other.parents

    def __ne__(self, other):
        return not self.__eq__(other)

def parse(lines, indent):
        toplevel = re.compile(r'\S')
        childline = re.compile(r'^\s*(.+)$')
        repl = r'([{|}|;])'

        ancestors = list()
        config = list()

        for line in str(lines).split('\n'):
            text = str(re.sub(repl, '', line)).strip()

            cfg = ConfigLine(text)
            cfg.raw = line

            if not text or text[0] in ['!', '#']:
                continue

            # handle top level commands
            if toplevel.match(line):
                ancestors = [cfg]

            # handle sub level commands
            else:
                match = childline.match(line)
                line_indent = match.start(1)
                level = int(line_indent / indent)
                parent_level = level - 1

                cfg.parents = ancestors[:level]

                if level > len(ancestors):
                    config.append(cfg)
                    continue

                for i in range(level, len(ancestors)):
                    ancestors.pop()

                ancestors.append(cfg)
                ancestors[parent_level].children.append(cfg)

            config.append(cfg)

        return config