1"""
2    salt.serializers.python
3    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5    .. versionadded:: 2016.3.0
6
7    Implements a Python serializer (via pprint.format)
8"""
9
10import pprint
11
12import salt.utils.json
13
14try:
15    import simplejson as _json
16except ImportError:
17    import json as _json  # pylint: disable=blacklisted-import
18
19
20__all__ = ["serialize", "available"]
21
22available = True
23
24
25def serialize(obj, **options):
26    """
27    Serialize Python data to a Python string representation (via pprint.format)
28
29    :param obj: the data structure to serialize
30    :param options: options given to pprint.format
31    """
32
33    # round-trip this through JSON to avoid OrderedDict types
34    # there's probably a more performant way to do this...
35    # TODO remove json round-trip when all dataset will use
36    # serializers
37    return pprint.pformat(
38        salt.utils.json.loads(
39            salt.utils.json.dumps(obj, _json_module=_json), _json_module=_json
40        ),
41        **options
42    )
43