Debian Codegraph @

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