def from_urlencoded(self, urlencoded, **kwargs):
        """
        Starting with a string of the application/x-www-form-urlencoded format
        this method creates a class instance

        :param urlencoded: The string
        :return: A class instance or raise an exception on error
        """

        # parse_qs returns a dictionary with keys and values. The values are
        # always lists even if there is only one value in the list.
        # keys only appears once.

        if isinstance(urlencoded, str):
            pass
        elif isinstance(urlencoded, list):
            urlencoded = urlencoded[0]

        _spec = self.c_param

        _info = parse_qs(urlencoded)
        if len(urlencoded) and _info == {}:
            raise FormatError('Wrong format')

        for key, val in _info.items():
            try:
                (typ, _, _, _deser, _) = _spec[key]
            except KeyError:
                try:
                    _key = key.split("#")[0]
                    (typ, _, _, _deser, _) = _spec[_key]
                except (ValueError, KeyError):
                    try:
                        (typ, _, _, _deser, _) = _spec['*']
                    except KeyError:
                        if len(val) == 1:
                            val = val[0]

                        self._dict[key] = val
                        continue

            if isinstance(typ, list):
                if _deser:
                    self._dict[key] = _deser(val[0], "urlencoded")
                else:
                    self._dict[key] = val
            else:  # must be single value
                if len(val) == 1:
                    if _deser:
                        self._dict[key] = _deser(val[0], "urlencoded")
                    elif isinstance(val[0], typ):
                        self._dict[key] = val[0]
                    else:
                        self._dict[key] = val[0]
                else:
                    raise TooManyValues('{}'.format(key))

        return self
Exemple #2
0
def deserialize_from_one_of(val, msgtype, sformat):
    if sformat in ["dict", "json"]:
        flist = ["json", "urlencoded"]
        if not isinstance(val, str):
            val = json.dumps(val)
    else:
        flist = ["urlencoded", "json"]

    for _format in flist:
        try:
            return msgtype().deserialize(val, _format)
        except FormatError:
            pass
    raise FormatError("Unexpected format")
    def deserialize(self, info, method="urlencoded", **kwargs):
        """
        Convert from an external representation to an internal.

        :param info: The input
        :param method: The method used to deserialize the info
        :param kwargs: extra Keyword arguments
        :return: In the normal case the Message instance
        """
        try:
            func = getattr(self, "from_%s" % method)
        except AttributeError:
            raise FormatError("Unknown serialization method (%s)" % method)
        else:
            return func(info, **kwargs)