Debian Codegraph @

ansible_collections/cisco/ios/tests/unit/mock/vault_helper.py

raw
1 # Ansible is free software: you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation, either version 3 of the License, or
4 # (at your option) any later version.
5 #
6 # Ansible is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License
12 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
13
14 # Make coding more python3-ish
15 from __future__ import absolute_import, division, print_function
16
17
18 __metaclass__ = type
19
20 from ansible.module_utils._text import to_bytes
21 from ansible.parsing.vault import VaultSecret
22
23
24 class TextVaultSecret(VaultSecret):
25 """A secret piece of text. ie, a password. Tracks text encoding.
26
27 The text encoding of the text may not be the default text encoding so
28 we keep track of the encoding so we encode it to the same bytes."""
29
30 def __init__(self, text, encoding=None, errors=None, _bytes=None):
31 super(TextVaultSecret, self).__init__()
32 self.text = text
33 self.encoding = encoding or "utf-8"
34 self._bytes = _bytes
35 self.errors = errors or "strict"
36
37 @property
38 def bytes(self):
39 """The text encoded with encoding, unless we specifically set _bytes."""
40 return self._bytes or to_bytes(self.text, encoding=self.encoding, errors=self.errors)