mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #45054 from AAbouZaid/toml_serializer
Add TOML serializer.
This commit is contained in:
commit
aea1a584a5
2 changed files with 75 additions and 0 deletions
63
salt/serializers/toml.py
Normal file
63
salt/serializers/toml.py
Normal file
|
@ -0,0 +1,63 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
salt.serializers.toml
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Implements TOML serializer.
|
||||
|
||||
It's just a wrapper around pytoml module.
|
||||
'''
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
# Import pytoml
|
||||
try:
|
||||
import pytoml as toml
|
||||
available = True
|
||||
except ImportError:
|
||||
available = False
|
||||
|
||||
# Import Salt libs
|
||||
from salt.serializers import DeserializationError, SerializationError
|
||||
|
||||
# Import 3rd-party libs
|
||||
from salt.ext import six
|
||||
|
||||
__all__ = ['deserialize', 'serialize', 'available']
|
||||
|
||||
|
||||
def deserialize(stream_or_string, **options):
|
||||
'''
|
||||
Deserialize from TOML into Python data structure.
|
||||
|
||||
:param stream_or_string: toml stream or string to deserialize.
|
||||
:param options: options given to lower pytoml module.
|
||||
'''
|
||||
|
||||
try:
|
||||
if not isinstance(stream_or_string, (bytes, six.string_types)):
|
||||
return toml.load(stream_or_string, **options)
|
||||
|
||||
if isinstance(stream_or_string, bytes):
|
||||
stream_or_string = stream_or_string.decode('utf-8')
|
||||
|
||||
return toml.loads(stream_or_string)
|
||||
except Exception as error:
|
||||
raise DeserializationError(error)
|
||||
|
||||
|
||||
def serialize(obj, **options):
|
||||
'''
|
||||
Serialize Python data to TOML.
|
||||
|
||||
:param obj: the data structure to serialize.
|
||||
:param options: options given to lower pytoml module.
|
||||
'''
|
||||
|
||||
try:
|
||||
if 'file_out' in options:
|
||||
return toml.dump(obj, options['file_out'], **options)
|
||||
else:
|
||||
return toml.dumps(obj, **options)
|
||||
except Exception as error:
|
||||
raise SerializationError(error)
|
|
@ -18,6 +18,7 @@ import salt.serializers.yaml as yaml
|
|||
import salt.serializers.yamlex as yamlex
|
||||
import salt.serializers.msgpack as msgpack
|
||||
import salt.serializers.python as python
|
||||
import salt.serializers.toml as toml
|
||||
from salt.serializers.yaml import EncryptedString
|
||||
from salt.serializers import SerializationError
|
||||
from salt.utils.odict import OrderedDict
|
||||
|
@ -349,3 +350,14 @@ class TestSerializers(TestCase):
|
|||
|
||||
deserialized = configparser.deserialize(serialized)
|
||||
assert deserialized == data, deserialized
|
||||
|
||||
@skipIf(not toml.available, SKIP_MESSAGE % 'toml')
|
||||
def test_serialize_toml(self):
|
||||
data = {
|
||||
"foo": "bar"
|
||||
}
|
||||
serialized = toml.serialize(data)
|
||||
assert serialized == 'foo = "bar"\n', serialized
|
||||
|
||||
deserialized = toml.deserialize(serialized)
|
||||
assert deserialized == data, deserialized
|
||||
|
|
Loading…
Add table
Reference in a new issue