Debian Codegraph @

test/units/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 from __future__ import annotations
19
20 import os
21 import typing as t
22
23 from ansible.parsing.dataloader import DataLoader
24 from ansible.module_utils.common.text.converters import to_bytes, to_text
25 from ansible._internal._datatag._tags import TrustedAsTemplate
26
27
28 class DictDataLoader(DataLoader):
29
30 def __init__(self, file_mapping=None):
31 file_mapping = {} if file_mapping is None else file_mapping
32 assert isinstance(file_mapping, dict)
33
34 super(DictDataLoader, self).__init__()
35
36 self._file_mapping = file_mapping
37 self._build_known_directories()
38 self._vault_secrets = None
39
40 def load_from_file(self, file_name: str, cache='all', unsafe: bool = False, json_only: bool = False, trusted_as_template: bool = False) -> t.Any:
41 data = None
42 path = to_text(file_name)
43
44 if path in self._file_mapping:
45 abs_path = os.path.join(self.get_basedir(), path)
46 data = self.load(self._file_mapping[path], abs_path, json_only=json_only)
47
48 if trusted_as_template:
49 data = TrustedAsTemplate().tag(data)
50
51 return data
52
53 # TODO: the real _get_file_contents returns a bytestring, so we actually convert the
54 # unicode/text it's created with to utf-8
55 def _get_file_contents(self, file_name):
56 return to_bytes(self._file_mapping[file_name]), False
57
58 def path_exists(self, path):
59 path = to_text(path)
60 return path in self._file_mapping or path in self._known_directories
61
62 def is_file(self, path):
63 path = to_text(path)
64 return path in self._file_mapping
65
66 def is_directory(self, path):
67 path = to_text(path)
68 return path in self._known_directories
69
70 def list_directory(self, path):
71 ret = []
72 path = to_text(path)
73 for x in (list(self._file_mapping.keys()) + self._known_directories):
74 if x.startswith(path):
75 if os.path.dirname(x) == path:
76 ret.append(os.path.basename(x))
77 return ret
78
79 def is_executable(self, path):
80 # FIXME: figure out a way to make paths return true for this
81 return False
82
83 def _add_known_directory(self, directory):
84 if directory not in self._known_directories:
85 self._known_directories.append(directory)
86
87 def _build_known_directories(self):
88 self._known_directories = []
89 for path in self._file_mapping:
90 dirname = os.path.dirname(path)
91 while dirname not in ('/', ''):
92 self._add_known_directory(dirname)
93 dirname = os.path.dirname(dirname)
94
95 def get_basedir(self):
96 return os.getcwd()
97
98 def set_vault_secrets(self, vault_secrets):
99 self._vault_secrets = vault_secrets