summaryrefslogtreecommitdiff
path: root/lib/ansible/plugins/cache/pickle.py
blob: 1a3163ef4ea74ff03a123f101b232602835116ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# (c) 2017, Brian Coca
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
'''
DOCUMENTATION:
    cache: yaml
    short_description: Pickle formatted files.
    description:
        - This cache uses Python's pickle serialization format, in per host files, saved to the filesystem.
    version_added: "2.3"
    author: Brian Coca (@bcoca)
'''

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

try:
    import cPickle as pickle
except ImportError:
    import pickle

from ansible.module_utils.six import PY3
from ansible.plugins.cache import BaseFileCacheModule


class CacheModule(BaseFileCacheModule):
    """
    A caching module backed by pickle files.
    """

    def _load(self, filepath):
        # Pickle is a binary format
        with open(filepath, 'rb') as f:
            if PY3:
                return pickle.load(f, encoding='bytes')
            else:
                return pickle.load(f)

    def _dump(self, value, filepath):
        with open(filepath, 'wb') as f:
            # Use pickle protocol 2 which is compatible with Python 2.3+.
            pickle.dump(value, f, protocol=2)