83
83
SCM_SENDER_VOUCHES = "urn:oasis:names:tc:SAML:2.0:cm:sender-vouches"
84
84
SCM_BEARER = "urn:oasis:names:tc:SAML:2.0:cm:bearer"
85
85
86
- XSD = "xs: "
86
+ XSD = "xs"
87
87
NS_SOAP_ENC = "http://schemas.xmlsoap.org/soap/encoding/"
88
88
89
89
@@ -163,81 +163,81 @@ def clear_type(self):
163
163
pass
164
164
165
165
def set_text (self , value , base64encode = False ):
166
- def _wrong_type_value (xs_type , value ):
167
- msg = _str ('Type and value do not match: {xs_type }:{type}:{value}' )
168
- msg = msg .format (xs_type = xs_type , type = type (value ), value = value )
166
+ def _wrong_type_value (xsd , value ):
167
+ msg = _str ('Type and value do not match: {xsd }:{type}:{value}' )
168
+ msg = msg .format (xsd = xsd , type = type (value ), value = value )
169
169
raise ValueError (msg )
170
170
171
171
# only work with six.string_types
172
172
_str = unicode if six .PY2 else str
173
173
if isinstance (value , six .binary_type ):
174
174
value = value .decode ()
175
175
176
- xs_type_from_type = {
177
- _str : 'xs: string' ,
178
- int : 'xs: integer' ,
179
- float : 'xs: float' ,
180
- bool : 'xs: boolean' ,
176
+ type_to_xsd = {
177
+ _str : 'string' ,
178
+ int : 'integer' ,
179
+ float : 'float' ,
180
+ bool : 'boolean' ,
181
181
type (None ): '' ,
182
182
}
183
183
184
- # entries of xs -types each declaring:
184
+ # entries of xsd -types each declaring:
185
185
# - a corresponding python type
186
186
# - a function to turn a string into that type
187
187
# - a function to turn that type into a text-value
188
- xs_types_map = {
189
- 'xs: string' : {
188
+ xsd_types_props = {
189
+ 'string' : {
190
190
'type' : _str ,
191
191
'to_type' : _str ,
192
192
'to_text' : _str ,
193
193
},
194
- 'xs: integer' : {
194
+ 'integer' : {
195
195
'type' : int ,
196
196
'to_type' : int ,
197
197
'to_text' : _str ,
198
198
},
199
- 'xs: short' : {
199
+ 'short' : {
200
200
'type' : int ,
201
201
'to_type' : int ,
202
202
'to_text' : _str ,
203
203
},
204
- 'xs: int' : {
204
+ 'int' : {
205
205
'type' : int ,
206
206
'to_type' : int ,
207
207
'to_text' : _str ,
208
208
},
209
- 'xs: long' : {
209
+ 'long' : {
210
210
'type' : int ,
211
211
'to_type' : int ,
212
212
'to_text' : _str ,
213
213
},
214
- 'xs: float' : {
214
+ 'float' : {
215
215
'type' : float ,
216
216
'to_type' : float ,
217
217
'to_text' : _str ,
218
218
},
219
- 'xs: double' : {
219
+ 'double' : {
220
220
'type' : float ,
221
221
'to_type' : float ,
222
222
'to_text' : _str ,
223
223
},
224
- 'xs: boolean' : {
224
+ 'boolean' : {
225
225
'type' : bool ,
226
226
'to_type' : lambda x : {
227
227
'true' : True ,
228
228
'false' : False ,
229
229
}[_str (x ).lower ()],
230
230
'to_text' : lambda x : _str (x ).lower (),
231
231
},
232
- 'xs: base64Binary' : {
232
+ 'base64Binary' : {
233
233
'type' : _str ,
234
234
'to_type' : _str ,
235
235
'to_text' : lambda x :
236
236
_b64_encode_fn (x .encode ())
237
237
if base64encode
238
238
else x ,
239
239
},
240
- 'xs: anyType' : {
240
+ 'anyType' : {
241
241
'type' : type (value ),
242
242
'to_type' : lambda x : x ,
243
243
'to_text' : lambda x : x ,
@@ -249,29 +249,41 @@ def _wrong_type_value(xs_type, value):
249
249
},
250
250
}
251
251
252
- xs_type = \
253
- 'xs:base64Binary' \
254
- if base64encode \
255
- else self .get_type () \
256
- or xs_type_from_type .get (type (value ), type (None ))
257
- xs_type_map = xs_types_map .get (xs_type , {})
258
- valid_type = xs_type_map .get ('type' , type (None ))
259
- to_type = xs_type_map .get ('to_type' , str )
260
- to_text = xs_type_map .get ('to_text' , str )
252
+ xsd_string = (
253
+ 'base64Binary' if base64encode
254
+ else self .get_type ()
255
+ or type_to_xsd .get (type (value )))
256
+
257
+ xsd_ns , xsd_type = (
258
+ ['' , type (None )] if xsd_string is None
259
+ else ['' , '' ] if xsd_string is ''
260
+ else [
261
+ XSD if xsd_string in xsd_types_props else '' ,
262
+ xsd_string
263
+ ] if ':' not in xsd_string
264
+ else xsd_string .split (':' , 1 ))
265
+
266
+ xsd_type_props = xsd_types_props .get (xsd_type , {})
267
+ valid_type = xsd_type_props .get ('type' , type (None ))
268
+ to_type = xsd_type_props .get ('to_type' , str )
269
+ to_text = xsd_type_props .get ('to_text' , str )
261
270
262
271
# cast to correct type before type-checking
263
272
if type (value ) is _str and valid_type is not _str :
264
273
try :
265
274
value = to_type (value )
266
275
except (TypeError , ValueError , KeyError ) as e :
267
276
# the cast failed
268
- _wrong_type_value (xs_type = xs_type , value = value )
277
+ _wrong_type_value (xsd = xsd_type , value = value )
269
278
270
279
if type (value ) is not valid_type :
271
- _wrong_type_value (xs_type = xs_type , value = value )
280
+ _wrong_type_value (xsd = xsd_type , value = value )
272
281
273
282
text = to_text (value )
274
- self .set_type (xs_type )
283
+ self .set_type (
284
+ '{ns}:{type}' .format (ns = xsd_ns , type = xsd_type ) if xsd_ns
285
+ else xsd_type if xsd_type
286
+ else '' )
275
287
SamlBase .__setattr__ (self , 'text' , text )
276
288
return self
277
289
0 commit comments