summaryrefslogtreecommitdiff
path: root/reconfigure/parsers/squid.py
blob: b9b4f971e1a39acd97e0018608f24dd7414a2b40 (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
from reconfigure.nodes import *
from reconfigure.parsers import BaseParser


class SquidParser (BaseParser):
    """
    A parser for Squid configs
    """

    def parse(self, content):
        lines = filter(None, [x.strip() for x in content.splitlines()])
        root = RootNode()
        last_comment = None
        for line in lines:
            line = line.strip()
            if line.startswith('#'):
                c = line.strip('#').strip()
                if last_comment:
                    last_comment += '\n' + c
                else:
                    last_comment = c
                continue
            if len(line) == 0:
                continue
            tokens = line.split()
            node = Node('line', Node('arguments'))
            if last_comment:
                node.comment = last_comment
                last_comment = None

            index = 0
            for token in tokens:
                if token.startswith('#'):
                    node.comment = ' '.join(tokens[tokens.index(token):])[1:].strip()
                    break
                if index == 0:
                    node.set_property('name', token)
                else:
                    node.get('arguments').set_property(str(index), token)
                index += 1
            root.append(node)
        return root

    def stringify(self, tree):
        r = ''
        for node in tree.children:
            if node.comment and '\n' in node.comment:
                r += ''.join('%s %s\n' % ('#', x) for x in node.comment.splitlines())
            r += node.get('name').value + ' ' + ' '.join(x.value for x in node.get('arguments').children)
            if node.comment and not '\n' in node.comment:
                r += ' # %s' % node.comment
            r += '\n'
        return r