2014-10-28 20:35:29 +01:00
|
|
|
# (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
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
2015-06-11 19:03:25 +02:00
|
|
|
from ansible.errors import AnsibleParserError
|
2015-10-26 22:23:09 +01:00
|
|
|
from ansible.parsing.dataloader import DataLoader
|
2016-11-07 16:07:26 +01:00
|
|
|
from ansible.module_utils._text import to_bytes
|
|
|
|
|
2014-10-28 20:35:29 +01:00
|
|
|
|
|
|
|
class DictDataLoader(DataLoader):
|
|
|
|
|
|
|
|
def __init__(self, file_mapping=dict()):
|
|
|
|
assert type(file_mapping) == dict
|
|
|
|
|
2015-08-06 16:28:22 +02:00
|
|
|
super(DictDataLoader, self).__init__()
|
|
|
|
|
2014-10-28 20:35:29 +01:00
|
|
|
self._file_mapping = file_mapping
|
|
|
|
self._build_known_directories()
|
|
|
|
|
2017-05-23 23:16:49 +02:00
|
|
|
def load_from_file(self, path, unsafe=False):
|
2014-10-28 20:35:29 +01:00
|
|
|
if path in self._file_mapping:
|
|
|
|
return self.load(self._file_mapping[path], path)
|
|
|
|
return None
|
|
|
|
|
2016-11-07 16:07:26 +01:00
|
|
|
# TODO: the real _get_file_contents returns a bytestring, so we actually convert the
|
|
|
|
# unicode/text it's created with to utf-8
|
2015-05-17 08:06:02 +02:00
|
|
|
def _get_file_contents(self, path):
|
|
|
|
if path in self._file_mapping:
|
2016-11-07 16:07:26 +01:00
|
|
|
return (to_bytes(self._file_mapping[path]), False)
|
2015-05-17 08:06:02 +02:00
|
|
|
else:
|
|
|
|
raise AnsibleParserError("file not found: %s" % path)
|
|
|
|
|
2014-10-28 20:35:29 +01:00
|
|
|
def path_exists(self, path):
|
|
|
|
return path in self._file_mapping or path in self._known_directories
|
|
|
|
|
|
|
|
def is_file(self, path):
|
|
|
|
return path in self._file_mapping
|
|
|
|
|
|
|
|
def is_directory(self, path):
|
|
|
|
return path in self._known_directories
|
|
|
|
|
2015-03-25 19:51:40 +01:00
|
|
|
def list_directory(self, path):
|
|
|
|
return [x for x in self._known_directories]
|
|
|
|
|
2015-09-04 22:41:38 +02:00
|
|
|
def is_executable(self, path):
|
|
|
|
# FIXME: figure out a way to make paths return true for this
|
|
|
|
return False
|
|
|
|
|
2014-10-28 20:35:29 +01:00
|
|
|
def _add_known_directory(self, directory):
|
|
|
|
if directory not in self._known_directories:
|
|
|
|
self._known_directories.append(directory)
|
|
|
|
|
|
|
|
def _build_known_directories(self):
|
|
|
|
self._known_directories = []
|
|
|
|
for path in self._file_mapping:
|
|
|
|
dirname = os.path.dirname(path)
|
|
|
|
while dirname not in ('/', ''):
|
|
|
|
self._add_known_directory(dirname)
|
|
|
|
dirname = os.path.dirname(dirname)
|
|
|
|
|
|
|
|
def push(self, path, content):
|
|
|
|
rebuild_dirs = False
|
|
|
|
if path not in self._file_mapping:
|
|
|
|
rebuild_dirs = True
|
2015-08-06 16:28:22 +02:00
|
|
|
|
2014-10-28 20:35:29 +01:00
|
|
|
self._file_mapping[path] = content
|
|
|
|
|
|
|
|
if rebuild_dirs:
|
|
|
|
self._build_known_directories()
|
|
|
|
|
|
|
|
def pop(self, path):
|
|
|
|
if path in self._file_mapping:
|
|
|
|
del self._file_mapping[path]
|
|
|
|
self._build_known_directories()
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
self._file_mapping = dict()
|
|
|
|
self._known_directories = []
|
|
|
|
|
2017-05-23 23:16:49 +02:00
|
|
|
def get_basedir(self):
|
|
|
|
return os.getcwd()
|