PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ansible/plugins/__init__.py

https://github.com/debfx/ansible
Python | 79 lines | 50 code | 8 blank | 21 comment | 5 complexity | ab1df7c970bf968db1dc3d10388e63d6 MD5 | raw file
 1# (c) 2012, Daniel Hokka Zakrisson <daniel@hozac.com>
 2# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com> and others
 3# (c) 2017, Toshio Kuratomi <tkuratomi@ansible.com>
 4#
 5# This file is part of Ansible
 6#
 7# Ansible is free software: you can redistribute it and/or modify
 8# it under the terms of the GNU General Public License as published by
 9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# Ansible is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
19
20# Make coding more python3-ish
21from __future__ import (absolute_import, division, print_function)
22__metaclass__ = type
23
24from abc import ABCMeta
25
26from ansible import constants as C
27from ansible.module_utils.six import with_metaclass, string_types
28from ansible.utils.display import Display
29
30display = Display()
31
32# Global so that all instances of a PluginLoader will share the caches
33MODULE_CACHE = {}
34PATH_CACHE = {}
35PLUGIN_PATH_CACHE = {}
36
37
38def get_plugin_class(obj):
39    if isinstance(obj, string_types):
40        return obj.lower().replace('module', '')
41    else:
42        return obj.__class__.__name__.lower().replace('module', '')
43
44
45class AnsiblePlugin(with_metaclass(ABCMeta, object)):
46
47    # allow extra passthrough parameters
48    allow_extras = False
49
50    def __init__(self):
51        self._options = {}
52
53    def get_option(self, option, hostvars=None):
54        if option not in self._options:
55            option_value = C.config.get_config_value(option, plugin_type=get_plugin_class(self), plugin_name=self._load_name, variables=hostvars)
56            self.set_option(option, option_value)
57        return self._options.get(option)
58
59    def set_option(self, option, value):
60        self._options[option] = value
61
62    def set_options(self, task_keys=None, var_options=None, direct=None):
63        '''
64        Sets the _options attribute with the configuration/keyword information for this plugin
65
66        :arg task_keys: Dict with playbook keywords that affect this option
67        :arg var_options: Dict with either 'connection variables'
68        :arg direct: Dict with 'direct assignment'
69        '''
70        self._options = C.config.get_plugin_options(get_plugin_class(self), self._load_name, keys=task_keys, variables=var_options, direct=direct)
71
72        # allow extras/wildcards from vars that are not directly consumed in configuration
73        # this is needed to support things like winrm that can have extended protocol options we don't direclty handle
74        if self.allow_extras and var_options and '_extras' in var_options:
75            self.set_option('_extras', var_options['_extras'])
76
77    def _check_required(self):
78        # FIXME: standarize required check based on config
79        pass