summaryrefslogtreecommitdiff
path: root/lib/ansible/plugins/inventory/__init__.py
blob: 74dbccc1bbc0720666cfd8f3e4bf076423777e0c (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
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.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/>.

#############################################

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

from abc import ABCMeta, abstractmethod

from six import with_metaclass

class InventoryParser(with_metaclass(ABCMeta, object)):
    '''Abstract Base Class for retrieving inventory information

    Any InventoryParser functions by taking an inven_source.  The caller then
    calls the parser() method.  Once parser is called, the caller can access
    InventoryParser.hosts for a mapping of Host objects and
    InventoryParser.Groups for a mapping of Group objects.
    '''

    def __init__(self, inven_source):
        '''
        InventoryParser contructors take a source of inventory information
        that they will parse the host and group information from.
        '''
        self.inven_source = inven_source
        self.reset_parser()

    @abstractmethod
    def reset_parser(self):
        '''
        InventoryParsers generally cache their data once parser() is
        called.  This method initializes any parser state before calling parser
        again.
        '''
        self.hosts = dict()
        self.groups = dict()
        self.parsed = False

    def _merge(self, target, addition):
        '''
        This method is provided to InventoryParsers to merge host or group
        dicts since it may take several passes to get all of the data

        Example usage:
            self.hosts = self.from_ini(filename)
            new_hosts = self.from_script(scriptname)
            self._merge(self.hosts, new_hosts)
        '''
        for i in addition:
            if i in target:
                target[i].merge(addition[i])
            else:
                target[i] = addition[i]

    @abstractmethod
    def parse(self, refresh=False):
        if refresh:
            self.reset_parser()
        if self.parsed:
            return self.parsed

        # Parse self.inven_sources here
        pass