summaryrefslogtreecommitdiff
path: root/v2/ansible/plugins/inventory/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'v2/ansible/plugins/inventory/__init__.py')
-rw-r--r--v2/ansible/plugins/inventory/__init__.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/v2/ansible/plugins/inventory/__init__.py b/v2/ansible/plugins/inventory/__init__.py
index 785fc45992..41e8578ee7 100644
--- a/v2/ansible/plugins/inventory/__init__.py
+++ b/v2/ansible/plugins/inventory/__init__.py
@@ -15,7 +15,66 @@
# 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
+
+class InventoryParser:
+ '''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.
+ '''
+ __metaclass__ = ABCMeta
+
+ 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
+