summaryrefslogtreecommitdiff
path: root/cloudinit/distros/parsers/hosts.py
blob: e43880afd240f1d35ac5a804c55b24cfd9e2ef52 (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
# Copyright (C) 2012 Yahoo! Inc.
#
# Author: Joshua Harlow <harlowja@yahoo-inc.com>
#
# This file is part of cloud-init. See LICENSE file for license information.

from io import StringIO

from cloudinit.distros.parsers import chop_comment


# See: man hosts
# or https://linux.die.net/man/5/hosts
# or https://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/configtuning-configfiles.html # noqa
class HostsConf(object):
    def __init__(self, text):
        self._text = text
        self._contents = None

    def parse(self):
        if self._contents is None:
            self._contents = self._parse(self._text)

    def get_entry(self, ip):
        self.parse()
        options = []
        for (line_type, components) in self._contents:
            if line_type == "option":
                (pieces, _tail) = components
                if len(pieces) and pieces[0] == ip:
                    options.append(pieces[1:])
        return options

    def del_entries(self, ip):
        self.parse()
        n_entries = []
        for (line_type, components) in self._contents:
            if line_type != "option":
                n_entries.append((line_type, components))
                continue
            else:
                (pieces, _tail) = components
                if len(pieces) and pieces[0] == ip:
                    pass
                elif len(pieces):
                    n_entries.append((line_type, list(components)))
        self._contents = n_entries

    def add_entry(self, ip, canonical_hostname, *aliases):
        self.parse()
        self._contents.append(
            ("option", ([ip, canonical_hostname] + list(aliases), ""))
        )

    def _parse(self, contents):
        entries = []
        for line in contents.splitlines():
            if not len(line.strip()):
                entries.append(("blank", [line]))
                continue
            (head, tail) = chop_comment(line.strip(), "#")
            if not len(head):
                entries.append(("all_comment", [line]))
                continue
            entries.append(("option", [head.split(None), tail]))
        return entries

    def __str__(self):
        self.parse()
        contents = StringIO()
        for (line_type, components) in self._contents:
            if line_type == "blank":
                contents.write("%s\n" % (components[0]))
            elif line_type == "all_comment":
                contents.write("%s\n" % (components[0]))
            elif line_type == "option":
                (pieces, tail) = components
                pieces = [str(p) for p in pieces]
                pieces = "\t".join(pieces)
                contents.write("%s%s\n" % (pieces, tail))
        return contents.getvalue()


# vi: ts=4 expandtab