Esempio n. 1
0
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 the python toml module.
    """

    try:
        if not isinstance(stream_or_string, (bytes, str)):
            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:  # pylint: disable=broad-except
        raise DeserializationError(error)
Esempio n. 2
0
def deserialize(stream_or_string, **options):
    '''
    Deserialize any string or stream like object into a Python data structure.

    :param stream_or_string: stream or string to deserialize.
    :param options: options given to lower json/simplejson module.
    '''

    try:
        if not isinstance(stream_or_string, (bytes, six.string_types)):
            return salt.utils.json.load(
                stream_or_string, _json_module=_json, **options)

        if isinstance(stream_or_string, bytes):
            stream_or_string = stream_or_string.decode('utf-8')

        return salt.utils.json.loads(stream_or_string, _json_module=_json)
    except Exception as error:  # pylint: disable=broad-except
        raise DeserializationError(error)
Esempio n. 3
0
def deserialize(stream_or_string, **options):
    """
    Deserialize any string or stream like object into a Python data structure.

    :param stream_or_string: stream or string to deserialize.
    :param options: options given to lower plist module.

    :returns: Deserialized data structure.
    """
    try:
        if not isinstance(stream_or_string, (bytes, str)):
            log.trace("Using plistlib.load to deserialize.")
            return plistlib.load(stream_or_string, **options)

        if isinstance(stream_or_string, str):
            log.trace("Need to encode plist string.")
            stream_or_string = stream_or_string.encode("utf-8")

        log.trace("Using plistlib.loads to deserialize.")
        return plistlib.loads(stream_or_string, **options)
    except Exception as error:  # pylint: disable=broad-except
        raise DeserializationError(error)
Esempio n. 4
0
def deserialize(stream_or_string, **options):
    """
    Deserialize any string or stream like object into a Python data structure.

    :param stream_or_string: stream or string to deserialize.
    :param options: options given to lower configparser module.
    """

    cp = configparser.ConfigParser(**options)

    try:
        if not isinstance(stream_or_string, (bytes, str)):
            cp.read_file(stream_or_string)
        else:
            cp.read_file(io.StringIO(stream_or_string))
        data = {}
        for section_name in cp.sections():
            section = {}
            for k, v in cp.items(section_name):
                section[k] = v
            data[section_name] = section
        return data
    except Exception as error:  # pylint: disable=broad-except
        raise DeserializationError(error)
Esempio n. 5
0
 def _deserialize(stream_or_string, **options):
     try:
         options.setdefault('use_list', True)
         return msgpack.loads(stream_or_string, **options)
     except Exception as error:
         raise DeserializationError(error)