1
|
|
|
# |
2
|
|
|
# Copyright (C) 2017 Satoru SATOH <ssato @ redhat.com> |
3
|
|
|
# License: MIT |
4
|
|
|
# |
5
|
|
|
r"""Pickle backend: |
6
|
|
|
|
7
|
|
|
- Format to support: Pickle |
8
|
|
|
- Requirements: It should be available always. |
9
|
|
|
|
10
|
|
|
- pickle/cPickle in python 2 standard library: |
11
|
|
|
https://docs.python.org/2/library/pickle.html |
12
|
|
|
|
13
|
|
|
- pickle in python 3 standard library: |
14
|
|
|
https://docs.python.org/3/library/pickle.html |
15
|
|
|
|
16
|
|
|
- Development Status :: 4 - Beta |
17
|
|
|
- Limitations: None obvious |
18
|
|
|
- Special options: All options of pickle.{load{s,},dump{s,}} should work. |
19
|
|
|
|
20
|
|
|
Changelog: |
21
|
|
|
|
22
|
|
|
.. versionadded:: 0.8.3 |
23
|
|
|
""" |
24
|
|
|
from __future__ import absolute_import |
25
|
|
|
|
26
|
|
|
try: |
27
|
|
|
import cPickle as pickle |
28
|
|
|
except ImportError: |
29
|
|
|
import pickle |
30
|
|
|
|
31
|
|
|
import anyconfig.backend.base |
32
|
|
|
import anyconfig.compat |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
if anyconfig.compat.IS_PYTHON_3: |
36
|
|
|
LOAD_OPTS = ["fix_imports", "encoding", "errors"] |
37
|
|
|
DUMP_OPTS = ["protocol", "fix_imports"] |
38
|
|
|
else: |
39
|
|
|
LOAD_OPTS = [] |
40
|
|
|
DUMP_OPTS = ["protocol"] |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
def load_with_fn(load_fn, content_or_strm, to_container, **opts): |
44
|
|
|
""" |
45
|
|
|
Load pickled config from given string or stream `content_or_strm`. |
46
|
|
|
|
47
|
|
|
:param content_or_strm: pickled config content or stream provides it |
48
|
|
|
:param to_container: callble to make a container object |
49
|
|
|
:param opts: keyword options passed to `pickle.load[s]` |
50
|
|
|
|
51
|
|
|
:return: Dict-like object holding configuration |
52
|
|
|
""" |
53
|
|
|
return to_container(load_fn(content_or_strm, **opts)) |
54
|
|
|
|
55
|
|
|
|
56
|
|
View Code Duplication |
class Parser(anyconfig.backend.base.FromStreamLoader, |
|
|
|
|
57
|
|
|
anyconfig.backend.base.ToStreamDumper): |
58
|
|
|
""" |
59
|
|
|
Parser for Pickle files. |
60
|
|
|
""" |
61
|
|
|
_type = "pickle" |
62
|
|
|
_extensions = ["pkl", "pickle"] |
63
|
|
|
_load_opts = LOAD_OPTS |
64
|
|
|
_dump_opts = DUMP_OPTS |
65
|
|
|
_open_flags = ('rb', 'wb') |
66
|
|
|
|
67
|
|
|
dump_to_string = anyconfig.backend.base.to_method(pickle.dumps) |
68
|
|
|
dump_to_stream = anyconfig.backend.base.to_method(pickle.dump) |
69
|
|
|
_load = anyconfig.backend.base.to_method(load_with_fn) |
70
|
|
|
|
71
|
|
|
def load_from_string(self, content, to_container, **opts): |
72
|
|
|
""" |
73
|
|
|
Load Pickle config from given string `content`. |
74
|
|
|
|
75
|
|
|
:param content: Pickled config content |
76
|
|
|
:param to_container: callble to make a container object |
77
|
|
|
:param opts: keyword options passed to `pickle.loads` |
78
|
|
|
|
79
|
|
|
:return: Dict-like object holding configuration |
80
|
|
|
""" |
81
|
|
|
return self._load(pickle.loads, content, to_container, **opts) |
82
|
|
|
|
83
|
|
|
def load_from_stream(self, stream, to_container, **opts): |
84
|
|
|
""" |
85
|
|
|
Load Pickle config from given stream `stream`. |
86
|
|
|
|
87
|
|
|
:param stream: Stream will provide Pickled config content string |
88
|
|
|
:param to_container: callble to make a container object |
89
|
|
|
:param opts: keyword options passed to `pickle.load` |
90
|
|
|
|
91
|
|
|
:return: Dict-like object holding configuration |
92
|
|
|
""" |
93
|
|
|
return self._load(pickle.load, stream, to_container, **opts) |
94
|
|
|
|
95
|
|
|
# vim:sw=4:ts=4:et: |
96
|
|
|
|