Debian Codegraph @

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