~andre-miras/ladon/ticket1306028-soap-fix-lists-args

« back to all changes in this revision

Viewing changes to frameworks/python/src/ladon/types/__init__.py

  • Committer: jakob at simon-gaarde
  • Date: 2013-11-08 12:44:11 UTC
  • Revision ID: jakob@simon-gaarde.dk-20131108124411-sx00z3j2klenxq07
Now expanding request args

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
        else:
24
24
                return None
25
25
 
26
 
 
27
 
def result_to_dict(method_info,result,encoding='UTF-8',allow_unsafe_conversion=False,only_strings_to_unicode=True):
28
 
        """
29
 
        Convert the result of a method call to it's dictionary representation.
30
 
        """
31
 
        from ladon.types.typeconverter import TypeConverter
32
 
        from ladon.tools.multiparthandler import AttachmentHandler
33
 
        from ladon.exceptions.types import AttachmentExpected
34
 
        response_attachments = AttachmentHandler()
35
 
        tc = TypeConverter(encoding=encoding,allow_unsafe_conversion=allow_unsafe_conversion,only_strings_to_unicode=only_strings_to_unicode)
36
 
        res_dict = {
37
 
                'servicename': method_info.sinfo.servicename,
38
 
                'servicenumber': method_info.sinfo.servicenumber,
39
 
                'method': method_info.name()}
40
 
        typ = method_info._rtype
 
26
def expand_value(value,valtype, encoding='UTF-8',allow_unsafe_conversion=False,only_strings_to_unicode=True,service_name='unknown', method_name='unknown'):
 
27
        """
 
28
        Convert the result of a method call to it's dictionary representation.
 
29
        """
 
30
        from ladon.types.typeconverter import TypeConverter
 
31
        from ladon.tools.multiparthandler import AttachmentHandler
 
32
        from ladon.exceptions.types import AttachmentExpected
 
33
        response_attachments = AttachmentHandler()
 
34
        tc = TypeConverter(encoding=encoding,allow_unsafe_conversion=allow_unsafe_conversion,only_strings_to_unicode=only_strings_to_unicode)
 
35
        type_info = get_type_info(valtype)
 
36
        if type_info==None:
 
37
                if [list,tuple].count(type(valtype)):
 
38
                        ret = []
 
39
                        type_info = get_type_info(valtype[0])
 
40
                        if value == valtype:
 
41
                                # Assumption list attributes are always optional
 
42
                                return
 
43
                        
 
44
                        if type_info:
 
45
                                for item in value:
 
46
                                        ret += [item.__dict__(tc,response_attachments)]
 
47
                        elif valtype[0]==attachment:
 
48
                                for item in value:
 
49
                                        if not type(item) == attachment:
 
50
                                                raise AttachmentExpected(service_name,method_name,'Attachment expected got: %s' % type(item))
 
51
                                        ret += [response_attachments.add_attachment(item)]
 
52
                        else:
 
53
                                for item in value:
 
54
                                        ret += [tc.to_unicode_string(item,valtype[0])]
 
55
                elif valtype==attachment:
 
56
                        ret = response_attachments.add_attachment(value)
 
57
                else:
 
58
                        ret = tc.to_unicode_string(value,valtype)
 
59
        else:
 
60
                ret = value.__dict__(tc,response_attachments)
 
61
        
 
62
        return ret
 
63
 
 
64
 
 
65
def result_to_dict(method_info,result, encoding='UTF-8',allow_unsafe_conversion=False,only_strings_to_unicode=True):
 
66
        """
 
67
        Convert the result of a method call to it's dictionary representation.
 
68
        """
 
69
        from ladon.types.typeconverter import TypeConverter
 
70
        from ladon.tools.multiparthandler import AttachmentHandler
 
71
        from ladon.exceptions.types import AttachmentExpected
 
72
        response_attachments = AttachmentHandler()
 
73
        tc = TypeConverter(encoding=encoding,allow_unsafe_conversion=allow_unsafe_conversion,only_strings_to_unicode=only_strings_to_unicode)
 
74
        if method_info==None:
 
75
                res_dict = {}
 
76
                typ = type(result)
 
77
                servicename = 'None'
 
78
        else:
 
79
                res_dict = {
 
80
                        'servicename': method_info.sinfo.servicename,
 
81
                        'servicenumber': method_info.sinfo.servicenumber,
 
82
                        'method': method_info.name()}
 
83
                typ = method_info._rtype
 
84
                servicename = method_info.sinfo.servicename
41
85
        type_info = get_type_info(typ)
42
86
        if type_info==None:
43
87
                if [list,tuple].count(type(typ)):
54
98
                        elif typ[0]==attachment:
55
99
                                for item in result:
56
100
                                        if not type(item) == attachment:
57
 
                                                raise AttachmentExpected('unknown',method_info.sinfo.servicename,'Attachment expected got: %s' % type(item))
 
101
                                                raise AttachmentExpected('unknown',servicename,'Attachment expected got: %s' % type(item))
58
102
                                        result_list += [response_attachments.add_attachment(item)]
59
103
                        else:
60
104
                                for item in result: