python3 - 2to3 + miscell obvious tweaks
[sfa.git] / wsdl / sfa2wsdl.py
index 741cbdf..f6fd2ea 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 #
 # Sapan Bhatia <sapanb@cs.princeton.edu>
 #
@@ -9,16 +9,17 @@ import os, sys
 import time
 import pdb
 import xml.dom.minidom
-import xml.dom.ext
 import apistub
 import inspect
 
 from types import *
 from optparse import OptionParser
 
-from sfa.util.parameter import Parameter,Mixed
+from sfa.storage.parameter import Parameter, Mixed
 
-import globals
+from sfa.util.py23 import StringType
+
+plc_ns="http://www.planet-lab.org/sfa"
 
 class SoapError(Exception):
      def __init__(self, value):
@@ -42,17 +43,14 @@ class WSDLGen:
         self.interface_options = interface_options
 
     def interface_name (self):
-         if self.interface_options.aggregate and \
-                  self.interface_options.slicemgr and \
-                  self.interface_options.registry:
+         if (self.interface_options.aggregate and
+             self.interface_options.registry):
               return "complete"
          if self.interface_options.aggregate: return "aggregate"
-         elif self.interface_options.slicemgr: return "slicemgr"
          elif self.interface_options.registry: return "registry"
-         elif self.interface_options.component: return "component"
          else: return "unknown"
 
-    def filter_argname(self,argname):
+    def filter_argname(self, argname):
         if (not self.interface_options.lite or (argname!="cred")):
             if (argname.find('(') != -1):
                 # The name has documentation in it :-/
@@ -63,24 +61,24 @@ class WSDLGen:
                 argname = inbrack
         return argname
 
-    def fold_complex_type_names(self,acc, arg):
-        name = arg.doc
-        if (type(acc)==list):
-            acc.append(name)
-        else:
-            p_i_b = acc.doc
-            acc = [p_i_b,name]
-        return acc
-
-    def fold_complex_type(self,acc, arg):
-        name = self.name_complex_type(arg)
-        self.complex_types[arg]=name
-        if (type(acc)==list):
-            acc.append(name)
-        else:
-            p_i_b = self.name_complex_type(acc)
-            acc = [p_i_b,name]
-        return acc
+#    def fold_complex_type_names(self,acc, arg):
+#        name = arg.doc
+#        if (type(acc)==list):
+#            acc.append(name)
+#        else:
+#            p_i_b = acc.doc
+#            acc = [p_i_b,name]
+#        return acc
+#
+#    def fold_complex_type(self,acc, arg):
+#        name = self.name_complex_type(arg)
+#        self.complex_types[arg]=name
+#        if (type(acc)==list):
+#            acc.append(name)
+#        else:
+#            p_i_b = self.name_complex_type(acc)
+#            acc = [p_i_b,name]
+#        return acc
 
     def name_complex_type(self,arg):
 
@@ -88,21 +86,23 @@ class WSDLGen:
 
         #pdb.set_trace()
         if (isinstance(arg, Mixed)):
-            inner_types = reduce(self.fold_complex_type, arg)
-            inner_names = reduce(self.fold_complex_type_names, arg)
+#            inner_types = reduce(self.fold_complex_type, arg)
+#            inner_names = reduce(self.fold_complex_type_names, arg)
+            inner_types = [ self.name_complex_type(x) for x in arg ]
+            inner_names = [ x.doc for x in arg ]
             if (inner_types[-1]=="none"):
                 inner_types=inner_types[:-1]
                 min_args = 0
             else:
                 min_args = 1
-        
-            self.num_types=self.num_types+1
+
+            self.num_types += 1
             type_name = "Type%d"%self.num_types
             complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
             complex_type.setAttribute("name", type_name)
 
             choice = complex_type.appendChild(self.types.createElement("xsd:choice"))
