forked from mehcode/python-saml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.py
More file actions
62 lines (42 loc) · 1.19 KB
/
types.py
File metadata and controls
62 lines (42 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
from dateutil.parser import parse as parse_datetime
class Base(object):
def prepare(self, value):
return str(value) if value is not None else None
def clean(self, text):
if text is not None:
return text
class String(Base):
"""String values [saml-core § 1.3.1].
"""
class Integer(Base):
"""Integral values.
"""
class Boolean(Base):
"""Boolean values.
"""
def prepare(self, value):
if value is None:
return None
return 'true' if value else 'false'
def clean(self, text):
if not text:
return None
return text == 'true'
class DateTime(Base):
"""
An ISO 8601 formatted, UTC (eg. 2008-01-23T04:56:22Z)
date-time [saml-core § 1.3.3].
"""
@staticmethod
def to_iso8601(when):
text = when.strftime("%Y-%m-%dT%H:%M:%SZ")
return text
@staticmethod
def from_iso8601(when):
return parse_datetime(when)
def prepare(self, value):
return DateTime.to_iso8601(value) if value is not None else None
def clean(self, text):
if text is not None:
return DateTime.from_iso8601(text)