Debian Codegraph @

ansible_collections/community/general/plugins/cache/pickle.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: pickle
11 short_description: Pickle formatted files
12 description:
13 - This cache uses Python's pickle serialization format, in 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: path
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: float
43 """
44
45 import pickle
46
47 from ansible.plugins.cache import BaseFileCacheModule
48
49
50 class CacheModule(BaseFileCacheModule):
51 """
52 A caching module backed by pickle files.
53 """
54
55 _persistent = False # prevent unnecessary JSON serialization and key munging
56
57 def _load(self, filepath):
58 # Pickle is a binary format
59 with open(filepath, "rb") as f:
60 return pickle.load(f, encoding="bytes")
61
62 def _dump(self, value, filepath):
63 with open(filepath, "wb") as f:
64 # Use pickle protocol 2 which is compatible with Python 2.3+.
65 pickle.dump(value, f, protocol=2)