Debian Codegraph @

ansible_collections/splunk/es/tests/unit/mock/loader.py

raw
1 # (c) 2012-2014, Michael DeHaan <[email protected]>
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17
18 import os
19
20 from ansible.errors import AnsibleParserError
21 from ansible.module_utils.common.text.converters import to_bytes, to_text
22 from ansible.parsing.dataloader import DataLoader
23
24
25 class DictDataLoader(DataLoader):
26 def __init__(self, file_mapping=None):
27 file_mapping = {} if file_mapping is None else file_mapping
28 assert isinstance(file_mapping, dict)
29
30 super(DictDataLoader, self).__init__()
31
32 self._file_mapping = file_mapping
33 self._build_known_directories()
34 self._vault_secrets = None
35
36 def load_from_file(self, path, cache=True, unsafe=False):
37 path = to_text(path)
38 if path in self._file_mapping:
39 return self.load(self._file_mapping[path], path)
40 return None
41
42 # TODO: the real _get_file_contents returns a bytestring, so we actually convert the
43 # unicode/text it's created with to utf-8
44 def _get_file_contents(self, file_name):
45 path = to_text(file_name)
46 if path in self._file_mapping:
47 return (to_bytes(self._file_mapping[path]), False)
48 else:
49 raise AnsibleParserError("file not found: %s" % path)
50
51 def path_exists(self, path):
52 path = to_text(path)
53 return path in self._file_mapping or path in self._known_directories
54
55 def is_file(self, path):
56 path = to_text(path)
57 return path in self._file_mapping
58
59 def is_directory(self, path):
60 path = to_text(path)
61 return path in self._known_directories
62
63 def list_directory(self, path):
64 ret = []
65 path = to_text(path)
66 for x in list(self._file_mapping.keys()) + self._known_directories:
67 if x.startswith(path):
68 if os.path.dirname(x) == path:
69 ret.append(os.path.basename(x))
70 return ret
71
72 def is_executable(self, path):
73 # FIXME: figure out a way to make paths return true for this
74 return False
75
76 def _add_known_directory(self, directory):
77 if directory not in self._known_directories:
78 self._known_directories.append(directory)
79
80 def _build_known_directories(self):
81 self._known_directories = []
82 for path in self._file_mapping:
83 dirname = os.path.dirname(path)
84 while dirname not in ("/", ""):
85 self._add_known_directory(dirname)
86 dirname = os.path.dirname(dirname)
87
88 def push(self, path, content):
89 rebuild_dirs = False
90 if path not in self._file_mapping:
91 rebuild_dirs = True
92
93 self._file_mapping[path] = content
94
95 if rebuild_dirs:
96 self._build_known_directories()
97
98 def pop(self, path):
99 if path in self._file_mapping:
100 del self._file_mapping[path]
101 self._build_known_directories()
102
103 def clear(self):
104 self._file_mapping = dict()
105 self._known_directories = []
106
107 def get_basedir(self):
108 return os.getcwd()
109
110 def set_vault_secrets(self, vault_secrets):
111 self._vault_secrets = vault_secrets