-            for n,t in zip(inner_names,inner_types):
+            for (n,t) in zip(inner_names,inner_types):
                 element = choice.appendChild(self.types.createElement("element"))
                 n = self.filter_argname(n)
                 element.setAttribute("name", n)
@@ -111,8 +111,8 @@ class WSDLGen:
             return "xsdl:%s"%type_name
         elif (isinstance(arg, Parameter)):
             return (self.name_simple_type(arg.type))
-        elif type(arg) == ListType or type(arg) == TupleType:
-            inner_type = self.name_complex_type(arg[0]) 
+        elif type(arg) in ( ListType , TupleType ):
+            inner_type = self.name_complex_type(arg[0])
             self.num_types=self.num_types+1
             type_name = "Type%d"%self.num_types
             complex_type = types_section.appendChild(self.types.createElement("xsd:complexType"))
@@ -134,14 +134,14 @@ class WSDLGen:
             type_name = self.filter_argname(type_name)
             complex_type.setAttribute("name", type_name)
             complex_content = complex_type.appendChild(self.types.createElement("xsd:sequence"))
-     
+
             for k in arg.fields:
-                inner_type = self.name_complex_type(arg.fields[k]) 
+                inner_type = self.name_complex_type(arg.fields[k])
                 element=complex_content.appendChild(self.types.createElement("xsd:element"))
                 element.setAttribute("name",k)
                 element.setAttribute("type",inner_type)
 
-            return "xsdl:%s"%type_name 
+            return "xsdl:%s"%type_name
         else:
             return (self.name_simple_type(arg))
 
@@ -161,11 +161,11 @@ class WSDLGen:
             return "xsd:boolean"
         elif arg_type == FloatType:
             return "xsd:double"
-        elif arg_type in StringTypes:
+        elif issubclass(arg_type, StringType):
             return "xsd:string"
         else:
            pdb.set_trace()
-           raise SoapError, "Cannot handle %s objects" % arg_type
+           raise SoapError("Cannot handle %s objects" % arg_type)
 
     def param_type(self, arg):
         return (self.name_complex_type(arg))
@@ -182,12 +182,12 @@ class WSDLGen:
             #print "\n".join(lines)
             #print
 
-            
+
             in_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("message"))
             in_el.setAttribute("name", method + "_in")
 
             for service_name in function.interfaces:
-                if (self.services.has_key(service_name)):
+                if (service_name in self.services):
                     if (not method in self.services[service_name]):
                         self.services[service_name].append(method)
                 else:
@@ -202,8 +202,8 @@ class WSDLGen:
                     arg_part = in_el.appendChild(self.wsdl.createElement("part"))
                     arg_part.setAttribute("name", argname)
                     arg_part.setAttribute("type", self.param_type(argtype))
-                    
-            # Return type            
+
+            # Return type
             return_type = function.returns
             out_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("message"))
             out_el.setAttribute("name", method + "_out")
@@ -215,7 +215,7 @@ class WSDLGen:
 
             port_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("portType"))
             port_el.setAttribute("name", method + "_port")
-            
+
             op_el = port_el.appendChild(self.wsdl.createElement("operation"))
             op_el.setAttribute("name", method)
             inp_el=self.wsdl.createElement("input")
@@ -232,34 +232,34 @@ class WSDLGen:
             bind_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("binding"))
             bind_el.setAttribute("name", method + "_binding")
             bind_el.setAttribute("type", "tns:" + method + "_port")
-            
+
             soap_bind = bind_el.appendChild(self.wsdl.createElement("soap:binding"))
             soap_bind.setAttribute("style", "rpc")
             soap_bind.setAttribute("transport","http://schemas.xmlsoap.org/soap/http")
 
-            
+
             wsdl_op = bind_el.appendChild(self.wsdl.createElement("operation"))
             wsdl_op.setAttribute("name", method)
             wsdl_op.appendChild(self.wsdl.createElement("soap:operation")).setAttribute("soapAction",
                     "urn:" + method)
 
