1
|
|
|
# |
2
|
|
|
# Copyright (C) 2011 - 2016 Satoru SATOH <ssato redhat.com> |
3
|
|
|
# License: MIT |
4
|
|
|
# |
5
|
|
|
# pylint: disable=unused-argument,unused-import |
6
|
|
|
"""Wrapper of m9dicts. |
7
|
|
|
|
8
|
|
|
.. versionadded: 0.4.99 |
9
|
|
|
Swtiched from mergeabledict to m9dicts |
10
|
|
|
""" |
11
|
|
|
from __future__ import absolute_import |
12
|
|
|
|
13
|
|
|
import m9dicts |
14
|
|
|
from m9dicts import ( |
15
|
|
|
MS_REPLACE, MS_NO_REPLACE, MS_DICTS, MS_DICTS_AND_LISTS, MERGE_STRATEGIES, |
16
|
|
|
NTPL_CLS_KEY, get, set_, is_namedtuple # flake8: noqa |
17
|
|
|
) |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
def to_container(obj=None, ac_ordered=False, ac_merge=m9dicts.MS_DICTS, |
21
|
|
|
ac_namedtuple=False, ac_ntpl_cls_key=NTPL_CLS_KEY, **options): |
22
|
|
|
r""" |
23
|
|
|
Factory function to create a dict-like object[s] supports merge operation |
24
|
|
|
from a dict or any other objects. |
25
|
|
|
|
26
|
|
|
.. seealso:: :func:`m9dicts.make` |
27
|
|
|
|
28
|
|
|
:param obj: A dict or other object[s] or None |
29
|
|
|
:param ordered: |
30
|
|
|
Create an instance of OrderedMergeableDict instead of MergeableDict If |
31
|
|
|
it's True. Please note that OrderedMergeableDict class will be chosen |
32
|
|
|
for namedtuple objects regardless of this argument always to keep keys |
33
|
|
|
(fields) order. |
34
|
|
|
:param merge: |
35
|
|
|
Specify strategy from MERGE_STRATEGIES of how to merge results loaded |
36
|
|
|
from multiple configuration files. |
37
|
|
|
:param _ntpl_cls_key: |
38
|
|
|
Special keyword to embedded the class name of namedtuple object to the |
39
|
|
|
dict-like object created. It's a hack and not elegant but I don't think |
40
|
|
|
there are another ways to make same namedtuple object from objects |
41
|
|
|
created from it. |
42
|
|
|
:param options: |
43
|
|
|
Optional keyword arguments for m9dicts.convert_to, will be converted to |
44
|
|
|
the above ac_\* options respectively as needed. |
45
|
|
|
""" |
46
|
|
|
opts = dict(ordered=ac_ordered, merge=ac_merge, |
47
|
|
|
_ntpl_cls_key=ac_ntpl_cls_key) |
48
|
|
|
|
49
|
|
|
return m9dicts.make(obj, **opts) |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
def convert_to(obj, ac_ordered=True, ac_namedtuple=False, |
53
|
|
|
ac_ntpl_cls_key=NTPL_CLS_KEY, **options): |
54
|
|
|
r""" |
55
|
|
|
Convert given `obj` :: m9dict object to a dict, dict or OrderedDict if |
56
|
|
|
ac_ordered == True, or a namedtuple if ac_namedtuple == True. |
57
|
|
|
|
58
|
|
|
.. seealso:: :func:`m9dicts.convert_to` |
59
|
|
|
|
60
|
|
|
:param obj: A m9dict object to convert to |
61
|
|
|
:param ac_ordered: OrderedDict will be chosen if True |
62
|
|
|
:param ac_namedtuple: A namedtuple object will be chosen if True |
63
|
|
|
:param ac_ntpl_cls_key: The name of namedtuple object |
64
|
|
|
:param options: |
65
|
|
|
Optional keyword arguments for m9dicts.convert_to, will be converted to |
66
|
|
|
the above ac_\* options respectively as needed. |
67
|
|
|
""" |
68
|
|
|
opts = dict(ordered=ac_ordered, to_namedtuple=ac_namedtuple, |
69
|
|
|
_ntpl_cls_key=ac_ntpl_cls_key) |
70
|
|
|
|
71
|
|
|
return m9dicts.convert_to(obj, **opts) |
72
|
|
|
|
73
|
|
|
# vim:sw=4:ts=4:et: |
74
|
|
|
|