Debian Codegraph @

ansible_collections/community/aws/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 # Make coding more python3-ish
19 from __future__ import absolute_import
20 from __future__ import division
21 from __future__ import print_function
22
23 __metaclass__ = type
24
25 import os
26
27 from ansible.errors import AnsibleParserError
28 from ansible.module_utils._text import to_bytes
29 from ansible.module_utils._text import to_text
30 from ansible.parsing.dataloader import DataLoader
31
32
33 class DictDataLoader(DataLoader):
34 def __init__(self, file_mapping=None):
35 file_mapping = {} if file_mapping is None else file_mapping
36 assert isinstance(file_mapping, dict)
37
38 super(DictDataLoader, self).__init__()
39
40 self._file_mapping = file_mapping
41 self._build_known_directories()
42 self._vault_secrets = None
43
44 def load_from_file(self, path, cache=True, unsafe=False):
45 path = to_text(path)
46 if path in self._file_mapping:
47 return self.load(self._file_mapping[path], path)
48 return None
49
50 # TODO: the real _get_file_contents returns a bytestring, so we actually convert the
51 # unicode/text it's created with to utf-8
52 def _get_file_contents(self, file_name):
53 file_name = to_text(file_name)
54 if file_name in self._file_mapping:
55 return (to_bytes(self._file_mapping[file_name]), False)
56 else:
57 raise AnsibleParserError(f"file not found: {file_name}")
58
59 def path_exists(self, path):
60 path = to_text(path)
61 return path in self._file_mapping or path in self._known_directories
62
63 def is_file(self, path):
64 path = to_text(path)
65 return path in self._file_mapping
66
67 def is_directory(self, path):
68 path = to_text(path)
69 return path in self._known_directories
70
71 def list_directory(self, path):
72 ret = []
73 path = to_text(path)
74 for x in list(self._file_mapping.keys()) + self._known_directories:
75 if x.startswith(path):
76 if os.path.dirname(x) == path:
77 ret.append(os.path.basename(x))
78 return ret
79
80 def is_executable(self, path):
81 # FIXME: figure out a way to make paths return true for this
82 return False
83
84 def _add_known_directory(self, directory):
85 if directory not in self._known_directories:
86 self._known_directories.append(directory)
87
88 def _build_known_directories(self):
89 self._known_directories = []
90 for path in self._file_mapping:
91 dirname = os.path.dirname(path)
92 while dirname not in ("/", ""):
93 self._add_known_directory(dirname)
94 dirname = os.path.dirname(dirname)
95
96 def push(self, path, content):
97 rebuild_dirs = False
98 if path not in self._file_mapping:
99 rebuild_dirs = True
100
101 self._file_mapping[path] = content
102
103 if rebuild_dirs:
104 self._build_known_directories()
105
106 def pop(self, path):
107 if path in self._file_mapping:
108 del self._file_mapping[path]
109 self._build_known_directories()
110
111 def clear(self):
112 self._file_mapping = dict()
113 self._known_directories = []
114
115 def get_basedir(self):
116 return os.getcwd()
117
118 def set_vault_secrets(self, vault_secrets):
119 self._vault_secrets = vault_secrets