00001
00002
00003
00004
00005
00006 import ZSI
00007 from ZSI import TC, TCtimes, TCcompound
00008 from ZSI.TC import TypeCode
00009 from ZSI import _copyright, EvaluateException
00010 from ZSI.wstools.Utility import SplitQName
00011 from ZSI.wstools.Namespaces import SOAP, SCHEMA
00012
00013
00014
00015
00016
00017 class NamespaceException(Exception): pass
00018 class BaseTypeInterpreter:
00019 """Example mapping of xsd/soapenc types to zsi python types.
00020 Checks against all available classes in ZSI.TC. Used in
00021 wsdl2python, wsdlInterpreter, and ServiceProxy.
00022 """
00023
00024 def __init__(self):
00025 self._type_list = [TC.Iinteger, TC.IunsignedShort, TC.gYearMonth, \
00026 TC.InonNegativeInteger, TC.Iint, TC.String, \
00027 TC.gDateTime, TC.IunsignedInt, TC.Duration,\
00028 TC.IpositiveInteger, TC.FPfloat, TC.gDay, TC.gMonth, \
00029 TC.InegativeInteger, TC.gDate, TC.URI, \
00030 TC.HexBinaryString, TC.IunsignedByte, \
00031 TC.gMonthDay, TC.InonPositiveInteger, \
00032 TC.Ibyte, TC.FPdouble, TC.gTime, TC.gYear, \
00033 TC.Ilong, TC.IunsignedLong, TC.Ishort, \
00034 TC.Token, TC.QName]
00035
00036 self._tc_to_int = [
00037 ZSI.TCnumbers.IEnumeration,
00038 ZSI.TCnumbers.Iint,
00039 ZSI.TCnumbers.Iinteger,
00040 ZSI.TCnumbers.Ilong,
00041 ZSI.TCnumbers.InegativeInteger,
00042 ZSI.TCnumbers.InonNegativeInteger,
00043 ZSI.TCnumbers.InonPositiveInteger,
00044 ZSI.TC.Integer,
00045 ZSI.TCnumbers.IpositiveInteger,
00046 ZSI.TCnumbers.Ishort]
00047
00048 self._tc_to_float = [
00049 ZSI.TC.Decimal,
00050 ZSI.TCnumbers.FPEnumeration,
00051 ZSI.TCnumbers.FPdouble,
00052 ZSI.TCnumbers.FPfloat]
00053
00054 self._tc_to_string = [
00055 ZSI.TC.Base64String,
00056 ZSI.TC.Enumeration,
00057 ZSI.TC.HexBinaryString,
00058 ZSI.TCnumbers.Ibyte,
00059 ZSI.TCnumbers.IunsignedByte,
00060 ZSI.TCnumbers.IunsignedInt,
00061 ZSI.TCnumbers.IunsignedLong,
00062 ZSI.TCnumbers.IunsignedShort,
00063 ZSI.TC.String,
00064 ZSI.TC.URI,
00065 ZSI.TC.XMLString,
00066 ZSI.TC.Token]
00067
00068 self._tc_to_tuple = [
00069 ZSI.TC.Duration,
00070 ZSI.TC.QName,
00071 ZSI.TCtimes.gDate,
00072 ZSI.TCtimes.gDateTime,
00073 ZSI.TCtimes.gDay,
00074 ZSI.TCtimes.gMonthDay,
00075 ZSI.TCtimes.gTime,
00076 ZSI.TCtimes.gYear,
00077 ZSI.TCtimes.gMonth,
00078 ZSI.TCtimes.gYearMonth]
00079
00080 return
00081
00082 def _get_xsd_typecode(self, msg_type):
00083 untaged_xsd_types = {'boolean':TC.Boolean,
00084 'decimal':TC.Decimal,
00085 'base64Binary':TC.Base64String}
00086 if untaged_xsd_types.has_key(msg_type):
00087 return untaged_xsd_types[msg_type]
00088 for tc in self._type_list:
00089 if tc.type == (SCHEMA.XSD3,msg_type):
00090 break
00091 else:
00092 tc = TC.AnyType
00093 return tc
00094
00095 def _get_soapenc_typecode(self, msg_type):
00096 if msg_type == 'Array':
00097 return TCcompound.Array
00098 if msg_type == 'Struct':
00099 return TCcompound.Struct
00100
00101 return self._get_xsd_typecode(msg_type)
00102
00103 def get_typeclass(self, msg_type, targetNamespace):
00104 prefix, name = SplitQName(msg_type)
00105 if targetNamespace in SCHEMA.XSD_LIST:
00106 return self._get_xsd_typecode(name)
00107 elif targetNamespace in [SOAP.ENC]:
00108 return self._get_soapenc_typecode(name)
00109 return None
00110
00111 def get_pythontype(self, msg_type, targetNamespace, typeclass=None):
00112 if not typeclass:
00113 tc = self.get_typeclass(msg_type, targetNamespace)
00114 else:
00115 tc = typeclass
00116 if tc in self._tc_to_int:
00117 return 'int'
00118 elif tc in self._tc_to_float:
00119 return 'float'
00120 elif tc in self._tc_to_string:
00121 return 'str'
00122 elif tc in self._tc_to_tuple:
00123 return 'tuple'
00124 elif tc in [TCcompound.Array]:
00125 return 'list'
00126 elif tc in [TC.Boolean]:
00127 return 'bool'
00128 elif isinstance(tc, TypeCode):
00129 raise EvaluateException,\
00130 'failed to map zsi typecode to a python type'
00131 return None
00132
00133