示例#1
0
文件: api.py 项目: avanov/formencode
    def unpack_errors(self, encode_variables=False, dict_char='.',
                      list_char='-'):
        """
        Returns the error as a simple data structure -- lists,
        dictionaries, and strings.

        If ``encode_variables`` is true, then this will return a flat
        dictionary, encoded with variable_encode
        """
        if self.error_list:
            assert not encode_variables, (
                "You can only encode dictionary errors")
            assert not self.error_dict
            return [item.unpack_errors() if item else item
                for item in self.error_list]
        if self.error_dict:
            result = {}
            for name, item in self.error_dict.iteritems():
                result[name] = item if isinstance(
                    item, basestring) else item.unpack_errors()
            if encode_variables:
                import variabledecode
                result = variabledecode.variable_encode(
                    result, add_repetitions=False,
                    dict_char=dict_char, list_char=list_char)
                for key in result.keys():
                    if not result[key]:
                        del result[key]
            return result
        assert not encode_variables, (
            "You can only encode dictionary errors")
        return self.msg
示例#2
0
    def unpack_errors(self,
                      encode_variables=False,
                      dict_char='.',
                      list_char='-'):
        """
        Returns the error as a simple data structure -- lists,
        dictionaries, and strings.

        If ``encode_variables`` is true, then this will return a flat
        dictionary, encoded with variable_encode
        """
        if self.error_list:
            assert not encode_variables, (
                "You can only encode dictionary errors")
            assert not self.error_dict
            result = []
            for item in self.error_list:
                if not item:
                    result.append(item)
                else:
                    result.append(item.unpack_errors())
            return result
        elif self.error_dict:
            result = {}
            for name, item in self.error_dict.items():
                if isinstance(item, (str, unicode)):
                    result[name] = item
                else:
                    result[name] = item.unpack_errors()
            if encode_variables:
                import variabledecode
                result = variabledecode.variable_encode(result,
                                                        add_repetitions=False,
                                                        dict_char=dict_char,
                                                        list_char=list_char)
                for key in result.keys():
                    if not result[key]:
                        del result[key]
            return result
        else:
            assert not encode_variables, (
                "You can only encode dictionary errors")
            return self.msg