00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 import re
00013 from ZSI import EvaluateException
00014 from ZSI.TCcompound import Struct
00015 from ZSI.generate import WsdlGeneratorError, Wsdl2PythonError
00016 from ZSI.wstools.Utility import SplitQName
00017 from ZSI.wstools.Namespaces import SCHEMA
00018
00019 NCName_to_ModuleName = lambda name: re.sub('\.', '_', TextProtect(name))
00020 NCName_to_ClassName = lambda name: re.sub('\.', '_', TextProtect(name))
00021 TextProtect = lambda s: re.sub('[-./:# ]', '_', s)
00022 TextProtectAttributeName = lambda name: TextProtect('_%s' %name)
00023 Namespace2ModuleName = lambda ns: TextProtect(ns.lstrip('http://')).rstrip('_')
00024
00025
00026 def GetModuleBaseNameFromWSDL(wsdl):
00027 """By default try to construct a reasonable base name for all
00028 generated modules. Otherwise return None.
00029 """
00030 base_name = wsdl.name or wsdl.services[0].name
00031 base_name = SplitQName(base_name)[1]
00032 if base_name is None:
00033 return None
00034 return NCName_to_ModuleName(base_name)
00035
00036 namespace_name = lambda cls, ns: 'ns%s' % len(cls.alias_list)
00037
00038 class NamespaceAliasDict:
00039 """a lookup table to store relevant namespaces and their aliases"""
00040 alias_dict = {}
00041 alias_list = []
00042
00043 def add(cls, ns):
00044 if cls.alias_dict.has_key(ns):
00045 return
00046 cls.alias_dict[ns] = (Namespace2ModuleName(ns), '%s' % namespace_name(cls,ns))
00047 cls.alias_list.append(ns)
00048 add = classmethod(add)
00049
00050 def getModuleName(cls, ns):
00051 if cls.alias_dict.has_key(ns):
00052 return cls.alias_dict[ns][0]
00053
00054 msg = 'failed to find import for schema "%s"'%ns +\
00055 'possibly missing @schemaLocation attribute.'
00056 if ns in SCHEMA.XSD_LIST:
00057 msg = 'missing built-in typecode for schema "%s"' %ns
00058
00059 raise WsdlGeneratorError, msg
00060
00061 getModuleName = classmethod(getModuleName)
00062
00063 def getAlias(cls, ns):
00064 if cls.alias_dict.has_key(ns):
00065 return cls.alias_dict[ns][1]
00066
00067 msg = 'failed to find import for schema "%s"'%ns +\
00068 'possibly missing @schemaLocation attribute.'
00069 if ns in SCHEMA.XSD_LIST:
00070 msg = 'missing built-in typecode for schema "%s"' %ns
00071
00072 raise WsdlGeneratorError, msg
00073
00074 getAlias = classmethod(getAlias)
00075
00076 def getNSList(cls):
00077 return tuple(cls.alias_list)
00078 getNSList = classmethod(getNSList)
00079
00080
00081 class StringWriter:
00082 """generator util"""
00083 def __init__(self, val=None):
00084 self.data = []
00085 if val:
00086 self.data.append(val)
00087
00088 def set(self, val):
00089 if self.data:
00090
00091 self.data = None
00092 self.data = []
00093
00094 self.data.append(val)
00095
00096 def write(self, val):
00097 self.data.append(val)
00098
00099 def getvalue(self):
00100 if self.data:
00101 return ''.join(self.data)
00102 else:
00103 return ''
00104
00105 def __iadd__(self, val):
00106 self.data.append(val)
00107 return self
00108
00109 def __str__(self):
00110 return self.getvalue()
00111
00112
00113
00114
00115 class MessageContainer:
00116 """generator util - used by address.py"""
00117 pass
00118
00119
00120
00121
00122 def GetPartsSubNames(args, wsdl):
00123 do_extended = True
00124 from wsdl2python import WriteServiceModule, SchemaDescription
00125 wsm = WriteServiceModule(wsdl, do_extended=do_extended)
00126 wsm.gatherNamespaces()
00127 toReturn = []
00128 for arg in args:
00129 argSubnames = []
00130 for l in wsm.usedNamespaces.values():
00131 for schema in l:
00132 sd = SchemaDescription(do_extended=do_extended)
00133 sd.fromSchema(schema)
00134 argNamespace = arg.element[0]
00135 if (sd.targetNamespace == argNamespace):
00136 for i in sd.items:
00137
00138 argElementType = arg.element[1]
00139 if str(argElementType) == str(i.content.name):
00140 argSubnames = []
00141
00142
00143
00144
00145
00146
00147 if i.content.mgContent != None:
00148 for c in i.content.mgContent:
00149 nValue = "None"
00150 if c.isWildCard():
00151 nValue="any"
00152 elif c.attributes.has_key("name"):
00153 nValue = c.attributes["name"]
00154 elif c.attributes.has_key("ref"):
00155 nValue = c.attributes["ref"][1]
00156 argSubnames.append(nValue)
00157
00158 toReturn.append(argSubnames)
00159 return toReturn