Package suds :: Package xsd :: Module sxbuiltin
[hide private]
[frames] | no frames]

Source Code for Module suds.xsd.sxbuiltin

  1  # This program is free software; you can redistribute it and/or modify 
  2  # it under the terms of the (LGPL) GNU Lesser General Public License as 
  3  # published by the Free Software Foundation; either version 3 of the  
  4  # License, or (at your option) any later version. 
  5  # 
  6  # This program is distributed in the hope that it will be useful, 
  7  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
  8  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  9  # GNU Library Lesser General Public License for more details at 
 10  # ( http://www.gnu.org/licenses/lgpl.html ). 
 11  # 
 12  # You should have received a copy of the GNU Lesser General Public License 
 13  # along with this program; if not, write to the Free Software 
 14  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 15  # written by: Jeff Ortel ( jortel@redhat.com ) 
 16   
 17  """ 
 18  The I{sxbuiltin} module provides classes that represent 
 19  XSD I{builtin} schema objects. 
 20  """ 
 21   
 22  from logging import getLogger 
 23  from suds import * 
 24  from suds.xsd import * 
 25  from suds.sax.date import * 
 26  from suds.xsd.sxbase import XBuiltin 
 27  import datetime as dt 
 28   
 29   
 30  log = getLogger(__name__) 
31 32 33 -class XString(XBuiltin):
34 """ 35 Represents an (xsd) <xs:string/> node 36 """ 37 pass
38
39 40 -class XAny(XBuiltin):
41 """ 42 Represents an (xsd) <any/> node 43 """ 44
45 - def __init__(self, schema, name):
46 XBuiltin.__init__(self, schema, name) 47 self.nillable = False
48
49 - def get_child(self, name):
50 child = XAny(self.schema, name) 51 return (child, [])
52
53 - def any(self):
54 return True
55
56 57 -class XBoolean(XBuiltin):
58 """ 59 Represents an (xsd) boolean builtin type. 60 """ 61 62 translation = ( 63 { '1':True,'true':True,'0':False,'false':False }, 64 { True:'true',1:'true',False:'false',0:'false' }, 65 ) 66
67 - def translate(self, value, topython=True):
68 if topython: 69 if isinstance(value, basestring): 70 return XBoolean.translation[0].get(value) 71 else: 72 return None 73 else: 74 if isinstance(value, (bool,int)): 75 return XBoolean.translation[1].get(value) 76 else: 77 return value
78
79 80 -class XInteger(XBuiltin):
81 """ 82 Represents an (xsd) xs:int builtin type. 83 """ 84
85 - def translate(self, value, topython=True):
86 if topython: 87 if isinstance(value, basestring) and len(value): 88 return int(value) 89 else: 90 return None 91 else: 92 if isinstance(value, int): 93 return str(value) 94 else: 95 return value
96
97 -class XLong(XBuiltin):
98 """ 99 Represents an (xsd) xs:long builtin type. 100 """ 101
102 - def translate(self, value, topython=True):
103 if topython: 104 if isinstance(value, basestring) and len(value): 105 return long(value) 106 else: 107 return None 108 else: 109 if isinstance(value, (int,long)): 110 return str(value) 111 else: 112 return value
113
114 115 -class XFloat(XBuiltin):
116 """ 117 Represents an (xsd) xs:float builtin type. 118 """ 119
120 - def translate(self, value, topython=True):
121 if topython: 122 if isinstance(value, basestring) and len(value): 123 return float(value) 124 else: 125 return None 126 else: 127 if isinstance(value, float): 128 return str(value) 129 else: 130 return value
131
132 133 -class XDate(XBuiltin):
134 """ 135 Represents an (xsd) xs:date builtin type. 136 """ 137
138 - def translate(self, value, topython=True):
139 if topython: 140 if isinstance(value, basestring) and len(value): 141 return Date(value).date 142 else: 143 return None 144 else: 145 if isinstance(value, dt.date): 146 return str(Date(value)) 147 else: 148 return value
149
150 151 -class XTime(XBuiltin):
152 """ 153 Represents an (xsd) xs:time builtin type. 154 """ 155
156 - def translate(self, value, topython=True):
157 if topython: 158 if isinstance(value, basestring) and len(value): 159 return Time(value).time 160 else: 161 return None 162 else: 163 if isinstance(value, dt.date): 164 return str(Time(value)) 165 else: 166 return value
167
168 169 -class XDateTime(XBuiltin):
170 """ 171 Represents an (xsd) xs:datetime builtin type. 172 """ 173
174 - def translate(self, value, topython=True):
175 if topython: 176 if isinstance(value, basestring) and len(value): 177 return DateTime(value).datetime 178 else: 179 return None 180 else: 181 if isinstance(value, dt.date): 182 return str(DateTime(value)) 183 else: 184 return value
185
186 187 -class Factory:
188 189 tags =\ 190 { 191 # any 192 'anyType' : XAny, 193 # strings 194 'string' : XString, 195 'normalizedString' : XString, 196 'ID' : XString, 197 'Name' : XString, 198 'QName' : XString, 199 'NCName' : XString, 200 'anySimpleType' : XString, 201 'anyURI' : XString, 202 'NOTATION' : XString, 203 'token' : XString, 204 'language' : XString, 205 'IDREFS' : XString, 206 'ENTITIES' : XString, 207 'IDREF' : XString, 208 'ENTITY' : XString, 209 'NMTOKEN' : XString, 210 'NMTOKENS' : XString, 211 # binary 212 'hexBinary' : XString, 213 'base64Binary' : XString, 214 # integers 215 'int' : XInteger, 216 'integer' : XInteger, 217 'unsignedInt' : XInteger, 218 'positiveInteger' : XInteger, 219 'negativeInteger' : XInteger, 220 'nonPositiveInteger' : XInteger, 221 'nonNegativeInteger' : XInteger, 222 # longs 223 'long' : XLong, 224 'unsignedLong' : XLong, 225 # shorts 226 'short' : XInteger, 227 'unsignedShort' : XInteger, 228 'byte' : XInteger, 229 'unsignedByte' : XInteger, 230 # floats 231 'float' : XFloat, 232 'double' : XFloat, 233 'decimal' : XFloat, 234 # dates & times 235 'date' : XDate, 236 'time' : XTime, 237 'dateTime': XDateTime, 238 'duration': XString, 239 'gYearMonth' : XString, 240 'gYear' : XString, 241 'gMonthDay' : XString, 242 'gDay' : XString, 243 'gMonth' : XString, 244 # boolean 245 'boolean' : XBoolean, 246 } 247 248 @classmethod
249 - def maptag(cls, tag, fn):
250 """ 251 Map (override) tag => I{class} mapping. 252 @param tag: An xsd tag name. 253 @type tag: str 254 @param fn: A function or class. 255 @type fn: fn|class. 256 """ 257 cls.tags[tag] = fn
258 259 @classmethod
260 - def create(cls, schema, name):
261 """ 262 Create an object based on the root tag name. 263 @param schema: A schema object. 264 @type schema: L{schema.Schema} 265 @param name: The name. 266 @type name: str 267 @return: The created object. 268 @rtype: L{XBuiltin} 269 """ 270 fn = cls.tags.get(name) 271 if fn is not None: 272 return fn(schema, name) 273 else: 274 return XBuiltin(schema, name)
275