-            
+
             wsdl_input = wsdl_op.appendChild(self.wsdl.createElement("input"))
             input_soap_body = wsdl_input.appendChild(self.wsdl.createElement("soap:body"))
             input_soap_body.setAttribute("use", "encoded")
             input_soap_body.setAttribute("namespace", "urn:" + method)
             input_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
 
-            
+
             wsdl_output = wsdl_op.appendChild(self.wsdl.createElement("output"))
             output_soap_body = wsdl_output.appendChild(self.wsdl.createElement("soap:body"))
             output_soap_body.setAttribute("use", "encoded")
             output_soap_body.setAttribute("namespace", "urn:" + method)
             output_soap_body.setAttribute("encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
-            
+
 
     def add_wsdl_services(self):
-        for service in self.services.keys():
+        for service in list(self.services.keys()):
             if (getattr(self.interface_options,service)):
                 service_el = self.wsdl.lastChild.appendChild(self.wsdl.createElement("service"))
                 service_el.setAttribute("name", service)
@@ -271,7 +271,7 @@ class WSDLGen:
                         servport_el.setAttribute("binding", "tns:" + name + "_binding")
 
                         soapaddress = servport_el.appendChild(self.wsdl.createElement("soap:address"))
-                        soapaddress.setAttribute("location", "%s/%s" % (globals.plc_ns,service))
+                        soapaddress.setAttribute("location", "%s/%s" % (plc_ns,service))
 
 
     def compute_wsdl_definitions(self):
@@ -287,10 +287,10 @@ class WSDLGen:
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
             xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
             xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"/>
-            """ % (self.interface_name(),globals.plc_ns,globals.plc_ns,globals.plc_ns)
-            
+            """ % (self.interface_name(),plc_ns,plc_ns,plc_ns)
+
         self.wsdl = xml.dom.minidom.parseString(wsdl_text_header)
-        
+
 
     def compute_wsdl_definitions_and_types(self):
         wsdl_text_header = """
@@ -308,9 +308,9 @@ class WSDLGen:
             <types>
                 <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="%s/schema"/>
             </types>
-        </wsdl:definitions> """ % (self.interface_name(),globals.plc_ns, globals.plc_ns, globals.plc_ns, globals.plc_ns)
+        </wsdl:definitions> """ % (self.interface_name(),plc_ns, plc_ns, plc_ns, plc_ns)
         self.types = xml.dom.minidom.parseString(wsdl_text_header)
-        
+
 
     def add_wsdl_types(self):
         wsdl_types = self.wsdl.importNode(self.types.getElementsByTagName("types")[0], True)
@@ -325,21 +325,16 @@ class WSDLGen:
 
     def pretty_print(self):
         if (self.wsdl):
-            xml.dom.ext.PrettyPrint(self.wsdl)
+             print(xml.dom.minidom.Document.toprettyxml(self.wsdl))
         else:
-            raise Exception("Empty WSDL")
+             raise Exception("Empty WSDL")
 
 def main():
     parser = OptionParser()
-    parser.add_option("-r", "--registry", dest="registry", action="store_true", 
+    parser.add_option("-r", "--registry", dest="registry", action="store_true",
                               help="Generate registry.wsdl", metavar="FILE")
-    parser.add_option("-s", "--slice-manager",
-                              action="store_true", dest="slicemgr", 
-                              help="Generate sm.wsdl")
     parser.add_option("-a", "--aggregate", action="store_true", dest="aggregate",
                               help="Generate am.wsdl")
-    parser.add_option("-c", "--component", action="store_true", dest="component",
-                              help="Generate cm.wsdl")
     parser.add_option("-g", "--geni-aggregate", action="store_true", dest="geni_am",
                       help="Generate gm.wsdl")
     parser.add_option("-l", "--lite", action="store_true", dest="lite",
@@ -349,7 +344,7 @@ def main():
     gen = WSDLGen(interface_options)
     gen.generate_wsdl()
     gen.pretty_print()
-    
+
 
 if __name__ == "__main__":
         main()