1 | # |
||
2 | # Copyright (C) 2017, 2018 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: The parser cannot load some primitive data such like '' (empty |
||
18 | string), ' ' (white space) and [] (empty list) as these are because of the |
||
19 | implementation of :func:`~anyconfig.backend.base.load_with_fn`. |
||
20 | - Special options: All options of pickle.{load{s,},dump{s,}} should work. |
||
21 | |||
22 | Changelog: |
||
23 | |||
24 | .. versionchanged:: 0.9.7 |
||
25 | |||
26 | - Add support of loading primitives other than mapping objects. |
||
27 | |||
28 | .. versionadded:: 0.8.3 |
||
29 | """ |
||
30 | from __future__ import absolute_import |
||
31 | |||
32 | try: |
||
33 | import cPickle as pickle |
||
34 | except ImportError: |
||
35 | import pickle |
||
36 | |||
37 | import anyconfig.backend.base |
||
38 | import anyconfig.compat |
||
39 | |||
40 | |||
41 | if anyconfig.compat.IS_PYTHON_3: |
||
42 | LOAD_OPTS = ["fix_imports", "encoding", "errors"] |
||
43 | DUMP_OPTS = ["protocol", "fix_imports"] |
||
44 | else: |
||
45 | LOAD_OPTS = [] |
||
46 | DUMP_OPTS = ["protocol"] |
||
47 | |||
48 | |||
49 | View Code Duplication | class Parser(anyconfig.backend.base.StringStreamFnParser, |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
50 | anyconfig.backend.base.BinaryFilesMixin): |
||
51 | """ |
||
52 | Parser for Pickle files. |
||
53 | """ |
||
54 | _type = "pickle" |
||
55 | _extensions = ["pkl", "pickle"] |
||
56 | _load_opts = LOAD_OPTS |
||
57 | _dump_opts = DUMP_OPTS |
||
58 | _allow_primitives = True |
||
59 | |||
60 | _load_from_string_fn = anyconfig.backend.base.to_method(pickle.loads) |
||
61 | _load_from_stream_fn = anyconfig.backend.base.to_method(pickle.load) |
||
62 | _dump_to_string_fn = anyconfig.backend.base.to_method(pickle.dumps) |
||
63 | _dump_to_stream_fn = anyconfig.backend.base.to_method(pickle.dump) |
||
64 | |||
65 | # vim:sw=4:ts=4:et: |
||
66 |