Got rid of debugging line
[sfa.git] / wsdl / gw2wsdl.py
1 #!/usr/bin/python
2 #
3 # Sapan Bhatia <sapanb@cs.princeton.edu>
4 #
5 # Generates a WSDL for geniwrapper
6
7
8 import os, sys
9 import time
10 import pdb
11 import xml.dom.minidom
12 import xml.dom.ext
13 import apistub
14 import inspect
15
16 import globals
17
18 from types import *
19
20 from sfa.trust.auth import Auth
21 from sfa.util.parameter import Parameter,Mixed
22
23 complex_types = {}
24 services = {}
25
26 num_types = 0
27
28 class SoapError(Exception):
29      def __init__(self, value):
30          self.value = value
31      def __str__(self):
32          return repr(self.value)
33 try:
34     set
35 except NameError:
36     from sets import Set
37     set = Set
38
39 def fold_complex_type_names(acc, arg):
40     name = arg.doc
41     if (type(acc)==list):
42         acc.append(name)
43     else:
44         python_is_braindead = acc.doc
45         acc = [python_is_braindead,name]
46     return acc
47
48 def fold_complex_type(acc, arg):
49     global complex_types
50     name = name_complex_type(arg)
51     complex_types[arg]=name
52     if (type(acc)==list):
53         acc.append(name)
54     else:
55         python_is_braindead = name_complex_type(acc)
56         acc = [python_is_braindead,name]
57     return acc
58
59 def name_complex_type(arg):
60     global num_types
61     global types
62
63     types_section = types.getElementsByTagName("xsd:schema")[0]
64
65     #pdb.set_trace()
66     if (isinstance(arg, Mixed)):
67         inner_types = reduce(fold_complex_type, arg)
68         inner_names = reduce(fold_complex_type_names, arg)
69         if (inner_types[-1]=="none"):
70             inner_types=inner_types[:-1]
71             min_args = 0
72         else:
73             min_args = 1
74     
75         num_types=num_types+1
76         type_name = "Type%d"%num_types
77         complex_type = types_section.appendChild(types.createElement("xsd:complexType"))
78         complex_type.setAttribute("name", type_name)
79
80         choice = complex_type.appendChild(types.createElement("xsd:choice"))
81         for n,t in zip(inner_names,inner_types):
82             element = choice.appendChild(types.createElement("element"))
83             element.setAttribute("name", n)
84             element.setAttribute("type", "%s"%t)
85             element.setAttribute("minOccurs","%d"%min_args)
86         return "xsdl:%s"%type_name
87     elif (isinstance(arg, Parameter)):
88         return (name_simple_type(arg.type))
89     elif type(arg) == ListType or type(arg) == TupleType:
90         inner_type = name_complex_type(arg[0]) 
91         num_types=num_types+1
92         type_name = "Type%d"%num_types
93         complex_type = types_section.appendChild(types.createElement("xsd:simpleType"))
94         complex_type.setAttribute("name", type_name)
95         complex_content = complex_type.appendChild(types.createElement("xsd:list"))
96         complex_content.setAttribute("itemType",inner_type)
97         return "xsdl:%s"%type_name
98     elif type(arg) == DictType or arg == DictType or (inspect.isclass(arg) and issubclass(arg, dict)):
99         num_types=num_types+1
100         type_name = "Type%d"%num_types
101         complex_type = types_section.appendChild(types.createElement("xsd:complexType"))
102         complex_type.setAttribute("name", type_name)
103         complex_content = complex_type.appendChild(types.createElement("xsd:sequence"))
104  
105         for k in arg.fields:
106             inner_type = name_complex_type(arg.fields[k]) 
107             element=complex_content.appendChild(types.createElement("xsd:element"))
108             element.setAttribute("name",k)
109             element.setAttribute("type",inner_type)
110
111         return "xsdl:%s"%type_name 
112     else:
113         return (name_simple_type(arg))
114
115 def name_simple_type(arg_type):
116     # A Parameter is reported as an instance, even though it is serialized as a type <>
117     if type(arg_type) == InstanceType:
118         return (name_simple_type(arg_type.type))
119     if arg_type == None:
120         return "none"
121     if arg_type == DictType:
122         return "xsd:anyType"
123     elif arg_type == IntType or arg_type == LongType:
124         return "xsd:int"
125     elif arg_type == bool:
126         return "xsd:boolean"
127     elif arg_type == FloatType:
128         return "xsd:double"
129     elif arg_type in StringTypes:
130         return "xsd:string"
131     else:
132        pdb.set_trace()
133        raise SoapError, "Cannot handle %s objects" % arg_type
134
135 def param_type(arg):
136     return (name_complex_type(arg))
137
138 def add_wsdl_ports_and_bindings (wsdl):
139     for method in apistub.methods:
140         print "Processing method %s"%method
141
142         # Skip system. methods
143         if "system." in method:
144             continue
145
146         function = apistub.callable(method) # Commented documentation
147         #lines = ["// " + line.strip() for line in function.__doc__.strip().split("\n")]
148         #print "\n".join(lines)
149         #print
150
151         
152         in_el = wsdl.firstChild.appendChild(wsdl.createElement("message"))
153         in_el.setAttribute("name", method + "_in")
154
155         for service_name in function.interfaces:
156             if (services.has_key(service_name)):
157                 if (not method in services[service_name]):
158                     services[service_name].append(method)
159             else:
160                 services[service_name]=[method]
161
162         # Arguments
163
164         if (function.accepts):
165             (min_args, max_args, defaults) = function.args()
166             for (argname,argtype) in zip(max_args,function.accepts):
167                 arg_part = in_el.appendChild(wsdl.createElement("part"))
168                 arg_part.setAttribute("name", argname)
169                 arg_part.setAttribute("type", param_type(argtype))
170                 
171         # Return type            
172         return_type = function.returns
173         out_el = wsdl.firstChild.appendChild(wsdl.createElement("message"))
174         out_el.setAttribute("name", method + "_out")
175         ret_part = out_el.appendChild(wsdl.createElement("part"))
176         ret_part.setAttribute("name", "returnvalue")
177         ret_part.setAttribute("type", param_type(return_type))
178
179         # Port connecting arguments with return type
180
181         port_el = wsdl.firstChild.appendChild(wsdl.createElement("portType"))
182         port_el.setAttribute("name", method + "_port")
183         
184         op_el = port_el.appendChild(wsdl.createElement("operation"))
185         op_el.setAttribute("name", method)
186         inp_el=wsdl.createElement("input")
187         inp_el.setAttribute("message","tns:" + method + "_in")
188         inp_el.setAttribute("name",method+"_request")
189         op_el.appendChild(inp_el)
190         out_el = wsdl.createElement("output")
191         out_el.setAttribute("message","tns:" + method + "_out")
192         out_el.setAttribute("name",method+"_response")
193         op_el.appendChild(out_el)
194
195         # Bindings
196
197         bind_el = wsdl.firstChild.appendChild(wsdl.createElement("binding"))
198         bind_el.setAttribute("name", method + "_binding")
199         bind_el.setAttribute("type", "tns:" + method + "_port")
200         
201         soap_bind = bind_el.appendChild(wsdl.createElement("soap:binding"))
202         soap_bind.setAttribute("style", "rpc")
203         soap_bind.setAttribute("transport","http://schemas.xmlsoap.org/soap/http")
204
205         
206         wsdl_op = bind_el.appendChild(wsdl.createElement("operation"))
207         wsdl_op.setAttribute("name", method)
208         wsdl_op.appendChild(wsdl.createElement("soap:operation")).setAttribute("soapAction",
209                 "urn:" + method)
210
211         
212         wsdl_input = wsdl_op.appendChild(wsdl.createElement("input"))
213         input_soap_body = wsdl_input.appendChild(wsdl.createElement("soap:body"))
214         input_soap_body.setAttribute("use", "encoded")
215         input_soap_body.setAttribute("namespace", "urn:" + method)
216         input_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
217
218         
219         wsdl_output = wsdl_op.appendChild(wsdl.createElement("output"))
220         output_soap_body = wsdl_output.appendChild(wsdl.createElement("soap:body"))
221         output_soap_body.setAttribute("use", "encoded")
222         output_soap_body.setAttribute("namespace", "urn:" + method)
223         output_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
224         
225
226 def add_wsdl_service(wsdl):
227     for service in services.keys():
228         service_el = wsdl.firstChild.appendChild(wsdl.createElement("service"))
229         service_el.setAttribute("name", service)
230
231         for method in services[service]:
232             name=method
233             servport_el = service_el.appendChild(wsdl.createElement("port"))
234             servport_el.setAttribute("name", name + "_port")
235             servport_el.setAttribute("binding", "tns:" + name + "_binding")
236
237             soapaddress = servport_el.appendChild(wsdl.createElement("soap:address"))
238             soapaddress.setAttribute("location", "%s/%s" % (globals.plc_ns,service))
239
240
241 def get_wsdl_definitions():
242     wsdl_text_header = """
243         <wsdl:definitions
244         name="geniwrapper_autogenerated"
245         targetNamespace="%s/2009/07/sfa.wsdl"
246         xmlns="http://schemas.xmlsoap.org/wsdl/"
247         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
248         xmlns:xsdl="%s/2009/07/schema"
249         xmlns:tns="%s/2009/07/sfa.wsdl"
250         xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
251         xmlns:soapenc="http://schemas.xmlsoap.org/wsdl/soap/encoding"
252         xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>
253         """ % (globals.plc_ns,globals.plc_ns,globals.plc_ns)
254         
255     wsdl = xml.dom.minidom.parseString(wsdl_text_header)
256     
257     return wsdl
258
259 def get_wsdl_definitions_and_types():
260     wsdl_text_header = """
261     <wsdl:definitions
262         name="geniwrapper_autogenerated"
263         targetNamespace="%s/2009/07/sfa.wsdl"
264         xmlns="http://schemas.xmlsoap.org/wsdl/"
265         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
266         xmlns:xsdl="%s/2009/07/schema"
267         xmlns:tns="%s/2009/07/sfa.wsdl"
268         xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
269         xmlns:soapenc="http://schemas.xmlsoap.org/wsdl/soap/encoding"
270         xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
271         <types>
272             <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="%s/2009/07/schema"/>
273         </types>
274     </wsdl:definitions> """ % (globals.plc_ns, globals.plc_ns, globals.plc_ns, globals.plc_ns)
275     wsdl = xml.dom.minidom.parseString(wsdl_text_header)
276     return wsdl
277
278     
279 types = get_wsdl_definitions_and_types()
280
281 wsdl = get_wsdl_definitions()
282 add_wsdl_ports_and_bindings(wsdl)
283 wsdl_types = wsdl.importNode(types.getElementsByTagName("types")[0], True)
284 wsdl.firstChild.appendChild(wsdl_types)
285 add_wsdl_service(wsdl)
286
287 xml.dom.ext.PrettyPrint(wsdl)