Debian Codegraph @

lib/ansible/plugins/cache/jsonfile.py

raw
1 # (c) 2014, Brian Coca, Josh Drake, et al
2 # (c) 2017 Ansible Project
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5 from __future__ import annotations
6
7 DOCUMENTATION = """
8 name: jsonfile
9 short_description: JSON formatted files.
10 description:
11 - This cache uses JSON formatted, per host, files saved to the filesystem.
12 version_added: "1.9"
13 author: Ansible Core (@ansible-core)
14 options:
15 _uri:
16 required: True
17 description:
18 - Path in which the cache plugin will save the JSON files
19 env:
20 - name: ANSIBLE_CACHE_PLUGIN_CONNECTION
21 ini:
22 - key: fact_caching_connection
23 section: defaults
24 type: path
25 _prefix:
26 description: User defined prefix to use when creating the JSON files
27 env:
28 - name: ANSIBLE_CACHE_PLUGIN_PREFIX
29 ini:
30 - key: fact_caching_prefix
31 section: defaults
32 _timeout:
33 default: 86400
34 description: Expiration timeout for the cache plugin data
35 env:
36 - name: ANSIBLE_CACHE_PLUGIN_TIMEOUT
37 ini:
38 - key: fact_caching_timeout
39 section: defaults
40 type: integer
41 persist_metadata:
42 version_added: "2.22"
43 description:
44 - By default, the fact cache preserves internal metadata, including deprecation notices and variable origin.
45 - Setting this option to False will remove this information, and use the filename and format matching the behavior before ansible-core 2.19.
46 default: True
47 type: bool
48 env:
49 - name: ANSIBLE_CACHE_JSONFILE_PERSIST_METADATA
50 ini:
51 - key: persist_metadata
52 section: jsonfile_cache
53 """
54
55 import json
56 import pathlib
57
58 from ansible.plugins.cache import BaseFileCacheModule
59
60
61 class CacheModule(BaseFileCacheModule):
62 """A caching module backed by json files."""
63
64 # NOTE: This relies on core impl details about cache instantiation and the PluginLoader that can change at any time.
65 @property
66 def _persistent(self):
67 return self.get_option("persist_metadata")
68
69 def _load(self, filepath: str) -> object:
70 return json.loads(pathlib.Path(filepath).read_text())
71
72 def _dump(self, value: object, filepath: str) -> None:
73 pathlib.Path(filepath).write_text(json.dumps(value))