Skip to content

Commit 83bfa20

Browse files
committed
Combine dicts based on xs_type
Signed-off-by: Ivan Kanakarakis <[email protected]>
1 parent e3e9aea commit 83bfa20

File tree

1 file changed

+68
-52
lines changed

1 file changed

+68
-52
lines changed

src/saml2/saml.py

Lines changed: 68 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -200,60 +200,76 @@ def clear_type(self):
200200
pass
201201

202202
def set_text(self, value, base64encode=False):
203-
_xs_type_from_type = {
203+
xs_type_from_type = {
204204
str: 'xs:string',
205205
int: 'xs:integer',
206206
float: 'xs:float',
207207
bool: 'xs:boolean',
208208
type(None): '',
209209
}
210210

211-
_type_from_xs_type = {
212-
'xs:string': str,
213-
'xs:integer': int,
214-
'xs:short': int,
215-
'xs:int': int,
216-
'xs:long': int,
217-
'xs:float': float,
218-
'xs:double': float,
219-
'xs:boolean': bool,
220-
'xs:base64Binary': str,
221-
'xs:anyType': type(value),
222-
'': type(None),
223-
}
224-
225-
_typed_value_constructor_from_xs_type = {
226-
'xs:string': str,
227-
'xs:integer': int,
228-
'xs:short': int,
229-
'xs:int': int,
230-
'xs:long': int,
231-
'xs:float': float,
232-
'xs:double': float,
233-
'xs:boolean': lambda x:
234-
True if str(x).lower() == 'true'
235-
else False if str(x).lower() == 'false'
236-
else None,
237-
'xs:base64Binary': str,
238-
'xs:anyType': lambda x: value,
239-
'': lambda x: None,
240-
}
241-
242-
_text_constructor_from_xs_type = {
243-
'xs:string': str,
244-
'xs:integer': str,
245-
'xs:short': str,
246-
'xs:int': str,
247-
'xs:long': str,
248-
'xs:float': str,
249-
'xs:double': str,
250-
'xs:boolean': lambda x: str(x).lower(),
251-
'xs:base64Binary': lambda x:
252-
_b64_encode_fn(x.encode())
253-
if base64encode
254-
else x,
255-
'xs:anyType': lambda x: value,
256-
'': lambda x: '',
211+
xs_types_map = {
212+
'xs:string': {
213+
'type': str,
214+
'typed_constructor': str,
215+
'text_constructor': str,
216+
},
217+
'xs:integer': {
218+
'type': int,
219+
'typed_constructor': int,
220+
'text_constructor': str,
221+
},
222+
'xs:short': {
223+
'type': int,
224+
'typed_constructor': int,
225+
'text_constructor': str,
226+
},
227+
'xs:int': {
228+
'type': int,
229+
'typed_constructor': int,
230+
'text_constructor': str,
231+
},
232+
'xs:long': {
233+
'type': int,
234+
'typed_constructor': int,
235+
'text_constructor': str,
236+
},
237+
'xs:float': {
238+
'type': float,
239+
'typed_constructor': float,
240+
'text_constructor': str,
241+
},
242+
'xs:double': {
243+
'type': float,
244+
'typed_constructor': float,
245+
'text_constructor': str,
246+
},
247+
'xs:boolean': {
248+
'type': bool,
249+
'typed_constructor': lambda x:
250+
True if str(x).lower() == 'true'
251+
else False if str(x).lower() == 'false'
252+
else None,
253+
'text_constructor': lambda x: str(x).lower(),
254+
},
255+
'xs:base64Binary': {
256+
'type': str,
257+
'typed_constructor': str,
258+
'text_constructor': lambda x:
259+
_b64_encode_fn(x.encode())
260+
if base64encode
261+
else x,
262+
},
263+
'xs:anyType': {
264+
'type': type(value),
265+
'typed_constructor': lambda x: value,
266+
'text_constructor': lambda x: value,
267+
},
268+
'': {
269+
'type': type(None),
270+
'typed_constructor': lambda x: None,
271+
'text_constructor': lambda x: '',
272+
},
257273
}
258274

259275
if isinstance(value, six.binary_type):
@@ -263,11 +279,11 @@ def set_text(self, value, base64encode=False):
263279
'xs:base64Binary' \
264280
if base64encode \
265281
else self.get_type() \
266-
or _xs_type_from_type.get(type(value), type(None))
267-
268-
valid_type = _type_from_xs_type.get(xs_type, type(None))
269-
to_typed = _typed_value_constructor_from_xs_type.get(xs_type, str)
270-
to_text = _text_constructor_from_xs_type.get(xs_type, str)
282+
or xs_type_from_type.get(type(value), type(None))
283+
xs_type_map = xs_types_map.get(xs_type, {})
284+
valid_type = xs_type_map.get('type', type(None))
285+
to_typed = xs_type_map.get('typed_constructor', str)
286+
to_text = xs_type_map.get('text_constructor', str)
271287

272288
value = to_typed(value)
273289
if type(value) is not valid_type:

0 commit comments

Comments
 (0)