Debian Codegraph @

ansible_collections/community/general/plugins/cache/yaml.py

raw
1 # Copyright (c) 2017, Brian Coca
2 # Copyright (c) 2017 Ansible Project
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 # Make coding more python3-ish
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 name: yaml
11 short_description: YAML formatted files
12 description:
13 - This cache uses YAML formatted, per host, files saved to the filesystem.
14 author: Brian Coca (@bcoca)
15 options:
16 _uri:
17 required: true
18 description:
19 - Path in which the cache plugin saves the files.
20 env:
21 - name: ANSIBLE_CACHE_PLUGIN_CONNECTION
22 ini:
23 - key: fact_caching_connection
24 section: defaults
25 type: string
26 _prefix:
27 description: User defined prefix to use when creating the files.
28 env:
29 - name: ANSIBLE_CACHE_PLUGIN_PREFIX
30 ini:
31 - key: fact_caching_prefix
32 section: defaults
33 type: string
34 _timeout:
35 default: 86400
36 description: Expiration timeout in seconds for the cache plugin data. Set to 0 to never expire.
37 env:
38 - name: ANSIBLE_CACHE_PLUGIN_TIMEOUT
39 ini:
40 - key: fact_caching_timeout
41 section: defaults
42 type: integer
43 # TODO: determine whether it is OK to change to: type: float
44 """
45
46 import os
47
48 import yaml
49 from ansible.parsing.yaml.dumper import AnsibleDumper
50 from ansible.parsing.yaml.loader import AnsibleLoader
51 from ansible.plugins.cache import BaseFileCacheModule
52
53
54 class CacheModule(BaseFileCacheModule):
55 """
56 A caching module backed by yaml files.
57 """
58
59 def _load(self, filepath):
60 with open(os.path.abspath(filepath), encoding="utf-8") as f:
61 return AnsibleLoader(f).get_single_data()
62
63 def _dump(self, value, filepath):
64 with open(os.path.abspath(filepath), "w", encoding="utf-8") as f:
65 yaml.dump(value, f, Dumper=AnsibleDumper, default_flow_style=False)