|
1
|
|
|
# |
|
2
|
|
|
# Copyright (C) 2015 Satoru SATOH <ssato @ redhat.com> |
|
3
|
|
|
# License: MIT |
|
4
|
|
|
# |
|
5
|
|
|
# Ref. python -c "import bson; help(bson)" |
|
6
|
|
|
# |
|
7
|
|
|
# Access to bson._use_c is required to switch loading options: |
|
8
|
|
|
# pylint: disable=protected-access |
|
9
|
|
|
r"""BSON backend. |
|
10
|
|
|
|
|
11
|
|
|
.. versionchanged:: 0.4.99 |
|
12
|
|
|
|
|
13
|
|
|
- utilize as_class keyword argument to allow container objects made directly |
|
14
|
|
|
on load if C extension is not used and enabled. |
|
15
|
|
|
- _load_opts() was removed because C extension looks forced to be enalbed |
|
16
|
|
|
if bson.has_c() == True, that is, C extension was built. see also: |
|
17
|
|
|
https://jira.mongodb.org/browse/PYTHON-379 |
|
18
|
|
|
|
|
19
|
|
|
.. versionadded:: 0.1.0 |
|
20
|
|
|
|
|
21
|
|
|
- Format to support: BSON, http://bsonspec.org |
|
22
|
|
|
- Requirements: bson in pymongo, https://pypi.python.org/pypi/pymongo/ |
|
23
|
|
|
- Limitations: It seems that the APIs of bson.decode\* were changed a lot in |
|
24
|
|
|
the current version (3.2) of python-bson in pymongo and this backend might |
|
25
|
|
|
not work with it. I don't have a time to test with that latest version yet |
|
26
|
|
|
and it's only tested with the older one, 2.5.2. |
|
27
|
|
|
- Special options: |
|
28
|
|
|
|
|
29
|
|
|
- All keyword options for :meth:`encode` (dump{s,}) and :meth:`decode` |
|
30
|
|
|
(load{s,}) of :class:`bson.BSON` except for as_class should just work. |
|
31
|
|
|
|
|
32
|
|
|
- See also: https://api.mongodb.org/python/current/api/bson/ |
|
33
|
|
|
""" |
|
34
|
|
|
from __future__ import absolute_import |
|
35
|
|
|
|
|
36
|
|
|
import bson |
|
37
|
|
|
import anyconfig.backend.base |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
class Parser(anyconfig.backend.base.FromStringLoader, |
|
41
|
|
|
anyconfig.backend.base.ToStringDumper): |
|
42
|
|
|
""" |
|
43
|
|
|
Loader/Dumper of BSON files. |
|
44
|
|
|
""" |
|
45
|
|
|
_type = "bson" |
|
46
|
|
|
_extensions = ["bson", "bsn"] # Temporary. |
|
47
|
|
|
_load_opts = [] if bson.has_c() else ["tz_aware", "uuid_subtype"] |
|
48
|
|
|
_dump_opts = ["check_keys", "uuid_subtype"] |
|
49
|
|
|
_open_flags = ('rb', 'wb') |
|
50
|
|
|
|
|
51
|
|
|
dump_to_string = anyconfig.backend.base.to_method(bson.BSON.encode) |
|
52
|
|
|
|
|
53
|
|
|
def load_from_string(self, content, to_container, **kwargs): |
|
54
|
|
|
""" |
|
55
|
|
|
Load BSON config from given string `content`. |
|
56
|
|
|
|
|
57
|
|
|
:param content: BSON config content in bytes data string |
|
58
|
|
|
:param to_container: callble to make a container object |
|
59
|
|
|
:param kwargs: optional keyword parameters |
|
60
|
|
|
|
|
61
|
|
|
:return: Dict-like object holding config parameters |
|
62
|
|
|
""" |
|
63
|
|
|
if self._load_opts: # indicates that C extension is not used. |
|
64
|
|
|
objs = bson.decode_all(content, as_class=to_container, **kwargs) |
|
65
|
|
|
else: |
|
66
|
|
|
# .. note:: |
|
67
|
|
|
# The order of loaded configuration keys may be lost but |
|
68
|
|
|
# there is no way to avoid that, AFAIK. |
|
69
|
|
|
objs = to_container(bson.decode_all(content)) |
|
70
|
|
|
|
|
71
|
|
|
return objs[0] if objs else None |
|
72
|
|
|
|
|
73
|
|
|
# vim:sw=4:ts=4:et: |
|
74
|
|
|
|