• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

contrib/opal/ZSI/ZSI/generate/utility.py

00001 ############################################################################
00002 # Monte M. Goode, LBNL
00003 # See LBNLCopyright for copyright notice!
00004 ###########################################################################
00005 
00006 # utility classes used by new generator - mostly 'sugar' classes
00007 # that are actually imported by the generated code.  also includes
00008 # utilities used by wsdl2python itself.
00009 
00010 # $Id: utility.py 1226 2006-05-26 18:11:19Z boverhof $
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             # in some cases the empty list reassignment fails, so....
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 # ---- generated code utils
00114 
00115 class MessageContainer:
00116     """generator util - used by address.py"""
00117     pass
00118 
00119 # Extract sub names from message parts so they can be used when mapping
00120 #    a message's contents to a function's arguments.
00121 # Args is a list of Message Parts.  i.e.: op.getInputMessage().parts.values()
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                         # arg.name is the part name, but we want it's type
00138                         argElementType = arg.element[1]
00139                         if str(argElementType) == str(i.content.name):
00140                             argSubnames = []
00141                                      # I'm not sure when the name attribute was dropped
00142                                      # but at some point, or in some circumstance it's not
00143                                      # there, but instead a ref attribute is there which is
00144                                      # tuple of (namespace, name). This hack fixes things, 
00145                                      # but I'm not sure why this happens or has happened.
00146                                      # IRJ - 2005-05-25
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

Generated on Wed Oct 20 2010 11:12:15 for APBS by  doxygen 1.7.2